Commit 33694cde

33694cdeee7dMerged

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

Description

fix(analyze,codegen): cross-variant sym_str / sym_poly Hash#merge

Closes #515. Sibling to #510. Both cross-variant directions were broken:

  • sym_str_hash.merge(sym_poly_hash) routed to sp_SymStrHash_merge with a sp_SymPolyHash* argument -- C warned -Wincompatible-pointer-types and the helper read the poly hash's memory through SymStrHash field offsets, producing runtime garbage.

  • sym_poly_hash.merge(sym_str_hash) had no codegen arm and warned "cannot resolve call to 'merge' on sym_poly_hash" before falling through to emit 0.

Two new helpers emitted alongside the existing sym_str_hash runtime helpers (codegen-emit_raw so they can refer to the codegen-generated sp_SymStrHash typedef):

  • sp_SymStrHash_to_sym_poly: promotes a sym_str_hash to a fresh sym_poly_hash by sp_box_str'ing every value.

  • sp_SymPolyHash_merge_str: copies the poly receiver's entries then folds in the str hash's entries with sp_box_str on the value side.

Dispatch updates:

  • sym_str_hash.merge arm: when arg infers as sym_poly_hash, promote recv via sp_SymStrHash_to_sym_poly and merge.
  • sym_poly_hash.merge arm: when arg infers as sym_str_hash, call sp_SymPolyHash_merge_str.

Analyze merge-return inference: returns sym_poly_hash when the recv is sym_str_hash and the arg is sym_poly_hash. The mirror direction already returned the (correct) sym_poly_hash via the existing "return infer_type(recv)" branch.

Local: 487 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
33694cdeee7d
Parent
b4125a5bffa4

spinel_analyze.rbmodifiedView file

@@ -4430,7 +4430,22 @@
44304430 end
44314431 if mname == "merge"
44324432 if recv >= 0
4433- return infer_type(recv)
4433+ rt_merge = infer_type(recv)
4434+ # Cross-variant promote: sym_str_hash.merge(sym_poly_hash)
4435+ # returns a fresh sym_poly_hash with the receiver's str
4436+ # values boxed. Mirror direction (poly recv, str arg)
4437+ # returns the receiver's type. Issue #515.
4438+ if rt_merge == "sym_str_hash"
4439+ args_id_mg = @nd_arguments[nid]
4440+ if args_id_mg >= 0
4441+ arg_ids_mg = get_args(args_id_mg)
4442+ if arg_ids_mg.length >= 1 && infer_type(arg_ids_mg[0]) == "sym_poly_hash"
4443+ @needs_rb_value = 1
4444+ return "sym_poly_hash"
4445+ end
4446+ end
4447+ end
4448+ return rt_merge
44344449 end
44354450 return "str_int_hash"
44364451 end