Commit 1898605d

1898605dc753Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Resolve namespaced exception classes in raise and rescue

raise M::Err, msg emitted a bare sp_raise (dropping the class) and rescue M::Err matched only the leaf name "Err", so a namespaced rescue never caught its own raised exception. Both the raise paths (statement and expression context, one- and two-arg forms) and the rescue-clause class check now resolve a ConstantPathNode to the registered class name (M_Err) via resolve_const_ref_name, matching how the class is registered and how the hierarchy walk keys it.

Fixes #1041

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
1898605dc753
Parent
7b753f3748bb

spinel_codegen.rbmodifiedView file

@@ -1358,6 +1358,16 @@
13581358 emit(" sp_raise(\"" + cname_cr + "\");")
13591359 return
13601360 end
1361+ # `raise M::Err` -- namespaced exception class, no message. Resolve
1362+ # to the registered class name so the class survives for rescue.
1363+ if @nd_type[arg_id] == "ConstantPathNode"
1364+ cname_cp = resolve_const_ref_name(arg_id)
1365+ if is_exception_class_name(cname_cp) == 1
1366+ @needs_exc_class_hierarchy = 1
1367+ emit(" sp_raise_cls(\"" + cname_cp + "\", " + exc_default_msg_expr(cname_cp) + ");")
1368+ return
1369+ end
1370+ end
13611371 # `raise <BuiltinExc>.new(<msg>)` -- ConstantReadNode is the exc
13621372 # class, msg comes from the .new args.
13631373 if @nd_type[arg_id] == "CallNode" && @nd_name[arg_id] == "new"
@@ -17862,6 +17872,10 @@
1786217872 if arg_ids.length >= 2
1786317873 if @nd_type[arg_ids[0]] == "ConstantReadNode"
1786417874 emit(" sp_raise_cls(\"" + @nd_name[arg_ids[0]] + "\", " + compile_expr(arg_ids[1]) + ");")
17875+ elsif @nd_type[arg_ids[0]] == "ConstantPathNode"
17876+ # `raise M::Err, msg` -- resolve the namespaced constant to the
17877+ # registered class name (M_Err) so it matches `rescue M::Err`.
17878+ emit(" sp_raise_cls(\"" + resolve_const_ref_name(arg_ids[0]) + "\", " + compile_expr(arg_ids[1]) + ");")
1786517879 else
1786617880 emit(" sp_raise(" + compile_expr(arg_ids[1]) + ");")
1786717881 end
@@ -36616,6 +36630,10 @@
3661636630 # raise ClassName, "message" - use the message with class
3661736631 if @nd_type[arg_ids[0]] == "ConstantReadNode"
3661836632 emit(" sp_raise_cls(\"" + @nd_name[arg_ids[0]] + "\", " + compile_expr(arg_ids[1]) + ");")
36633+ elsif @nd_type[arg_ids[0]] == "ConstantPathNode"
36634+ # raise M::Err, msg -- resolve namespaced constant to its
36635+ # registered class name so `rescue M::Err` matches.
36636+ emit(" sp_raise_cls(\"" + resolve_const_ref_name(arg_ids[0]) + "\", " + compile_expr(arg_ids[1]) + ");")
3661936637 else
3662036638 emit(" sp_raise(" + compile_expr(arg_ids[1]) + ");")
3662136639 end
@@ -43900,7 +43918,13 @@
4390043918 if k > 0
4390143919 cond = cond + " || "
4390243920 end
43903- cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")"
43921+ # Namespaced rescue target (`rescue M::Err`) resolves to the
43922+ # registered class name (M_Err); a bare name uses @nd_name as-is.
43923+ excn_k = @nd_name[exc_types[k]]
43924+ if @nd_type[exc_types[k]] == "ConstantPathNode"
43925+ excn_k = resolve_const_ref_name(exc_types[k])
43926+ end
43927+ cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + excn_k + "\")"
4390443928 k = k + 1
4390543929 end
4390643930 emit(" if (" + cond + ") {")
@@ -45797,7 +45821,13 @@
4579745821 if k > 0
4579845822 cond = cond + " || "
4579945823 end
45800- cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + @nd_name[exc_types[k]] + "\")"
45824+ # Namespaced rescue target (`rescue M::Err`) resolves to the
45825+ # registered class name (M_Err); a bare name uses @nd_name as-is.
45826+ excn_k = @nd_name[exc_types[k]]
45827+ if @nd_type[exc_types[k]] == "ConstantPathNode"
45828+ excn_k = resolve_const_ref_name(exc_types[k])
45829+ end
45830+ cond = cond + "sp_exc_class_le((const char*)sp_last_exc_cls, \"" + excn_k + "\")"
4580145831 k = k + 1
4580245832 end
4580345833 emit(" if (" + cond + ") {")