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:
-
compile_call_args_with_defaults (top-level method call): replaces the unconditional
compile_expr(positional_ids[k])fall- through withcompile_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. -
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.pfor 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
"" end # Like find_var_type but bypasses the is_a? narrow stack — returns # the var's declared (C-level) type. Used by call-site arg lowering # to detect cases where the narrow says "string" but the underlying # storage is still sp_RbVal, so the arg needs an unbox cast. def find_var_declared_type(name) i = @scope_names.length - 1 while i >= 0 if @scope_names[i] == name return @scope_types[i] end i = i - 1 end "" end # ---- is_a? type narrowing ---- # `<expr>.is_a?(<Class>)` (or kind_of?) used as the predicate of # an if / ternary lets us treat <expr> as <Class>'s type inside end val = compile_expr(nid) at = infer_type(nid) # `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 # is_a? guard establishes the tag at runtime; we trust it and # unbox via the matching union field. Without an explicit guard # the C compiler still type-checks (the unbox is a memory pun); # the same trust extends from the #438 unbox-on-IntStrHash-key # pattern. Skip the unbox when the expected slot is itself poly # — that path already widens via box_value_to_poly below. if at == "poly" && expected_base != "poly" if expected_base == "string" || expected_base == "mutable_str" @needs_rb_value = 1 return "(" + val + ").v.s" end if expected_base == "int" @needs_rb_value = 1 return "(" + val + ").v.i" end if expected_base == "float" @needs_rb_value = 1 return "(" + val + ").v.f" end if expected_base == "bool" @needs_rb_value = 1 return "(" + val + ").v.b" end if expected_base == "symbol" @needs_rb_value = 1 return "(" + val + ").v.sym" end if is_obj_type(expected_base) == 1 @needs_rb_value = 1 cname = expected_base[4, expected_base.length - 4] return "((sp_" + cname + " *)(" + val + ").v.p)" end end if expected_base == "poly" && at != "poly" return box_value_to_poly(at, val) end end end end result = result + compile_expr(positional_ids[k]) if k < ptypes.length # Route through compile_expr_for_expected_type so a poly arg # passed to a typed param (after an is_a? guard, or whenever # the caller is more general than the callee) is unboxed via # the matching union field. Mirrors the #438 IntStrHash-key # unbox and extends it to user-method args. result = result + compile_expr_for_expected_type(positional_ids[k], ptypes[k]) else result = result + compile_expr(positional_ids[k]) end else # Use default value if k < defaults.lengthtest/poly_arg_to_typed_method_param.rbaddedView file
Loading diff…
test/poly_arg_to_typed_method_param.rb.expectedaddedView file
Loading diff…