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 existingsp_StrIntHash_merge/sp_SymPolyHash_mergehelpers. - Codegen:
compile_hash_method_expr's str_poly_hash arm gains amergecase 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
static mrb_int sp_StrPolyHash_length(sp_StrPolyHash*h){return h->len;}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;}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;}/* Issue #426: Hash#merge for str_poly_hash. Same shape as the StrIntHash / SymPolyHash siblings -- copy recv's entries into a fresh hash, then overlay other's. */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;}/* SymPolyHash: symbol keys, sp_RbVal values — same shape as SymStrHash but with poly values. */typedef struct{sp_sym*keys;sp_RbVal*vals;sp_sym*order;mrb_int len;mrb_int cap;mrb_int mask;}sp_SymPolyHash;spinel_codegen.rbmodifiedView file
Loading diff…
test/str_poly_hash_merge.rbaddedView file
Loading diff…
test/str_poly_hash_merge.rb.expectedaddedView file
Loading diff…