Commit c6c932f3

c6c932f37a90Merged

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

Description

fix(codegen): Hash#merge on str_poly_hash (#426)

Hash#merge was unresolved when the receiver was a polymorphic- value string-keyed hash ({"a" => 1, "b" => "two"} shape), and the emit produced 0 for the dispatch — which then cascaded through m.length as int and yielded garbage output. The monomorphic siblings (str_int_hash, str_str_hash) already worked; only the str_poly_hash specialization was missing.

Fix:

  • Runtime: sp_StrPolyHash_merge(a, b) in lib/sp_runtime.h, same shape as the existing sp_StrIntHash_merge / sp_SymPolyHash_merge helpers.
  • Codegen: compile_hash_method_expr's str_poly_hash arm gains a merge case that dispatches by arg variant:
    • str_poly_hash arg -> sp_StrPolyHash_merge direct.
    • str_str_hash arg -> inline copy with sp_box_str.
    • str_int_hash arg -> inline copy with sp_box_int.

Out of scope: poly-typed args whose runtime hash variant is unknown statically -- those would need the runtime-dispatch pattern from sym_poly_hash's poly-arg branch, not in this minimal fix.

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

Details

Commit
c6c932f37a90
Parent
0439b816cb41

lib/sp_runtime.hmodifiedView file

@@ -1173,6 +1173,10 @@
11731173 static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}
11741174 static sp_StrArray*sp_StrPolyHash_keys(sp_StrPolyHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}
11751175 static sp_PolyArray*sp_StrPolyHash_values(sp_StrPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_StrPolyHash_get(h,h->order[i]));return a;}
1176+/* Issue #426: Hash#merge for str_poly_hash. Same shape as the
1177+ StrIntHash / SymPolyHash siblings -- copy recv's entries into a
1178+ fresh hash, then overlay other's. */
1179+static sp_StrPolyHash*sp_StrPolyHash_merge(sp_StrPolyHash*a,sp_StrPolyHash*b){sp_StrPolyHash*r=sp_StrPolyHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrPolyHash_set(r,a->order[i],sp_StrPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_StrPolyHash_set(r,b->order[i],sp_StrPolyHash_get(b,b->order[i]));return r;}
11761180
11771181 /* SymPolyHash: symbol keys, sp_RbVal values — same shape as SymStrHash but with poly values. */
11781182 typedef struct{sp_sym*keys;sp_RbVal*vals;sp_sym*order;mrb_int len;mrb_int cap;mrb_int mask;}sp_SymPolyHash;