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

@@ -1816,6 +1816,50 @@
18161816 # `sp_StrPolyHash *` slot — different struct layouts (vals[] of
18171817 # `const char **` vs `sp_RbVal *`), so reads through the slot return
18181818 # garbage even when -Werror is off. Issue #614.
1819+ # Cross-variant hash converter at call boundary. Returns the C
1820+ # expression for promoting `src_var` (typed `src_t`) into a hash
1821+ # of `dst_t`, or "" when no converter is available. Currently
1822+ # covers conversions into str_poly_hash from the four source
1823+ # variants: str_int_hash, str_str_hash, sym_int_hash, sym_str_hash,
1824+ # sym_poly_hash. Symmetric directions (into sym_poly_hash from
1825+ # str-keyed variants) are absent because spinel's str_poly_hash
1826+ # is the canonical wide-keyed shape.
1827+ def hash_variant_convert(src_t, dst_t, src_var)
1828+ if dst_t == "str_poly_hash"
1829+ if src_t == "str_int_hash"
1830+ @needs_str_poly_hash = 1
1831+ @needs_str_int_hash = 1
1832+ @needs_rb_value = 1
1833+ return "sp_StrPolyHash_from_str_int_hash(" + src_var + ")"
1834+ end
1835+ if src_t == "str_str_hash"
1836+ @needs_str_poly_hash = 1
1837+ @needs_str_str_hash = 1
1838+ @needs_rb_value = 1
1839+ return "sp_StrPolyHash_from_str_str_hash(" + src_var + ")"
1840+ end
1841+ if src_t == "sym_int_hash"
1842+ @needs_str_poly_hash = 1
1843+ @needs_sym_int_hash = 1
1844+ @needs_rb_value = 1
1845+ return "sp_StrPolyHash_from_sym_int_hash(" + src_var + ")"
1846+ end
1847+ if src_t == "sym_str_hash"
1848+ @needs_str_poly_hash = 1
1849+ @needs_sym_str_hash = 1
1850+ @needs_rb_value = 1
1851+ return "sp_StrPolyHash_from_sym_str_hash(" + src_var + ")"
1852+ end
1853+ if src_t == "sym_poly_hash"
1854+ @needs_str_poly_hash = 1
1855+ @needs_sym_poly_hash = 1
1856+ @needs_rb_value = 1
1857+ return "sp_StrPolyHash_from_sym_poly_hash(" + src_var + ")"
1858+ end
1859+ end
1860+ ""
1861+ end
1862+
18191863 def widen_hash_assign(val, vt, rhs_t)
18201864 if vt == "str_poly_hash" && rhs_t == "str_str_hash"
18211865 @needs_str_poly_hash = 1
@@ -6198,6 +6242,15 @@
61986242 return "((" + c_type(expected_base) + ")(" + val + ").v.p)"
61996243 end
62006244 end
6245+ # Cross-variant hash arg: `<Variant_A> hash` passed where the
6246+ # callee expects `<Variant_B>` hash. Convert via a runtime helper
6247+ # so the destination's struct layout matches.
6248+ if is_hash_type(at) == 1 && is_hash_type(expected_base) == 1 && at != expected_base
6249+ conv_h = hash_variant_convert(at, expected_base, val)
6250+ if conv_h != ""
6251+ return conv_h
6252+ end
6253+ end
62016254 if expected_base == "poly" && at != "poly"
62026255 return box_value_to_poly(at, val)
62036256 end
@@ -8272,6 +8325,22 @@
82728325 if @needs_sym_str_hash == 1
82738326 emit_sym_str_hash_runtime
82748327 end
8328+ # Sym-key -> str-key converters: emit when both the source sym-keyed
8329+ # variant and the destination str_poly_hash are in use. Lets
8330+ # cross-key-shape merge sites (`{:a => 1}.merge({"b" => 2})`) compose
8331+ # both sides into a single str_poly_hash via sp_StrPolyHash_merge.
8332+ # Sym keys go through sp_sym_to_s; values are boxed to sp_RbVal.
8333+ if @needs_str_poly_hash == 1
8334+ if @needs_sym_int_hash == 1
8335+ 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;}")
8336+ end
8337+ if @needs_sym_str_hash == 1
8338+ 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;}")
8339+ end
8340+ if @needs_sym_poly_hash == 1
8341+ 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;}")
8342+ end
8343+ end
82758344 end
82768345
82778346 # Index of symbol name in @sym_names, or -1 if not found.