Commit e4c4118e

e4c4118efb3fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

feat(codegen,analyze): rescue => e binds sp_Exception * (Phase 2.3)

Promote rescue => e binding from a const char * message slot (plus a @exc_var_names side-channel carrying the class) to a first-class sp_Exception *. The binding now flows through the same recv_type == "exception" dispatch arm that Phase 2.2 added for BuiltinExc.new(...).

  • analyze: scan_locals_first_type types the rescue-bound LV as "exception" (was "string"). walk_and_cache no longer needs to push the bound name onto @exc_var_names -- infer_call_type's Phase 2 arm fires via the typed receiver.
  • codegen: compile_rescue_chain / compile_rescue_chain_into emit lv_<name> = sp_exc_new(_rescue_cls_N, _rescue_msg_N); so the LV holds an sp_Exception * with both class and message.
  • codegen puts/interpolation: add at == "exception" arms in compile_puts, compile_puts_single, and compile_interpolated so puts e / "#{e}" print the message (CRuby's implicit .to_s), preserving the Phase 1 surface where the LV was the message string. Without these, the pointer was printed as a long long.

The Phase 1 find_exc_var_cls dispatch arms in codegen/analyze are now unreachable (the rescue LV is typed "exception", so my Phase 2 arm wins). Cleanup of the dead code is left for a follow-up commit.

Tests: default 450/0/0, promote 450/0/0, optcarrot checksum 59662.

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

Details

Commit
e4c4118efb3f
Parent
9c62cd3d05a0

spinel_analyze.rbmodifiedView file

@@ -25861,7 +25861,10 @@
2586125861 end
2586225862 end
2586325863 end
25864- # Rescue reference (=> e) needs to be declared as a local
25864+ # Rescue reference (=> e) needs to be declared as a local. Phase 2
25865+ # promotes the binding to a first-class sp_Exception * (was const
25866+ # char * in Phase 1); compile_rescue_chain emits the
25867+ # `lv_<name> = sp_exc_new(<cls>, <msg>);` assignment that fills it.
2586525868 if @nd_type[nid] == "RescueNode"
2586625869 ref = @nd_reference[nid]
2586725870 if ref >= 0
@@ -25869,7 +25872,7 @@
2586925872 if not_in(lname, names) == 1
2587025873 if not_in(lname, params) == 1
2587125874 names.push(lname)
25872- types.push("string")
25875+ types.push("exception")
2587325876 @scan_literal_flags.push("")
2587425877 @scan_empty_flags.push("")
2587525878 @scan_empty_hash_flags.push("")
@@ -28046,20 +28049,10 @@
2804628049 if @nd_type[nid] == "DefNode"
2804728050 return
2804828051 end
28049- # RescueNode: register the bound exception var so infer_call_type
28050- # recognises `.message` / `.class` / etc. as string-returning methods
28051- # within the rescue body. compile_begin_with_rescue does the same
28052- # at emit time; we mirror it here.
28052+ # RescueNode: the bound exception var is typed "exception" (Phase 2.3),
28053+ # so infer_call_type's recv_type == "exception" arm handles dispatch
28054+ # without needing the @exc_var_names side-channel.
2805328055 if @nd_type[nid] == "RescueNode"
28054- ref_re = @nd_reference[nid]
28055- bound_re = ""
28056- if ref_re >= 0
28057- bound_re = @nd_name[ref_re]
28058- @exc_var_names.push(bound_re)
28059- # Class is unknown at analyze time but find_exc_var_cls's
28060- # callers only check for non-empty (presence). Use a sentinel.
28061- @exc_var_cls_vars.push("?")
28062- end
2806328056 cs_re = []
2806428057 push_child_ids(nid, cs_re)
2806528058 blk_re = @nd_block[nid]
@@ -28069,10 +28062,6 @@
2806928062 walk_and_cache(cs_re[k])
2807028063 end
2807128064 k = k + 1
28072- end
28073- if bound_re != ""
28074- @exc_var_names.pop
28075- @exc_var_cls_vars.pop
2807628065 end
2807728066 @nd_inferred_type[nid] = infer_type(nid)
2807828067 return