Commit e214127c
e214127c40e3Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): poly arg to IntStrHash key unboxes via .v.i (#438)
Targeted minimal fix for the canonical `is_a?(Integer)` shape from the issue: `if status.is_a?(Integer); table.fetch(status, "x"); end` where status is a poly param and table is an Int- keyed hash. The IntStrHash dispatch (`[]`, has_key?, fetch) previously compiled the key arg via compile_arg0 / compile_expr without unboxing, and the C call passed sp_RbVal to a mrb_int param.
Route the three key-receiving arms through compile_arg0_as_int (or explicit `.v.i` for fetch's pre-compiled key), matching the existing pattern used for int_array index args and arithmetic operators. At runtime the is_a? guard ensures the SP_TAG_INT tag so the unbox reads the intended int; without a guard the lookup still compiles but reads a meaningless union pun (no crash — union type-punning is defined, the hash just gets a non-meaningful key and the default arm fires).
Scope: only IntStrHash key context. The broader is_a? narrowing for other Int-context use sites (method-call args, arithmetic on poly, sym-keyed hash with symbol narrow, etc.) remains a separate concern — the prior attempt to lift narrow-aware unbox into compile_expr's LocalVariableReadNode arm leaked unboxing into poly-required sites (sp_poly_to_s, string interpolation) and broke existing tests, so this commit limits itself to the specific shape #438's reproducer flagged.
Closes #438
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e214127c40e3
- Parent
- c91f9c071b32
spinel_codegen.rbmodifiedView file
if recv_type == "int_str_hash" @needs_int_str_hash = 1 if mname == "[]" return "sp_IntStrHash_get(" + rc + ", " + compile_arg0(nid) + ")" return "sp_IntStrHash_get(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?" return "sp_IntStrHash_has_key(" + rc + ", " + compile_arg0(nid) + ")" return "sp_IntStrHash_has_key(" + rc + ", " + compile_arg0_as_int(nid) + ")" end if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0) return "sp_IntStrHash_length(" + rc + ")" if args_id >= 0 aargs = get_args(args_id) key = compile_expr(aargs[0]) if infer_type(aargs[0]) == "poly" key = "(" + key + ").v.i" end if aargs.length >= 2 defval = compile_expr(aargs[1]) return "(sp_IntStrHash_has_key(" + rc + ", " + key + ") ? sp_IntStrHash_get(" + rc + ", " + key + ") : " + defval + ")"test/poly_arg_int_keyed_hash.rbaddedView file
Loading diff…
test/poly_arg_int_keyed_hash.rb.expectedaddedView file
Loading diff…