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:

  1. 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.

  2. 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 after val = 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

@@ -1883,6 +1883,13 @@
18831883 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;}
18841884 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;}
18851885 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;}
1886+/* Convert a narrower StrStrHash to a StrPolyHash. Needed when the
1887+ analyzer widens an LV slot to sp_StrPolyHash* (e.g. later poly-value
1888+ writes) but the initial RHS is a sibling narrower hash variant —
1889+ raw pointer assignment would mix incompatible struct layouts
1890+ (vals[] of const char** vs sp_RbVal*). See issue #614. */
1891+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;}
1892+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;}
18861893
18871894 /* SymPolyHash: symbol keys, sp_RbVal values — same shape as SymStrHash but with poly values. */
18881895 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;