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

@@ -4843,17 +4843,38 @@
48434843 if mname == "merge"
48444844 if recv >= 0
48454845 rt_merge = infer_type(recv)
4846- # Cross-variant promote: sym_str_hash.merge(sym_poly_hash)
4847- # returns a fresh sym_poly_hash with the receiver's str
4848- # values boxed. Mirror direction (poly recv, str arg)
4849- # returns the receiver's type. Issue #515.
4850- if rt_merge == "sym_str_hash"
4846+ # Cross-variant promote: when recv and arg are both sym-keyed
4847+ # hash variants but with different value types, the merge returns
4848+ # a fresh sym_poly_hash (boxing each side's values). Issue #515
4849+ # covered the str_hash<>poly_hash direction; #661 extends to
4850+ # int_hash <-> str_hash and int_hash <-> poly_hash.
4851+ if rt_merge == "sym_int_hash" || rt_merge == "sym_str_hash" || rt_merge == "sym_poly_hash"
48514852 args_id_mg = @nd_arguments[nid]
48524853 if args_id_mg >= 0
48534854 arg_ids_mg = get_args(args_id_mg)
4854- if arg_ids_mg.length >= 1 && infer_type(arg_ids_mg[0]) == "sym_poly_hash"
4855- @needs_rb_value = 1
4856- return "sym_poly_hash"
4855+ if arg_ids_mg.length >= 1
4856+ arg_t_mg = infer_type(arg_ids_mg[0])
4857+ if (arg_t_mg == "sym_int_hash" || arg_t_mg == "sym_str_hash" || arg_t_mg == "sym_poly_hash") && arg_t_mg != rt_merge
4858+ @needs_rb_value = 1
4859+ return "sym_poly_hash"
4860+ end
4861+ end
4862+ end
4863+ end
4864+ # Same shape for str-keyed hashes. The corresponding
4865+ # StrPolyHash converters already exist in sp_runtime.h
4866+ # (sp_StrPolyHash_from_str_int_hash / _from_str_str_hash) and
4867+ # sp_codegen.rb's emit raws sp_StrPolyHash_merge below.
4868+ if rt_merge == "str_int_hash" || rt_merge == "str_str_hash" || rt_merge == "str_poly_hash"
4869+ args_id_mg2 = @nd_arguments[nid]
4870+ if args_id_mg2 >= 0
4871+ arg_ids_mg2 = get_args(args_id_mg2)
4872+ if arg_ids_mg2.length >= 1
4873+ arg_t_mg2 = infer_type(arg_ids_mg2[0])
4874+ if (arg_t_mg2 == "str_int_hash" || arg_t_mg2 == "str_str_hash" || arg_t_mg2 == "str_poly_hash") && arg_t_mg2 != rt_merge
4875+ @needs_rb_value = 1
4876+ return "str_poly_hash"
4877+ end
48574878 end
48584879 end
48594880 end