Commit 39a30d36

39a30d368438Merged

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

Description

codegen: pad proc-call args + recognise .inspect cache-miss path

Two related bugs in Proc.new { |a, b| ... }.call(1):

  1. compile_proc_call_args built the C99 compound literal (mrb_int[]){1}. The proc body read args[1], which lay past the stack-local literal — pointer-sized garbage. Pad to 16 zero slots so missing args read as 0 (matches sp_proc_call's noargs path which already uses a 16-element zero buffer).

  2. Codegen's infer_type cache-miss fallback recognised .to_s but not .inspect, so a proc body's "#{a.inspect}" got typed int by the default arm. The string-interp dispatcher then picked %lld and cast the string pointer to long long — printing a decimal address instead of the value.

The user-visible result for unfilled params is now 0 (matches the existing #997 pattern) rather than the prior pointer-garbage; the int→int? widening for true nil parity is the same deferred cascade as #801 Phase 4.

Fixes #976

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

Details

Commit
39a30d368438
Parent
5599c98e9229

spinel_codegen.rbmodifiedView file

@@ -3440,6 +3440,13 @@
34403440 if cmp_mname == "to_s"
34413441 return "string"
34423442 end
3443+ # `.inspect` returns string for every recv type. Without this
3444+ # arm a proc/lambda body's `"#{a.inspect}"` got typed int by
3445+ # the fallback, the interp dispatcher picked %lld, and the
3446+ # string pointer printed as a decimal address.
3447+ if cmp_mname == "inspect"
3448+ return "string"
3449+ end
34433450 # `lv.call(...)` / `lv.()` on a lambda local — recover the
34443451 # lambda's recorded return type from @lambda_var_ret_*.
34453452 # walk_and_cache skips lambda bodies, so the call itself isn't
@@ -30252,12 +30259,9 @@
3025230259 def compile_proc_call_args(nid)
3025330260 args_id = @nd_arguments[nid]
3025430261 if args_id < 0
30255- return "0"
30262+ return "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
3025630263 end
3025730264 arg_ids = get_args(args_id)
30258- if arg_ids.length == 0
30259- return "0"
30260- end
3026130265 result = ""
3026230266 k = 0
3026330267 while k < arg_ids.length
@@ -30280,6 +30284,17 @@
3028030284 end
3028130285 k = k + 1
3028230286 end
30287+ # Pad the compound literal to 16 zero slots so a proc body that
30288+ # reads `args[i]` past the caller-supplied arity sees 0 instead
30289+ # of past-the-end stack garbage. Matches sp_proc_call's noargs
30290+ # fast-path which already uses a 16-element zero-init buffer.
30291+ while k < 16
30292+ if k > 0
30293+ result = result + ", "
30294+ end
30295+ result = result + "0"
30296+ k = k + 1
30297+ end
3028330298 result
3028430299 end
3028530300