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

@@ -16220,10 +16220,10 @@
1622016220 if recv_type == "int_str_hash"
1622116221 @needs_int_str_hash = 1
1622216222 if mname == "[]"
16223- return "sp_IntStrHash_get(" + rc + ", " + compile_arg0(nid) + ")"
16223+ return "sp_IntStrHash_get(" + rc + ", " + compile_arg0_as_int(nid) + ")"
1622416224 end
1622516225 if mname == "has_key?" || mname == "key?" || mname == "include?" || mname == "member?"
16226- return "sp_IntStrHash_has_key(" + rc + ", " + compile_arg0(nid) + ")"
16226+ return "sp_IntStrHash_has_key(" + rc + ", " + compile_arg0_as_int(nid) + ")"
1622716227 end
1622816228 if mname == "length" || mname == "size" || (mname == "count" && @nd_block[nid] < 0 && @nd_arguments[nid] < 0)
1622916229 return "sp_IntStrHash_length(" + rc + ")"
@@ -16247,6 +16247,9 @@
1624716247 if args_id >= 0
1624816248 aargs = get_args(args_id)
1624916249 key = compile_expr(aargs[0])
16250+ if infer_type(aargs[0]) == "poly"
16251+ key = "(" + key + ").v.i"
16252+ end
1625016253 if aargs.length >= 2
1625116254 defval = compile_expr(aargs[1])
1625216255 return "(sp_IntStrHash_has_key(" + rc + ", " + key + ") ? sp_IntStrHash_get(" + rc + ", " + key + ") : " + defval + ")"