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

@@ -4238,6 +4238,22 @@
42384238 0
42394239 end
42404240
4241+ def is_hash_type(t)
4242+ if is_nullable_type(t) == 1
4243+ t = base_type(t)
4244+ end
4245+ if t == "str_int_hash" || t == "str_str_hash" || t == "int_str_hash"
4246+ return 1
4247+ end
4248+ if t == "sym_int_hash" || t == "sym_str_hash"
4249+ return 1
4250+ end
4251+ if t == "str_poly_hash" || t == "sym_poly_hash"
4252+ return 1
4253+ end
4254+ 0
4255+ end
4256+
42414257 # Set the right @needs_<runtime> flag for the given array type.
42424258 def mark_array_runtime_needs(t)
42434259 if t == "float_array"
@@ -6689,6 +6705,30 @@
66896705 @needs_rb_value = 1
66906706 return "poly_array"
66916707 end
6708+ # Hash variants: empty-default str_int_hash (from `attrs = {}`)
6709+ # is type-flexible — let the call-site type win, same as the
6710+ # int_array → typed-array escalation above. Issue #176. The
6711+ # poly variants subsume any other hash; mismatched concrete
6712+ # hash types (str_int_hash vs sym_str_hash) fall through to
6713+ # the "incompatible → poly" tail.
6714+ if is_hash_type(old_pt) == 1 && is_hash_type(at) == 1
6715+ if old_pt == "str_poly_hash" || at == "str_poly_hash"
6716+ @needs_str_poly_hash = 1
6717+ @needs_rb_value = 1
6718+ return "str_poly_hash"
6719+ end
6720+ if old_pt == "sym_poly_hash" || at == "sym_poly_hash"
6721+ @needs_sym_poly_hash = 1
6722+ @needs_rb_value = 1
6723+ return "sym_poly_hash"
6724+ end
6725+ if old_pt == "str_int_hash"
6726+ return at
6727+ end
6728+ if at == "str_int_hash"
6729+ return old_pt
6730+ end
6731+ end
66926732 if at == "int"
66936733 # Numeric compat: int + float is safe in both directions.
66946734 if old_pt == "float"