Commit 36f7fe41
36f7fe41f83aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #348 from ryanseys/rs/exception-rescue-bindings
fix(codegen): exception object methods, bare-raise re-raise, propagate-on-no-match
Details
- Commit
- 36f7fe41f83a
- Parent
- 2e1c2c409741
- Merge parent
- 9aa3594a0b6c
spinel_codegen.rbmodifiedView file
@@ -302,6 +302,19 @@
302302
@needs_sym_str_hash = 0303303
@needs_sym_intern = 0304304
@needs_setjmp = 0305+
# Stack of (class_var, msg_var) pairs naming the snapshot locals306+
# emitted at the top of each rescue body. A bare `raise` inside a307+
# rescue body re-raises with the snapshotted class+message rather308+
# than fabricating a fresh RuntimeError. Empty outside any rescue.309+
@rescue_cls_stack = "".split(",")310+
@rescue_msg_stack = "".split(",")311+
@rescue_depth = 0312+
# Exception variable bindings: parallel stacks of (var_name, cls_var).313+
# A `rescue => e` binds `e` to the message string and registers it314+
# here so that `e.message`, `e.class`, `e.to_s`, and `e.inspect`315+
# dispatch correctly. Pushed at rescue body entry, popped at exit.316+
@exc_var_names = "".split(",")317+
@exc_var_cls_vars = "".split(",")305318
@needs_mutable_str = 0306319
@needs_rb_value = 0307320
@needs_regexp = 0@@ -1270,6 +1283,33 @@
12701283
012711284
end12721285
1286+
# Returns the snapshot class-name C variable for an exception1287+
# variable currently bound by an enclosing `rescue => name`, or1288+
# empty string if `name` is not an active exception binding.1289+
def find_exc_var_cls(name)1290+
i = @exc_var_names.length - 11291+
while i >= 01292+
if @exc_var_names[i] == name1293+
return @exc_var_cls_vars[i]1294+
end1295+
i = i - 11296+
end1297+
""1298+
end1299+
1300+
# Emit a bare `raise` (no message arg). Inside a rescue body the1301+
# snapshotted class+message is re-raised; outside any rescue it1302+
# falls back to a fresh RuntimeError, matching CRuby.1303+
def emit_bare_raise1304+
if @rescue_cls_stack.length > 01305+
rcls = @rescue_cls_stack.last1306+
rmsg = @rescue_msg_stack.last1307+
emit(" sp_raise_cls(" + rcls + ", " + rmsg + ");")1308+
else1309+
emit(" sp_raise(\"RuntimeError\");")1310+
end1311+
end1312+
12731313
def find_var_type(name)12741314
# Type-narrow override (issue #207): walk top-down so the12751315
# innermost is_a? guard wins. Skipped for the unscoped global@@ -2706,6 +2746,20 @@
27062746
mname = @nd_name[nid]27072747
recv = @nd_receiver[nid]27082748
2749+
# Methods on a `rescue => e` bound exception variable. The variable2750+
# itself is string-typed but .class / .message / .to_s / .inspect /2751+
# .full_message return strings; .backtrace returns nil for now.2752+
if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"2753+
if find_exc_var_cls(@nd_name[recv]) != ""2754+
if mname == "message" || mname == "to_s" || mname == "class" || mname == "inspect" || mname == "full_message"2755+
return "string"2756+
end2757+
if mname == "backtrace"2758+
return "nil"2759+
end2760+
end2761+
end2762+
27092763
# `recv.__sp_ieval_<N>(...)`: the rewritten form of an27102764
# `recv.instance_eval { ... }` call. v1 only fired on top-level call27112765
# sites, where the call's value was always discarded — so its return@@ -21278,6 +21332,35 @@
2127821332
mname = @nd_name[nid]2127921333
recv = @nd_receiver[nid]2128021334
21335+
# Exception variable bound by `rescue => e`: spinel binds `e` to21336+
# the message string but tracks it as an exception in21337+
# @exc_var_names so .class / .message / .to_s / .inspect dispatch21338+
# correctly. The variable's static type is still "string", so any21339+
# other method call falls through to string method dispatch — this21340+
# preserves backward compatibility for the common `puts e` shape.21341+
if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"21342+
exc_cls = find_exc_var_cls(@nd_name[recv])21343+
if exc_cls != ""21344+
recv_name = @nd_name[recv]21345+
if mname == "message" || mname == "to_s"21346+
return "lv_" + recv_name21347+
end21348+
if mname == "class"21349+
return exc_cls21350+
end21351+
if mname == "inspect"21352+
return "sp_sprintf(\"#<%s: %s>\", " + exc_cls + ", lv_" + recv_name + ")"21353+
end21354+
if mname == "backtrace"21355+
# Spinel doesn't track per-exception backtraces; return nil.21356+
return "0"21357+
end21358+
if mname == "full_message"21359+
return "sp_sprintf(\"%s: %s\", " + exc_cls + ", lv_" + recv_name + ")"21360+
end21361+
end21362+
end21363+
2128121364
# Issue #126: `Module.accessor.<method>` where the slot was2128221365
# resolved by `resolve_module_singleton_accessors`. With a single2128321366
# constant in the resolved set, inline the call directly. With@@ -22121,7 +22204,7 @@
2212122204
emit(" sp_raise(" + compile_expr(arg_ids[0]) + ");")2212222205
end2212322206
else22124-
emit(" sp_raise(\"RuntimeError\");")22207+
emit_bare_raise2212522208
end2212622209
return "0"2212722210
end@@ -31397,11 +31480,11 @@
3139731480
emit(" sp_raise(" + compile_expr(arg_ids[0]) + ");")3139831481
end3139931482
else31400-
emit(" sp_raise(\"RuntimeError\");")31483+
emit_bare_raise3140131484
end3140231485
end3140331486
else31404-
emit(" sp_raise(\"RuntimeError\");")31487+
emit_bare_raise3140531488
end3140631489
return 13140731490
end@@ -35867,6 +35950,19 @@
3586735950
# Check for exception type matching3586835951
exc_types = parse_id_list(@nd_exceptions[rc])3586935952
ref = @nd_reference[rc]35953+
# Snapshot the in-flight exception BEFORE the type-check `if` so35954+
# that the else-branch (re-raise / propagation) can also reference35955+
# the snapshot. Bare `raise` inside this body re-raises with the35956+
# original class+message even if the body itself triggers (and35957+
# catches) a nested raise that would otherwise clobber35958+
# sp_last_exc_cls / sp_exc_msg[sp_exc_top].35959+
@rescue_depth = @rescue_depth + 135960+
saved_cls = "_rescue_cls_" + @rescue_depth.to_s35961+
saved_msg = "_rescue_msg_" + @rescue_depth.to_s35962+
emit(" const char *" + saved_cls + " = (const char *)sp_last_exc_cls;")35963+
emit(" const char *" + saved_msg + " = sp_exc_msg[sp_exc_top];")35964+
@rescue_cls_stack.push(saved_cls)35965+
@rescue_msg_stack.push(saved_msg)3587035966
has_type_check = 03587135967
if exc_types.length > 03587235968
has_type_check = 1@@ -35883,11 +35979,21 @@
3588335979
emit(" if (" + cond + ") {")3588435980
@indent = @indent + 13588535981
end35982+
bound_rname = ""3588635983
if ref >= 035887-
rname = @nd_name[ref]35888-
emit(" lv_" + rname + " = sp_exc_msg[sp_exc_top];")35984+
bound_rname = @nd_name[ref]35985+
emit(" lv_" + bound_rname + " = " + saved_msg + ";")35986+
@exc_var_names.push(bound_rname)35987+
@exc_var_cls_vars.push(saved_cls)3588935988
end3589035989
compile_rescue_body(@nd_body[rc], has_retry)35990+
if bound_rname != ""35991+
@exc_var_names.pop35992+
@exc_var_cls_vars.pop35993+
end35994+
@rescue_cls_stack.pop35995+
@rescue_msg_stack.pop35996+
@rescue_depth = @rescue_depth - 13589135997
if has_type_check == 13589235998
@indent = @indent - 13589335999
# Check for subsequent rescue@@ -35897,6 +36003,13 @@
3589736003
@indent = @indent + 13589836004
compile_rescue_chain(sub, has_retry)3589936005
@indent = @indent - 136006+
else36007+
# No subsequent clause and the type check missed: re-raise36008+
# using the snapshotted class+message so an enclosing handler36009+
# can match it (or the program terminates with the original36010+
# diagnostic). Without this the exception was silently dropped.36011+
emit(" } else {")36012+
emit(" sp_raise_cls(" + saved_cls + ", " + saved_msg + ");")3590036013
end3590136014
emit(" }")3590236015
endtest/exception_object_methods.rbaddedView file
Loading diff…
test/exception_object_methods.rb.expectedaddedView file
Loading diff…