Commit d5685270

d56852703c56Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): emit Hash arms in poly-recv [] / []= when key is String

#456. Chained `outer[k1][k2] = v` (or `outer[k1][k2]`) where the outer hash carries heterogeneous values (StrPolyHash) had the inner dispatch land on a poly receiver. The existing poly-recv `[]` / `[]=` codegen enumerated only Array storage arms (IntArray / StrArray / FloatArray / SymArray / PtrArray / PolyArray) so:

  • compile failure: the Array-set arms pass the (string) index to `sp_PolyArray_set(sp_PolyArray *, mrb_int, sp_RbVal)` etc., which fails C compile with "passing argument 2 makes integer from pointer without a cast" when the index is a string literal.
  • silent miscompile at the read side: `if (recv.tag == SP_TAG_OBJ) {}` had an empty body for hash storage, so `outer[k1][k2]` returned the default (nil/0) for any Hash-storage receiver.

Add Str-keyed Hash arms to both paths, gated by the index's static type. Sym-keyed variants are deferred — the sp_SymIntHash / sp_SymStrHash typedefs are emit-gated by @needs flags set during pre-scan, which doesn't see this dispatch yet; the Str*Hash family ships unconditionally in sp_runtime.h.

Surfaced in Roundhouse's `CgiIo.assign_form_pair` canonical form-decoder `into[outer][inner] = val`. Closes #456.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
d56852703c56
Parent
3d2e19353fe5

spinel_codegen.rbmodifiedView file

@@ -18301,6 +18301,25 @@
1830118301 # not an array) and emitting them produces Wint-conversion
1830218302 # warnings — or hard errors under -Werror.
1830318303 a0_is_int = arg_compiled.length >= 1 && (arg_types.length == 0 || arg_types[0] == "int" || arg_types[0] == "poly")
18304+ # Hash dispatch arms for `[]` on a poly recv whose runtime
18305+ # storage is a *-Hash variant. The chained `outer[k1][k2]`
18306+ # shape where outer is StrPolyHash lands here — the inner []
18307+ # recv carries an arbitrary str-keyed hash storage. Emit
18308+ # arms keyed by the string-typed index. Sym-keyed variants
18309+ # are skipped here because the sp_SymIntHash / sp_SymStrHash
18310+ # typedefs are emit-gated by @needs flags set during pre-scan,
18311+ # which doesn't see this dispatch path yet; the existing
18312+ # sp_StrIntHash family ships unconditionally in sp_runtime.h.
18313+ if mname == "[]" && arg_compiled.length >= 1
18314+ key_t = arg_types.length > 0 ? arg_types[0] : ""
18315+ if key_t == "string" || key_t == "mutable_str"
18316+ if is_poly_ret == 1
18317+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_STR_INT_HASH) " + result_tmp + " = sp_box_int(sp_StrIntHash_get((sp_StrIntHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + "));")
18318+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_STR_STR_HASH) " + result_tmp + " = sp_box_str(sp_StrStrHash_get((sp_StrStrHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + "));")
18319+ emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_STR_POLY_HASH) " + result_tmp + " = sp_StrPolyHash_get((sp_StrPolyHash *)" + recv_tmp + ".v.p, " + arg_compiled[0] + ");")
18320+ end
18321+ end
18322+ end
1830418323 if mname == "[]" && arg_compiled.length >= 1 && a0_is_int
1830518324 ic = "sp_IntArray_get((sp_IntArray *)" + recv_tmp + ".v.p, " + a0 + ")"
1830618325 irhs = is_poly_ret == 1 ? "sp_box_int(" + ic + ")" : ic
@@ -25418,6 +25437,33 @@
2541825437 return
2541925438 end
2542025439 if rt == "poly"
25440+ # Statically string index: the array arms below (PolyArray /
25441+ # PtrArray / IntArray) all expect an mrb_int index and would
25442+ # fail C compile when called with a `const char *`. The chained
25443+ # `outer[k1][k2] = v` shape where outer is StrPolyHash lands
25444+ # here — `outer[k1]` returns sp_RbVal which may be any hash
25445+ # storage at runtime. Emit Hash setter arms instead, keyed by
25446+ # the same cls_id dispatch.
25447+ idx_t_poly = arg_ids.length >= 1 ? infer_type(arg_ids[0]) : ""
25448+ val_t_poly = arg_ids.length >= 2 ? infer_type(arg_ids[1]) : "int"
25449+ if idx_t_poly == "string" || idx_t_poly == "mutable_str"
25450+ @needs_rb_value = 1
25451+ @needs_str_int_hash = 1
25452+ @needs_str_str_hash = 1
25453+ @needs_str_poly_hash = 1
25454+ vbox = val
25455+ if val_t_poly != "poly"
25456+ vbox = box_value_to_poly(val_t_poly, val)
25457+ end
25458+ vbox_tmp = new_temp
25459+ emit(" sp_RbVal " + vbox_tmp + " = " + vbox + ";")
25460+ cur_h = new_temp
25461+ emit(" sp_RbVal " + cur_h + " = " + rc + ";")
25462+ emit(" if (" + cur_h + ".cls_id == SP_BUILTIN_STR_INT_HASH) sp_StrIntHash_set((sp_StrIntHash *)" + cur_h + ".v.p, " + idx + ", " + vbox_tmp + ".v.i);")
25463+ emit(" else if (" + cur_h + ".cls_id == SP_BUILTIN_STR_STR_HASH) sp_StrStrHash_set((sp_StrStrHash *)" + cur_h + ".v.p, " + idx + ", " + vbox_tmp + ".v.s);")
25464+ emit(" else if (" + cur_h + ".cls_id == SP_BUILTIN_STR_POLY_HASH) sp_StrPolyHash_set((sp_StrPolyHash *)" + cur_h + ".v.p, " + idx + ", " + vbox_tmp + ");")
25465+ return
25466+ end
2542125467 # Slot is sp_RbVal — runtime cls_id determines which storage
2542225468 # the underlying pointer points at. Optcarrot's `@fetch[a] =
2542325469 # method(:peek_X)` lands here when @fetch was widened to poly