Commit 951f34f4

951f34f4ccfdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): unbox poly arg passed to typed-param top-level method

Extends the #438 unbox pattern from IntStrHash-key lookups to user method-call arguments. When a top-level method's positional param is typed (e.g. def helper(s); s.length; end → const char * lv_s) and the caller passes a poly value (typically a local narrowed by an is_a?(String) guard from an outer poly param), the call site now unboxes via the matching union field rather than passing the raw sp_RbVal.

Two coordinated changes:

  1. compile_call_args_with_defaults (top-level method call): replaces the unconditional compile_expr(positional_ids[k]) fall- through with compile_expr_for_expected_type(..., ptypes[k]) when the param type is known. Mirrors what compile_cls_meth_args_with_defaults already does for class methods.

  2. compile_expr_for_expected_type: new arm for at == "poly" && expected_base != "poly". Emits the matching union-field unbox for string / mutable_str / int / float / bool / symbol, and (sp_<C> *)(val).v.p for obj_ classes. The poly → poly case still falls through to the existing box_value_to_poly path so call sites that legitimately want a sp_RbVal are unaffected.

The unbox trusts the user's is_a? guard for runtime tag correctness, same as #438. Without a guard the C compile still passes (union pun is defined), the lookup just reads a meaningless field.

Tests: +1 regression test (test/poly_arg_to_typed_method_param.rb) covering String and Integer narrows on top-level def. All 402 pass, bench 52, optcarrot checksum 59662.

Refs #448

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
951f34f4ccfd
Parent
bc7388719fe6

spinel_codegen.rbmodifiedView file

@@ -1432,6 +1432,21 @@
14321432 ""
14331433 end
14341434
1435+ # Like find_var_type but bypasses the is_a? narrow stack — returns
1436+ # the var's declared (C-level) type. Used by call-site arg lowering
1437+ # to detect cases where the narrow says "string" but the underlying
1438+ # storage is still sp_RbVal, so the arg needs an unbox cast.
1439+ def find_var_declared_type(name)
1440+ i = @scope_names.length - 1
1441+ while i >= 0
1442+ if @scope_names[i] == name
1443+ return @scope_types[i]
1444+ end
1445+ i = i - 1
1446+ end
1447+ ""
1448+ end
1449+
14351450 # ---- is_a? type narrowing ----
14361451 # `<expr>.is_a?(<Class>)` (or kind_of?) used as the predicate of
14371452 # an if / ternary lets us treat <expr> as <Class>'s type inside
@@ -5033,6 +5048,42 @@
50335048 end
50345049 val = compile_expr(nid)
50355050 at = infer_type(nid)
5051+ # `if v.is_a?(String); helper(v); end` (and analogues for int /
5052+ # float / sym / obj) — `v`'s C storage is sp_RbVal but the
5053+ # callee's param expects an unboxed concrete type. The user-level
5054+ # is_a? guard establishes the tag at runtime; we trust it and
5055+ # unbox via the matching union field. Without an explicit guard
5056+ # the C compiler still type-checks (the unbox is a memory pun);
5057+ # the same trust extends from the #438 unbox-on-IntStrHash-key
5058+ # pattern. Skip the unbox when the expected slot is itself poly
5059+ # — that path already widens via box_value_to_poly below.
5060+ if at == "poly" && expected_base != "poly"
5061+ if expected_base == "string" || expected_base == "mutable_str"
5062+ @needs_rb_value = 1
5063+ return "(" + val + ").v.s"
5064+ end
5065+ if expected_base == "int"
5066+ @needs_rb_value = 1
5067+ return "(" + val + ").v.i"
5068+ end
5069+ if expected_base == "float"
5070+ @needs_rb_value = 1
5071+ return "(" + val + ").v.f"
5072+ end
5073+ if expected_base == "bool"
5074+ @needs_rb_value = 1
5075+ return "(" + val + ").v.b"
5076+ end
5077+ if expected_base == "symbol"
5078+ @needs_rb_value = 1
5079+ return "(" + val + ").v.sym"
5080+ end
5081+ if is_obj_type(expected_base) == 1
5082+ @needs_rb_value = 1
5083+ cname = expected_base[4, expected_base.length - 4]
5084+ return "((sp_" + cname + " *)(" + val + ").v.p)"
5085+ end
5086+ end
50365087 if expected_base == "poly" && at != "poly"
50375088 return box_value_to_poly(at, val)
50385089 end
@@ -19460,7 +19511,16 @@
1946019511 end
1946119512 end
1946219513 end
19463- result = result + compile_expr(positional_ids[k])
19514+ if k < ptypes.length
19515+ # Route through compile_expr_for_expected_type so a poly arg
19516+ # passed to a typed param (after an is_a? guard, or whenever
19517+ # the caller is more general than the callee) is unboxed via
19518+ # the matching union field. Mirrors the #438 IntStrHash-key
19519+ # unbox and extends it to user-method args.
19520+ result = result + compile_expr_for_expected_type(positional_ids[k], ptypes[k])
19521+ else
19522+ result = result + compile_expr(positional_ids[k])
19523+ end
1946419524 else
1946519525 # Use default value
1946619526 if k < defaults.length