Commit 90e9db0d

90e9db0d9ebbMerged

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

Description

Type rescue-bound locals inside lambda/proc bodies as exceptions

rescue => e inside a lambda {} / proc {} body bound e in the enclosing scope, so the closure's generated function referenced an undeclared lv_e (hard compile error) and, once declared, infer_type recomputed e.message as int because walk_and_cache skips proc/lambda bodies and the codegen recompute path lacked the exception-method arm. A "#{e.message}" interpolation then formatted the string pointer with %lld.

compile_proc_literal now collects the rescue-bound names lexically in the body (not descending into nested closures), drops them from the capture set, declares them as body-local exceptions, and emits the C declaration -- mirroring the Fiber-body handling. codegen's infer_type gains the exception-method return arm (message / full_message / class -> string, backtrace -> str_array) mirroring spinel_analyze.rb so the cache and the proc/lambda-body recompute agree (keeping the self-host fixpoint stable).

The OP's literal puts f.call still renders the returned string as an integer: that is a separate proc-call return-value ABI gap (the lambda/proc .call return type), tracked in #1362, not this mistype.

Closes #1363 Refs #1362

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

Details

Commit
90e9db0d9ebb
Parent
5cdf761735b4

spinel_codegen.rbmodifiedView file

@@ -3958,6 +3958,24 @@
39583958 if cmp_mname == "inspect"
39593959 return "string"
39603960 end
3961+ # Methods on a first-class exception recv. walk_and_cache skips
3962+ # proc/lambda bodies, so `e.message` inside a closure is recomputed
3963+ # here; without this it falls to the int default and a `#{e.message}`
3964+ # interp picks %lld on the string pointer. Mirrors spinel_analyze.rb's
3965+ # exception arm (3431) so cache and recompute agree.
3966+ if cmp_mname == "message" || cmp_mname == "full_message" || cmp_mname == "class"
3967+ cmp_exc_recv = @nd_receiver[nid]
3968+ if cmp_exc_recv >= 0 && base_type(infer_type(cmp_exc_recv)) == "exception"
3969+ return "string"
3970+ end
3971+ end
3972+ if cmp_mname == "backtrace"
3973+ cmp_bt_recv = @nd_receiver[nid]
3974+ if cmp_bt_recv >= 0 && base_type(infer_type(cmp_bt_recv)) == "exception"
3975+ @needs_str_array = 1
3976+ return "str_array"
3977+ end
3978+ end
39613979 # `lv.call(...)` / `lv.()` on a lambda local — recover the
39623980 # lambda's recorded return type from @lambda_var_ret_*.
39633981 # walk_and_cache skips lambda bodies, so the call itself isn't
@@ -43062,11 +43080,34 @@
4306243080 # Every block param is in scope inside the body; only locals from
4306343081 # the outer scope read inside the body count as free.
4306443082 free_vars = "".split(",", -1)
43083+ # `rescue => e` inside the proc/lambda body binds `e` as a body local,
43084+ # not a capture of a same-named outer var. compile_rescue_chain emits a
43085+ # bare `lv_e = sp_exc_new(...)`, so the name must be declared (typed
43086+ # exception) as a body local and dropped from the capture set. Mirrors
43087+ # the Fiber-body handling.
43088+ rb_names_p = "".split(",", -1)
43089+ if bbody >= 0
43090+ collect_rescue_bound_names(bbody, rb_names_p)
43091+ end
4306543092 if bbody >= 0
4306643093 # scan_lambda_free_vars treats `params` as read-only, so bps can
4306743094 # be passed directly without an intermediate copy.
4306843095 proc_locals = "".split(",", -1)
4306943096 scan_lambda_free_vars(bbody, bps, proc_locals, free_vars)
43097+ # Drop rescue-bound names from the free-var set so they aren't captured.
43098+ rbpf = 0
43099+ while rbpf < rb_names_p.length
43100+ new_fv_p = "".split(",", -1)
43101+ fvk = 0
43102+ while fvk < free_vars.length
43103+ if free_vars[fvk] != rb_names_p[rbpf]
43104+ new_fv_p.push(free_vars[fvk])
43105+ end
43106+ fvk = fvk + 1
43107+ end
43108+ free_vars = new_fv_p
43109+ rbpf = rbpf + 1
43110+ end
4307043111 end
4307143112 captures = "".split(",", -1)
4307243113 capture_types = "".split(",", -1)
@@ -43140,6 +43181,19 @@
4314043181 declare_var(bps[di], pt)
4314143182 di = di + 1
4314243183 end
43184+ # Declare `rescue => e` bindings as body locals typed exception so the
43185+ # bare `lv_e = sp_exc_new(...)` compiles and `e.<exc method>` infers
43186+ # off the exception type rather than the int fallback. Both register the
43187+ # scope type (so find_var_type resolves `exception` on recompute) and
43188+ # emit the C declaration into the body (mirrors the fiber-body path).
43189+ rbpd = 0
43190+ while rbpd < rb_names_p.length
43191+ if not_in(rb_names_p[rbpd], bps) == 1
43192+ declare_var(rb_names_p[rbpd], "exception")
43193+ emit(" " + c_type("exception") + " lv_" + rb_names_p[rbpd] + " = " + c_default_val("exception") + ";")
43194+ end
43195+ rbpd = rbpd + 1
43196+ end
4314343197 bexpr = "0"
4314443198 bexpr_t_proc = "int"
4314543199 body_stmts = ""