Commit b7131c5b
b7131c5b593cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · +4 added · ~2 modified
Description
fix(codegen,runtime): wrap narrower-hash dup into *_poly_hash slot
When the analyzer widens an LV slot to sp_StrPolyHash* (e.g. later
poly-value writes), the initial RHS may still be a sibling narrower
hash variant. cdb355e's set_var_type sticky guard keeps the slot's
tracked type at the wider variant so bracket-assigns dispatch
through the right runtime helpers, but the actual LV-write emit
still produced a raw lv = sp_StrStrHash_dup(...) assignment. The
two structs have different vals[] element types
(const char ** vs sp_RbVal *), so subsequent reads through the
widened slot returned garbage (0 / nil) even with -Werror disabled.
The half of #613 that ace0708 left open.
Two pieces:
-
lib/sp_runtime.h: add sp_StrPolyHash_from_str_str_hash and sp_StrPolyHash_from_str_int_hash that allocate a fresh sp_StrPolyHash and
sp_box_{str,int}each value. -
spinel_codegen.rb: a new
widen_hash_assign(val, vt, rhs_t)helper detects (sp_StrPolyHash *, sp_StrStrHash) and (sp_StrPolyHash *, sp_StrIntHash) mismatches and wraps the val expression with the matching converter. Invoked from compile_stmt's LocalVariableWriteNode arm afterval = compile_expr(...)and before the existing poly-box arm. The self-host caveat noted on the issue (an earlier in-place wrap broke the obj-type unbox arm a few lines below) was the #620 parallel-make race, fixed in 94fc6f3.
Tests cover str_str_hash -> str_poly_hash and str_int_hash -> str_poly_hash. Sym-variant siblings are deferred since sp_SymStrHash / sp_SymIntHash are emitted into the C output rather than defined in lib/sp_runtime.h; that's reachable when a real workload hits it.
Fixes #614
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- b7131c5b593c
- Parent
- 9bf8f4862012
lib/sp_runtime.hmodifiedView file
static sp_StrPolyHash*sp_StrPolyHash_merge(sp_StrPolyHash*a,sp_StrPolyHash*b){sp_StrPolyHash*r=sp_StrPolyHash_new();r->default_v=a->default_v;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();r->default_v=h->default_v;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;}static mrb_bool sp_StrPolyHash_eq(sp_StrPolyHash*a,sp_StrPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){const char*k=a->order[i];if(!sp_StrPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_StrPolyHash_get(a,k),sp_StrPolyHash_get(b,k)))return FALSE;}return TRUE;}/* Convert a narrower StrStrHash to a StrPolyHash. Needed when the analyzer widens an LV slot to sp_StrPolyHash* (e.g. later poly-value writes) but the initial RHS is a sibling narrower hash variant — raw pointer assignment would mix incompatible struct layouts (vals[] of const char** vs sp_RbVal*). See issue #614. */static sp_StrPolyHash*sp_StrPolyHash_from_str_str_hash(sp_StrStrHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;if(h->default_v)r->default_v=sp_box_str(h->default_v);for(mrb_int i=0;i<h->len;i++){const char*k=h->order[i];sp_StrPolyHash_set(r,k,sp_box_str(sp_StrStrHash_get(h,k)));}return r;}static sp_StrPolyHash*sp_StrPolyHash_from_str_int_hash(sp_StrIntHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=sp_box_int(h->default_v);for(mrb_int i=0;i<h->len;i++){const char*k=h->order[i];sp_StrPolyHash_set(r,k,sp_box_int(sp_StrIntHash_get(h,k)));}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_RbVal default_v;}sp_SymPolyHash;spinel_codegen.rbmodifiedView file
Loading diff…
test/str_poly_hash_dup_widen.rbaddedView file
Loading diff…
test/str_poly_hash_dup_widen.rb.expectedaddedView file
Loading diff…
test/str_poly_hash_dup_widen_from_int.rbaddedView file
Loading diff…
test/str_poly_hash_dup_widen_from_int.rb.expectedaddedView file
Loading diff…