Commit d56ba8b5

d56ba8b5dcc0Merged

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

Description

fix(codegen): is_a?(Hash) narrow unboxes to typed-Hash param at call site

compile_typed_call_args dispatched poly args through compile_expr_for_expected_type only when the param's base type was poly/string/array or obj (with poly arg). Hash-typed params fell to the bare compile_expr path and emitted the raw sp_RbVal, which gcc rejected with "passing sp_RbVal to parameter of incompatible type sp_StrStrHash *" (a hard error, not a -Wincompatible-pointer-types warning). Same story for typed-array params.

Extend the dispatch condition so at == "poly" + hash-typed or array-typed param routes through the unbox path, and add the matching hash/array arms in compile_expr_for_expected_type that extract the sp_RbVal's .v.p and cast to the expected pointer type. Same trust model as the obj_ arm: the runtime is_a? guard establishes that the value is some hash/array; we trust the variant matches the callee.

Fixes #631.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
d56ba8b5dcc0
Parent
d7a62e512691

spinel_codegen.rbmodifiedView file

@@ -5484,6 +5484,24 @@
54845484 cname = expected_base[4, expected_base.length - 4]
54855485 return "((sp_" + cname + " *)(" + val + ").v.p)"
54865486 end
5487+ # `if v.is_a?(Hash); helper(v); end` where `helper`'s param has
5488+ # been monomorphized to a concrete hash variant (str_str_hash,
5489+ # sym_int_hash, ...). Same unbox shape as the obj_ arm: extract
5490+ # the sp_RbVal's `.v.p` and cast to the expected hash pointer
5491+ # type. The runtime is_a?(Hash) guard ensures the cls_id is
5492+ # some hash; trusting that the variant matches the callee mirrors
5493+ # how the obj_ arm trusts is_a?(C). Issue #631 (sibling to #448).
5494+ if is_hash_type(expected_base) == 1
5495+ @needs_rb_value = 1
5496+ return "((" + c_type(expected_base) + ")(" + val + ").v.p)"
5497+ end
5498+ # Same shape for an is_a?(Array)-narrowed slot landing in a
5499+ # concrete typed-array param (int_array, str_array, poly_array,
5500+ # any `*_ptr_array`).
5501+ if is_array_type(expected_base) == 1
5502+ @needs_rb_value = 1
5503+ return "((" + c_type(expected_base) + ")(" + val + ").v.p)"
5504+ end
54875505 end
54885506 if expected_base == "poly" && at != "poly"
54895507 return box_value_to_poly(at, val)
@@ -23588,7 +23606,7 @@
2358823606 if k < ptypes.length
2358923607 pt = ptypes[k]
2359023608 pt_base = base_type(pt)
23591- if pt_base == "poly" || pt_base == "string" || is_array_type(pt_base) == 1 || (at == "poly" && is_obj_type(pt_base) == 1)
23609+ if pt_base == "poly" || pt_base == "string" || is_array_type(pt_base) == 1 || (at == "poly" && is_obj_type(pt_base) == 1) || (at == "poly" && is_hash_type(pt_base) == 1)
2359223610 aexpr = compile_expr_for_expected_type(arg_ids[k], pt)
2359323611 if root_constructor_args == 1
2359423612 aexpr = root_constructor_arg_if_needed(arg_ids[k], aexpr, pt, later_arg_may_gc[k])