Commit bb1ba86f
bb1ba86ff3d4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +1 added · ~3 modified
Description
fix: poly-recv Hash#dup dispatch + LV typing
`matched[:path_params].dup` shape: outer is sym_poly_hash?, the `[]` access returns sp_RbVal, and `.dup` on the resulting poly used to fall through to int default ("cannot resolve call to 'dup' on int"). The downstream `iv_params = lv_merged` writes against typed StrIntHash * slots then warned 8x under -Wint-conversion as part of the same chain.
Three changes land together:
-
runtime helpers (lib/sp_runtime.h): add `sp_*Hash_dup` for each always-shipped hash variant — StrIntHash, StrStrHash, IntStrHash, StrPolyHash, SymPolyHash, PolyPolyHash. Each walks order[] and re-sets entries into a fresh hash, same pattern as the existing _merge helpers.
Sym-keyed gated variants (SymIntHash, SymStrHash) get their _dup emitted alongside the existing _new/_set/get in the emit_sym*_hash_runtime block in codegen.
-
codegen poly dispatcher (compile_poly_method_call): new `dup` arm in emit_poly_builtin_dispatch that picks the matching sp_*Hash_dup based on runtime cls_id and boxes the result back as obj. Only emits arms for the always-in hash variants; sym-keyed variants left as a follow-up because their typedefs are pre-scan-gated by @needs flags.
-
analyze + codegen poly_dispatch_return_type: `dup`, `each`, `to_h`, `merge` now return "poly" instead of falling through to the "int" default. The result_tmp in the poly dispatcher's emit was previously typed mrb_int, swallowing the per-cls_id arm's sp_box_obj assignment.
-
analyze infer_call_type for `dup`: recognize the `nullable_poly_hash[k].dup` shape and surface "poly" so the receiving LV slot widens up front. Without this, the caller-side LV stays int and the dispatched sp_RbVal doesn't fit in the slot.
-
compile_call_expr: when the recv expression `expr_emits_poly_ rb_val(recv) == 1` (existing helper) but the cached recv_type is narrower (typically int default), override recv_type to "poly" so dispatch reaches compile_poly_method_call instead of compile_int_method_expr.
Archive (i450) warnings: 46 → 36 (10 cleared). Full test suite 418/0/0. Regression test (test/poly_recv_hash_dup.rb) pins the dup chain end-to-end.
Refs #450
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- bb1ba86ff3d4
- Parent
- d5b20a0db9db
lib/sp_runtime.hmodifiedView file
static sp_StrIntHash*sp_StrArray_tally(sp_StrArray*a){sp_StrIntHash*h=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++){const char*k=a->data[i];mrb_int c=sp_StrIntHash_has_key(h,k)?sp_StrIntHash_get(h,k):0;sp_StrIntHash_set(h,k,c+1);}return h;}static sp_StrIntHash*sp_StrIntHash_merge(sp_StrIntHash*a,sp_StrIntHash*b){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrIntHash_set(r,a->order[i],sp_StrIntHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(r,b->order[i],sp_StrIntHash_get(b,b->order[i]));return r;}static void sp_StrIntHash_update(sp_StrIntHash*a,sp_StrIntHash*b){for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(a,b->order[i],sp_StrIntHash_get(b,b->order[i]));}static sp_StrIntHash*sp_StrIntHash_dup(sp_StrIntHash*h){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<h->len;i++)sp_StrIntHash_set(r,h->order[i],sp_StrIntHash_get(h,h->order[i]));return r;}typedef struct{const char**keys;const char**vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrStrHash;static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}static sp_StrArray*sp_StrStrHash_values(sp_StrStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,sp_StrStrHash_get(h,h->order[i]));return a;}static sp_StrStrHash*sp_StrStrHash_invert(sp_StrStrHash*h){sp_StrStrHash*r=sp_StrStrHash_new();for(mrb_int i=0;i<h->len;i++){const char*k=h->order[i];sp_StrStrHash_set(r,sp_StrStrHash_get(h,k),k);}return r;}static void sp_StrStrHash_update(sp_StrStrHash*a,sp_StrStrHash*b){for(mrb_int i=0;i<b->len;i++)sp_StrStrHash_set(a,b->order[i],sp_StrStrHash_get(b,b->order[i]));}static sp_StrStrHash*sp_StrStrHash_dup(sp_StrStrHash*h){sp_StrStrHash*r=sp_StrStrHash_new();for(mrb_int i=0;i<h->len;i++)sp_StrStrHash_set(r,h->order[i],sp_StrStrHash_get(h,h->order[i]));return r;}typedef struct{mrb_int*keys;const char**vals;mrb_int*order;mrb_bool*used;mrb_int len;mrb_int cap;mrb_int mask;}sp_IntStrHash;static void sp_IntStrHash_fin(void*p){sp_IntStrHash*h=(sp_IntStrHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->used);}static mrb_int sp_IntStrHash_length(sp_IntStrHash*h){return h->len;}static sp_IntArray*sp_IntStrHash_keys(sp_IntStrHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,h->order[i]);return a;}static sp_StrArray*sp_IntStrHash_values(sp_IntStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,sp_IntStrHash_get(h,h->order[i]));return a;}static sp_IntStrHash*sp_IntStrHash_dup(sp_IntStrHash*h){sp_IntStrHash*r=sp_IntStrHash_new();for(mrb_int i=0;i<h->len;i++)sp_IntStrHash_set(r,h->order[i],sp_IntStrHash_get(h,h->order[i]));return r;}/* Reuse an existing StrArray for split, avoiding GC alloc. Clears a->len and refills. Substring strings are still malloc'd. */ 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;}static sp_StrPolyHash*sp_StrPolyHash_dup(sp_StrPolyHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_StrPolyHash_set(r,h->order[i],sp_StrPolyHash_get(h,h->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;static sp_IntArray*sp_SymPolyHash_keys(sp_SymPolyHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,(mrb_int)h->order[i]);return a;}static sp_PolyArray*sp_SymPolyHash_values(sp_SymPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,sp_SymPolyHash_get(h,h->order[i]));return a;}static sp_SymPolyHash*sp_SymPolyHash_merge(sp_SymPolyHash*a,sp_SymPolyHash*b){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<a->len;i++)sp_SymPolyHash_set(r,a->order[i],sp_SymPolyHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_SymPolyHash_set(r,b->order[i],sp_SymPolyHash_get(b,b->order[i]));return r;}static sp_SymPolyHash*sp_SymPolyHash_dup(sp_SymPolyHash*h){sp_SymPolyHash*r=sp_SymPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_SymPolyHash_get(h,h->order[i]));return r;}/* PolyPolyHash: heterogeneous keys + values (both sp_RbVal). For primitives the hash/eql is tag-based (value equality); for OBJ tagstatic mrb_int sp_PolyPolyHash_length(sp_PolyPolyHash*h){return h->len;}static sp_PolyArray*sp_PolyPolyHash_keys(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->keys[h->order[i]]);return a;}static sp_PolyArray*sp_PolyPolyHash_values(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->vals[h->order[i]]);return a;}static sp_PolyPolyHash*sp_PolyPolyHash_dup(sp_PolyPolyHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,h->keys[h->order[i]],h->vals[h->order[i]]);return r;}#include <setjmp.h>#define SP_EXC_STACK_MAX 64spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/poly_recv_hash_dup.rbaddedView file
Loading diff…