Commit 3c118c20
3c118c2093fcMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): empty-Hash default param adopts caller's hash type
def m(attrs = {}); attrs[:x]; end followed by m({x: 42}) used
to silently print 0. The param's empty-{} default infers as
str_int_hash; the caller's symbol-keyed hash infers as
sym_int_hash. unify_call_types treated the two as 'genuinely
incompatible' and widened the param to poly. The body's attrs[:x]
then went through the poly built-in dispatch, which only knows the
five built-in array shapes — no hash arm — and silently returned
sp_box_nil(), so reads always saw 0/nil.
Add a hash-variant escalation branch to unify_call_types, mirroring
the int_array → typed-array rule already there: when both sides are
hash types, str_int_hash (the empty-default shape) yields to the
other side's concrete shape; *_poly_hash subsumes anything else;
mismatched concrete hash variants still fall through to the poly
tail.
Closes #176
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 3c118c2093fc
- Parent
- e9810bca9387
spinel_codegen.rbmodifiedView file
0 end def is_hash_type(t) if is_nullable_type(t) == 1 t = base_type(t) end if t == "str_int_hash" || t == "str_str_hash" || t == "int_str_hash" return 1 end if t == "sym_int_hash" || t == "sym_str_hash" return 1 end if t == "str_poly_hash" || t == "sym_poly_hash" return 1 end 0 end # Set the right @needs_<runtime> flag for the given array type. def mark_array_runtime_needs(t) if t == "float_array" @needs_rb_value = 1 return "poly_array" end # Hash variants: empty-default str_int_hash (from `attrs = {}`) # is type-flexible — let the call-site type win, same as the # int_array → typed-array escalation above. Issue #176. The # poly variants subsume any other hash; mismatched concrete # hash types (str_int_hash vs sym_str_hash) fall through to # the "incompatible → poly" tail. if is_hash_type(old_pt) == 1 && is_hash_type(at) == 1 if old_pt == "str_poly_hash" || at == "str_poly_hash" @needs_str_poly_hash = 1 @needs_rb_value = 1 return "str_poly_hash" end if old_pt == "sym_poly_hash" || at == "sym_poly_hash" @needs_sym_poly_hash = 1 @needs_rb_value = 1 return "sym_poly_hash" end if old_pt == "str_int_hash" return at end if at == "str_int_hash" return old_pt end end if at == "int" # Numeric compat: int + float is safe in both directions. if old_pt == "float"test/issue176_empty_hash_default.rbaddedView file
Loading diff…