Commit ee61faad
ee61faad91f1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen,runtime): cross-variant hash conversion at call boundary
When a sym-keyed hash flowed into a str_poly_hash callee param, clang errored with -Wincompatible-pointer-types: the struct layouts diverge (sym keys are sp_sym, str keys are const char *), so the raw cast was a quiet semantic bug.
- runtime: gated emit of sp_StrPolyHash_from_sym_int_hash / _from_sym_str_hash / _from_sym_poly_hash. The helpers render sym keys via sp_sym_to_s and box int/str values to sp_RbVal so the destination str_poly_hash holds them as poly values.
- codegen: new hash_variant_convert helper centralises the conversion choice; compile_expr_for_expected_type's hash-to-hash cross-variant arm routes through it.
Clears 3 of the 4 sp_ActionView_ViewHelpers_cls_render_attrs warns in #685's roundhouse emit. The remaining cross-variant warns (sp_SymIntHash_merge / sp_SymStrHash_merge with sp_StrIntHash arg) need the merge call-site to detect the cross-key-shape and route through sp_StrPolyHash_merge; deferred to a follow-up commit.
Tests: default 460/0/0, promote 460/0/0, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- ee61faad91f1
- Parent
- 5465db8ad48d
spinel_codegen.rbmodifiedView file
# `sp_StrPolyHash *` slot — different struct layouts (vals[] of # `const char **` vs `sp_RbVal *`), so reads through the slot return # garbage even when -Werror is off. Issue #614. # Cross-variant hash converter at call boundary. Returns the C # expression for promoting `src_var` (typed `src_t`) into a hash # of `dst_t`, or "" when no converter is available. Currently # covers conversions into str_poly_hash from the four source # variants: str_int_hash, str_str_hash, sym_int_hash, sym_str_hash, # sym_poly_hash. Symmetric directions (into sym_poly_hash from # str-keyed variants) are absent because spinel's str_poly_hash # is the canonical wide-keyed shape. def hash_variant_convert(src_t, dst_t, src_var) if dst_t == "str_poly_hash" if src_t == "str_int_hash" @needs_str_poly_hash = 1 @needs_str_int_hash = 1 @needs_rb_value = 1 return "sp_StrPolyHash_from_str_int_hash(" + src_var + ")" end if src_t == "str_str_hash" @needs_str_poly_hash = 1 @needs_str_str_hash = 1 @needs_rb_value = 1 return "sp_StrPolyHash_from_str_str_hash(" + src_var + ")" end if src_t == "sym_int_hash" @needs_str_poly_hash = 1 @needs_sym_int_hash = 1 @needs_rb_value = 1 return "sp_StrPolyHash_from_sym_int_hash(" + src_var + ")" end if src_t == "sym_str_hash" @needs_str_poly_hash = 1 @needs_sym_str_hash = 1 @needs_rb_value = 1 return "sp_StrPolyHash_from_sym_str_hash(" + src_var + ")" end if src_t == "sym_poly_hash" @needs_str_poly_hash = 1 @needs_sym_poly_hash = 1 @needs_rb_value = 1 return "sp_StrPolyHash_from_sym_poly_hash(" + src_var + ")" end end "" end def widen_hash_assign(val, vt, rhs_t) if vt == "str_poly_hash" && rhs_t == "str_str_hash" @needs_str_poly_hash = 1 return "((" + c_type(expected_base) + ")(" + val + ").v.p)" end end # Cross-variant hash arg: `<Variant_A> hash` passed where the # callee expects `<Variant_B>` hash. Convert via a runtime helper # so the destination's struct layout matches. if is_hash_type(at) == 1 && is_hash_type(expected_base) == 1 && at != expected_base conv_h = hash_variant_convert(at, expected_base, val) if conv_h != "" return conv_h end end if expected_base == "poly" && at != "poly" return box_value_to_poly(at, val) end if @needs_sym_str_hash == 1 emit_sym_str_hash_runtime end # Sym-key -> str-key converters: emit when both the source sym-keyed # variant and the destination str_poly_hash are in use. Lets # cross-key-shape merge sites (`{:a => 1}.merge({"b" => 2})`) compose # both sides into a single str_poly_hash via sp_StrPolyHash_merge. # Sym keys go through sp_sym_to_s; values are boxed to sp_RbVal. if @needs_str_poly_hash == 1 if @needs_sym_int_hash == 1 emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_int_hash(sp_SymIntHash*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++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_int(sp_SymIntHash_get(h,k)));}return r;}") end if @needs_sym_str_hash == 1 emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_str_hash(sp_SymStrHash*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++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_box_str(sp_SymStrHash_get(h,k)));}return r;}") end if @needs_sym_poly_hash == 1 emit_raw("static sp_StrPolyHash*sp_StrPolyHash_from_sym_poly_hash(sp_SymPolyHash*h){sp_StrPolyHash*r=sp_StrPolyHash_new();if(!h)return r;r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++){sp_sym k=h->order[i];sp_StrPolyHash_set(r,sp_sym_to_s(k),sp_SymPolyHash_get(h,k));}return r;}") end end end # Index of symbol name in @sym_names, or -1 if not found.