From 4c03ac5b36b05d08a37b91b6bca4dd0be668d3a1 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Sun, 12 Jan 2025 19:38:19 -0800 Subject: [PATCH] Reject invalid casts to real Only vector types can be cast to real. Report an error when trying to cast a different type instead of triggering an assert later on. Signed-off-by: Lars-Peter Clausen --- elab_expr.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/elab_expr.cc b/elab_expr.cc index 682c017ba..8b77b9c76 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -3706,7 +3706,20 @@ NetExpr* PECastType::elaborate_expr(Design*des, NetScope*scope, NetExpr*tmp = 0; if (dynamic_cast(target_type_)) { - return cast_to_real(sub); + switch (sub->expr_type()) { + case IVL_VT_REAL: + return sub; + case IVL_VT_LOGIC: + case IVL_VT_BOOL: + return cast_to_real(sub); + default: + break; + } + cerr << get_fileline() << " error: Expression of type `" + << sub->expr_type() << "` can not be cast to target type `real`." + << endl; + des->errors++; + return nullptr; } else if (dynamic_cast(target_type_)) { if (base_->expr_type() == IVL_VT_STRING) return sub; // no conversion