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 at to "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

@@ -5048,6 +5048,13 @@
50485048 end
50495049 val = compile_expr(nid)
50505050 at = infer_type(nid)
5051+ # Override `at` to "poly" when the nid's actual C emit is
5052+ # sp_RbVal even though the cached static type was a narrower
5053+ # default (the nullable-poly_hash `[]` shape — see
5054+ # expr_emits_poly_rb_val for the failure mode).
5055+ if at != "poly" && expr_emits_poly_rb_val(nid) == 1
5056+ at = "poly"
5057+ end
50515058 # `if v.is_a?(String); helper(v); end` (and analogues for int /
50525059 # float / sym / obj) — `v`'s C storage is sp_RbVal but the
50535060 # callee's param expects an unboxed concrete type. The user-level
@@ -17616,6 +17623,30 @@
1761617623 "poly"
1761717624 end
1761817625
17626+ # `nullable_poly_hash[k]` — recv's static type carries `?` (a
17627+ # `if recv.nil?; return; end` upstream doesn't strip the suffix),
17628+ # so analyze's [] dispatch falls through to the int default in
17629+ # its cache. The actual emit is sp_<*PolyHash>_get(...) returning
17630+ # sp_RbVal, so the cached "int" diverges from the C temp type and
17631+ # the caller's int-typed param then receives an sp_RbVal unboxed.
17632+ # Detect the shape here so compile_expr_for_expected_type and the
17633+ # poly-dispatch arm can do the unbox even when cached "at" lied.
17634+ def expr_emits_poly_rb_val(nid)
17635+ if @nd_type[nid] != "CallNode" || @nd_name[nid] != "[]"
17636+ return 0
17637+ end
17638+ recv_id = @nd_receiver[nid]
17639+ if recv_id < 0
17640+ return 0
17641+ end
17642+ rt = infer_type(recv_id)
17643+ bt = base_type(rt)
17644+ if bt == "sym_poly_hash" || bt == "str_poly_hash" || bt == "poly_poly_hash"
17645+ return 1
17646+ end
17647+ 0
17648+ end
17649+
1761917650 # whenever the chain doesn't fit the pattern, so the default
1762017651 # poly-typed temp is preserved for everything else.
1762117652 def poly_index_narrow_int(nid)
@@ -18035,6 +18066,7 @@
1803518066 # branch below can unbox a poly arg without re-walking the AST.
1803618067 arg_compiled = "".split(",")
1803718068 arg_types = "".split(",")
18069+ aargs_for_unbox = []
1803818070 arg_strs = ""
1803918071 args_id = @nd_arguments[nid]
1804018072 if args_id >= 0
@@ -18044,6 +18076,7 @@
1804418076 ce = compile_expr(aargs[k])
1804518077 arg_compiled.push(ce)
1804618078 arg_types.push(infer_type(aargs[k]))
18079+ aargs_for_unbox.push(aargs[k])
1804718080 arg_strs = arg_strs + ", " + ce
1804818081 k = k + 1
1804918082 end
@@ -18125,6 +18158,29 @@
1812518158 # downstream; this only does the boxing direction.
1812618159 arg_v = arg_compiled[pk]
1812718160 arm_pt_base = base_type(arm_ptypes[pk])
18161+ # Inverse direction: this arm expects a concrete primitive
18162+ # but the call site supplied a value whose actual C type
18163+ # is sp_RbVal (analyze's cached `at` lagged the emit -- see
18164+ # expr_emits_poly_rb_val). Unbox via the matching union
18165+ # field. Mirrors compile_expr_for_expected_type's poly→prim.
18166+ if pk < aargs_for_unbox.length && expr_emits_poly_rb_val(aargs_for_unbox[pk]) == 1
18167+ if arm_pt_base == "int" || arm_pt_base == "symbol"
18168+ @needs_rb_value = 1
18169+ arg_v = "(" + arg_v + ").v.i"
18170+ if arm_pt_base == "symbol"
18171+ arg_v = "(sp_sym)" + arg_v
18172+ end
18173+ elsif arm_pt_base == "string" || arm_pt_base == "mutable_str"
18174+ @needs_rb_value = 1
18175+ arg_v = "(" + arg_v + ").v.s"
18176+ elsif arm_pt_base == "float"
18177+ @needs_rb_value = 1
18178+ arg_v = "(" + arg_v + ").v.f"
18179+ elsif arm_pt_base == "bool"
18180+ @needs_rb_value = 1
18181+ arg_v = "(" + arg_v + ").v.b"
18182+ end
18183+ end
1812818184 if arm_pt_base == "poly" && pk < arg_types.length && arg_types[pk] != "poly" && arg_types[pk] != ""
1812918185 arg_v = box_value_to_poly(arg_types[pk], arg_v)
1813018186 end