Commit af0dc6e8
af0dc6e8e641Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): unbox nullable-poly_hash get into typed param
When recv : sym_poly_hash? (or str_poly_hash?) survives a nil
guard and a recv[k] value flows into a typed-param call site,
analyze's cached infer for recv[k] falls through to int. The
[] dispatch in analyze checks rt == "sym_poly_hash" etc. but the
recv carries a ? suffix, so every concrete branch misses and the
leaf type lands on the int default. Downstream, compile_expr_for_
expected_type sees at == "int" and skips the poly→int unbox; the
actual C emit is sp_<*PolyHash>_get(...) returning sp_RbVal, so
the call passes sp_RbVal into a mrb_int slot.
Codegen-side detection: expr_emits_poly_rb_val recognizes the
nullable-poly_hash [] shape at emit time, independent of the
analyze cache. Wired into two arg-emit paths:
- compile_expr_for_expected_type: override
atto "poly" so the existing poly→prim unbox runs. - compile_poly_method_call arm: when the arm's param is concrete (int / string / symbol / float / bool) but the arg emits sp_RbVal, unbox via the matching union field before passing.
Found by /tmp/i450/spinel real-blog: `Main.instantiate_controller (matched[:controller])` and the four-class `process_action` poly dispatch on `matched[:action]` produced four C compile errors ("incompatible type for argument: sp_RbVal vs mrb_int"). All four clear with this change; full test suite (411 tests) still green.
Refs #450
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- af0dc6e8e641
- Parent
- 6165d11f070a
spinel_codegen.rbmodifiedView file
end val = compile_expr(nid) at = infer_type(nid) # Override `at` to "poly" when the nid's actual C emit is # sp_RbVal even though the cached static type was a narrower # default (the nullable-poly_hash `[]` shape — see # expr_emits_poly_rb_val for the failure mode). if at != "poly" && expr_emits_poly_rb_val(nid) == 1 at = "poly" end # `if v.is_a?(String); helper(v); end` (and analogues for int / # float / sym / obj) — `v`'s C storage is sp_RbVal but the # callee's param expects an unboxed concrete type. The user-level "poly" end # `nullable_poly_hash[k]` — recv's static type carries `?` (a # `if recv.nil?; return; end` upstream doesn't strip the suffix), # so analyze's [] dispatch falls through to the int default in # its cache. The actual emit is sp_<*PolyHash>_get(...) returning # sp_RbVal, so the cached "int" diverges from the C temp type and # the caller's int-typed param then receives an sp_RbVal unboxed. # Detect the shape here so compile_expr_for_expected_type and the # poly-dispatch arm can do the unbox even when cached "at" lied. def expr_emits_poly_rb_val(nid) if @nd_type[nid] != "CallNode" || @nd_name[nid] != "[]" return 0 end recv_id = @nd_receiver[nid] if recv_id < 0 return 0 end rt = infer_type(recv_id) bt = base_type(rt) if bt == "sym_poly_hash" || bt == "str_poly_hash" || bt == "poly_poly_hash" return 1 end 0 end # whenever the chain doesn't fit the pattern, so the default # poly-typed temp is preserved for everything else. def poly_index_narrow_int(nid) # branch below can unbox a poly arg without re-walking the AST. arg_compiled = "".split(",") arg_types = "".split(",") aargs_for_unbox = [] arg_strs = "" args_id = @nd_arguments[nid] if args_id >= 0 ce = compile_expr(aargs[k]) arg_compiled.push(ce) arg_types.push(infer_type(aargs[k])) aargs_for_unbox.push(aargs[k]) arg_strs = arg_strs + ", " + ce k = k + 1 end # downstream; this only does the boxing direction. arg_v = arg_compiled[pk] arm_pt_base = base_type(arm_ptypes[pk]) # Inverse direction: this arm expects a concrete primitive # but the call site supplied a value whose actual C type # is sp_RbVal (analyze's cached `at` lagged the emit -- see # expr_emits_poly_rb_val). Unbox via the matching union # field. Mirrors compile_expr_for_expected_type's poly→prim. if pk < aargs_for_unbox.length && expr_emits_poly_rb_val(aargs_for_unbox[pk]) == 1 if arm_pt_base == "int" || arm_pt_base == "symbol" @needs_rb_value = 1 arg_v = "(" + arg_v + ").v.i" if arm_pt_base == "symbol" arg_v = "(sp_sym)" + arg_v end elsif arm_pt_base == "string" || arm_pt_base == "mutable_str" @needs_rb_value = 1 arg_v = "(" + arg_v + ").v.s" elsif arm_pt_base == "float" @needs_rb_value = 1 arg_v = "(" + arg_v + ").v.f" elsif arm_pt_base == "bool" @needs_rb_value = 1 arg_v = "(" + arg_v + ").v.b" end end if arm_pt_base == "poly" && pk < arg_types.length && arg_types[pk] != "poly" && arg_types[pk] != "" arg_v = box_value_to_poly(arg_types[pk], arg_v) endtest/nullable_poly_hash_arg_unbox.rbaddedView file
Loading diff…