Commit 7a3f615d
7a3f615d994fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix(analyze,codegen): Hash#merge across hash variants widens to poly (#661)
The typed Hash#merge codegen used the receiver's prefix
unconditionally for both args (sp_SymIntHash_merge(h1, h2) where
h2 was sp_SymStrHash *), failing -Werror with an incompatible
pointer type.
Fix routes any pair of sym-keyed variants (or str-keyed variants) with different value types through the *PolyHash family:
analyze: infer_call_type for merge returns sym_poly_hash / str_poly_hash
when the recv and arg disagree on value type. The corresponding LV
slot then carries the wider type through.
codegen:
- sym_int_hash recv + sym_str_hash arg -> SymPolyHash via sp_SymPolyHash_merge(sp_SymIntHash_to_sym_poly(recv), sp_SymStrHash_to_sym_poly(arg))
- sym_str_hash recv + sym_int_hash arg -> same shape, args swapped.
- sym_int/str recv + sym_poly arg -> merge against the existing sym_poly arg directly, after converting recv via *_to_sym_poly.
- str_int_hash recv + str_str_hash / str_poly_hash arg -> mirror via sp_StrPolyHash_from_str_int_hash / _from_str_str_hash.
Added the missing converter sp_SymIntHash_to_sym_poly (sibling of the pre-existing sp_SymStrHash_to_sym_poly) and emit it alongside sp_SymPolyHash_merge_int's startup block.
Closes #661.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 7a3f615d994f
- Parent
- b69ff9f545fd
spinel_analyze.rbmodifiedView file
if mname == "merge" if recv >= 0 rt_merge = infer_type(recv) # Cross-variant promote: sym_str_hash.merge(sym_poly_hash) # returns a fresh sym_poly_hash with the receiver's str # values boxed. Mirror direction (poly recv, str arg) # returns the receiver's type. Issue #515. if rt_merge == "sym_str_hash" # Cross-variant promote: when recv and arg are both sym-keyed # hash variants but with different value types, the merge returns # a fresh sym_poly_hash (boxing each side's values). Issue #515 # covered the str_hash<>poly_hash direction; #661 extends to # int_hash <-> str_hash and int_hash <-> poly_hash. if rt_merge == "sym_int_hash" || rt_merge == "sym_str_hash" || rt_merge == "sym_poly_hash" args_id_mg = @nd_arguments[nid] if args_id_mg >= 0 arg_ids_mg = get_args(args_id_mg) if arg_ids_mg.length >= 1 && infer_type(arg_ids_mg[0]) == "sym_poly_hash" @needs_rb_value = 1 return "sym_poly_hash" if arg_ids_mg.length >= 1 arg_t_mg = infer_type(arg_ids_mg[0]) if (arg_t_mg == "sym_int_hash" || arg_t_mg == "sym_str_hash" || arg_t_mg == "sym_poly_hash") && arg_t_mg != rt_merge @needs_rb_value = 1 return "sym_poly_hash" end end end end # Same shape for str-keyed hashes. The corresponding # StrPolyHash converters already exist in sp_runtime.h # (sp_StrPolyHash_from_str_int_hash / _from_str_str_hash) and # sp_codegen.rb's emit raws sp_StrPolyHash_merge below. if rt_merge == "str_int_hash" || rt_merge == "str_str_hash" || rt_merge == "str_poly_hash" args_id_mg2 = @nd_arguments[nid] if args_id_mg2 >= 0 arg_ids_mg2 = get_args(args_id_mg2) if arg_ids_mg2.length >= 1 arg_t_mg2 = infer_type(arg_ids_mg2[0]) if (arg_t_mg2 == "str_int_hash" || arg_t_mg2 == "str_str_hash" || arg_t_mg2 == "str_poly_hash") && arg_t_mg2 != rt_merge @needs_rb_value = 1 return "str_poly_hash" end end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/hash_merge_cross_variant.rbaddedView file
Loading diff…