Commit 4de7032c

4de7032cd104Merged

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

Description

fix(codegen): widen user-class def [](k) param from poly-recv arg type

Two-part fix for the same bug family:

  1. scan_new_calls' receiver-method widening had a single arm that only fired when infer_type(recv) returned a concrete obj_<C>. For poly receivers (a method param widened to accept multiple user classes — the common case for cross-class dispatch), the per-class [] ptype stayed at the default int, so the per-arm call in compile_poly_method_call passed a const char * key to a function whose signature still said mrb_int. Add a parallel poly arm: walk every user class that defines mname, unify each class's ptypes with the call site's arg types. unify_call_types collapses to poly if a class has another disagreeing call site.

  2. compile_poly_method_call's int-bit-extract emit and emit_poly_builtin_dispatch's [] block both unconditionally emitted (recv.v.i >> key) & 1 and sp_IntArray_get(..., key) etc. — both expect mrb_int keys. Gate on arg_types[0] being int or poly; for non-int keys (string, sym) the SP_TAG_INT and SP_BUILTIN_INT_ARRAY etc. arms are unreachable at runtime and emitting them produces Wint-conversion warnings or hard incompatible-pointer errors.

Surfaced via Roundhouse's HashWithIndifferentAccess#initialize shape (other[k] poly-recv lookup with String key from other.keys), which currently emits 11+ errors per archive test (matz/spinel#314 follow-up). Standalone repro in test/poly_recv_aref_str_key.rb.

Fixes #341

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

Details

Commit
4de7032cd104
Parent
d3b8ea6ed04a

spinel_codegen.rbmodifiedView file

@@ -10198,6 +10198,45 @@
1019810198 end
1019910199 end
1020010200 end
10201+ # Issue #341: when the receiver is poly (e.g. a method param
10202+ # widened to accept multiple user classes), the per-class
10203+ # arms emitted by compile_poly_method_call call each class's
10204+ # `<mname>` C function with the same arg expressions. If
10205+ # callers pass a String key but the class methods' params
10206+ # are still at the default `int`, the arms produce
10207+ # `Wint-conversion` warnings or hard `incompatible pointer
10208+ # to integer conversion` errors (sp_A__aref(sp_A *, mrb_int)
10209+ # called with const char *). Walk every user class that
10210+ # defines mname and unify its ptypes with the call site's
10211+ # arg types; unify_call_types collapses to `poly` if any
10212+ # other call site disagrees.
10213+ if rt == "poly"
10214+ args_id_p = @nd_arguments[nid]
10215+ if args_id_p >= 0
10216+ arg_ids_p = get_args(args_id_p)
10217+ ci_p = 0
10218+ while ci_p < @cls_names.length
10219+ midx_p = cls_find_method_direct(ci_p, mname)
10220+ if midx_p >= 0
10221+ all_ptypes_p = @cls_meth_ptypes[ci_p].split("|")
10222+ if midx_p < all_ptypes_p.length
10223+ ptypes_p = all_ptypes_p[midx_p].split(",")
10224+ kk_p = 0
10225+ while kk_p < arg_ids_p.length
10226+ if kk_p < ptypes_p.length
10227+ at_p = infer_type(arg_ids_p[kk_p])
10228+ ptypes_p[kk_p] = unify_call_types(ptypes_p[kk_p], at_p, arg_ids_p[kk_p])
10229+ end
10230+ kk_p = kk_p + 1
10231+ end
10232+ all_ptypes_p[midx_p] = ptypes_p.join(",")
10233+ @cls_meth_ptypes[ci_p] = all_ptypes_p.join("|")
10234+ end
10235+ end
10236+ ci_p = ci_p + 1
10237+ end
10238+ end
10239+ end
1020110240 end
1020210241 # Issue #207: `<Class>.cls_method(args)` — widen class
1020310242 # method parameter types from call-site argument types.
@@ -26222,9 +26261,14 @@
2622226261 # instructions etc.) must compile to `(pc >> 8) & 1`, not to
2622326262 # the array dispatch in the SP_TAG_OBJ block below — which
2622426263 # would silently leave the result at nil and miscount cycles.
26225- if mname == "[]" && arg_compiled.length >= 1
26264+ # Issue #341: only emit the int-bit-extract fallback when the
26265+ # index argument is itself an int (or a poly we can unbox to
26266+ # int). For non-int keys (string, sym, etc.) the SP_TAG_INT
26267+ # branch is unreachable at runtime and `(recv.v.i >> key)`
26268+ # is invalid C — `>> const char *` won't compile.
26269+ if mname == "[]" && arg_compiled.length >= 1 && (arg_types[0] == "int" || arg_types[0] == "poly")
2622626270 a0_int = arg_compiled[0]
26227- if arg_types.length > 0 && arg_types[0] == "poly"
26271+ if arg_types[0] == "poly"
2622826272 a0_int = "(" + a0_int + ").v.i"
2622926273 end
2623026274 bit = "((" + recv_tmp + ".v.i >> (" + a0_int + ")) & 1)"
@@ -26314,7 +26358,16 @@
2631426358 # temp at its default (`0`/empty) for that input, which is
2631526359 # acceptable since the caller's static type analysis already
2631626360 # picked a compatible result type.
26317- if mname == "[]" && arg_compiled.length >= 1
26361+ #
26362+ # Issue #341: every built-in [] arm here passes the key to a
26363+ # function expecting `mrb_int`. Only emit the arms when the key
26364+ # type is int or poly (poly is unboxed via `.v.i` above). For
26365+ # non-int keys (string, sym), these arms are unreachable at
26366+ # runtime (a poly recv carrying a String key would be a hash,
26367+ # not an array) and emitting them produces Wint-conversion
26368+ # warnings — or hard errors under -Werror.
26369+ a0_is_int = arg_compiled.length >= 1 && (arg_types.length == 0 || arg_types[0] == "int" || arg_types[0] == "poly")
26370+ if mname == "[]" && arg_compiled.length >= 1 && a0_is_int
2631826371 ic = "sp_IntArray_get((sp_IntArray *)" + recv_tmp + ".v.p, " + a0 + ")"
2631926372 irhs = is_poly_ret == 1 ? "sp_box_int(" + ic + ")" : ic
2632026373 emit(" if (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY) " + result_tmp + " = " + irhs + ";")