Commit 25e1efc9

25e1efc9339bMerged

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

Description

fix(codegen): proc arg packing unboxes bigint; expr_emit detects method_ref alias

Two related promote-mode fixes for Proc/Method paths.

  1. compile_proc_call_args: a bigint arg packed via the proc ABI's mrb_int* array used (mrb_int)(uintptr_t)val -- but val is an sp_Bigint* heap pointer, not a tagged int. The proc body then read the pointer's low bits as n, producing wild output. Unbox via sp_bigint_to_int before packing.

  2. expr_emit_is_bigint: extend the Method.call branch to also catch the static-alias path. m = method(:foo); m.call(x) rewrites in compile_dot_call_expr to a direct sp_foo(x) -- no Method object exists at runtime, so the caller's type lookup falls to the LV's stale int slot type. Resolve via @method_ref_vars and inspect the target method's promoted return type so puts / arg-coerce paths see bigint.

Fixes: method, proc, proc_closure under promote.

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

Details

Commit
25e1efc9339b
Parent
e0c45fb3f979

spinel_codegen.rbmodifiedView file

@@ -15810,6 +15810,26 @@
1581015810 if rcv_bm_eb >= 0 && base_type(infer_type(rcv_bm_eb)) == "obj_Method"
1581115811 return 1
1581215812 end
15813+ # Static alias: `m = method(:foo)` then `m.call(x)` rewrites to
15814+ # a direct `sp_foo(x)` (see compile_dot_call_expr). Look up the
15815+ # target method's promote-mode return.
15816+ if rcv_bm_eb >= 0 && @nd_type[rcv_bm_eb] == "LocalVariableReadNode"
15817+ rname_ma = @nd_name[rcv_bm_eb]
15818+ ri_ma = 0
15819+ while ri_ma < @method_ref_vars.length
15820+ if @method_ref_vars[ri_ma] == rname_ma
15821+ ref_mname_ma = @method_ref_names[ri_ma]
15822+ mi_ma = find_method_idx(ref_mname_ma)
15823+ if mi_ma >= 0 && mi_ma < @meth_return_types.length
15824+ if base_type(@meth_return_types[mi_ma]) == "bigint"
15825+ return 1
15826+ end
15827+ end
15828+ ri_ma = @method_ref_vars.length
15829+ end
15830+ ri_ma = ri_ma + 1
15831+ end
15832+ end
1581315833 end
1581415834 # CallNode dispatch to a user method whose return slot has been
1581515835 # promoted to bigint: infer_type can be stale (the cls_method
@@ -25055,7 +25075,14 @@
2505525075 end
2505625076 val = compile_expr(arg_ids[k])
2505725077 at = infer_type(arg_ids[k])
25058- if type_is_pointer(at) == 1
25078+ if base_type(at) == "bigint"
25079+ # Proc ABI is `mrb_int(*)(void*, mrb_int*)`. In promote mode an
25080+ # int arg arrives as sp_Bigint *; unbox to the underlying int
25081+ # before packing. Wrapping the pointer via (mrb_int)(uintptr_t)
25082+ # would silently pass a heap address as the proc's `n`.
25083+ @needs_bigint = 1
25084+ result = result + "sp_bigint_to_int((sp_Bigint *)" + val + ")"
25085+ elsif type_is_pointer(at) == 1
2505925086 result = result + "(mrb_int)(uintptr_t)(" + val + ")"
2506025087 else
2506125088 result = result + val