Commit f8ad2fae
f8ad2fae3a36Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Type str + poly / str * poly as String, not poly
s = s + x where x is statically poly (but holds a string at runtime, e.g.
a type-tag returned by a poly-typed method) monotonically poisoned the
accumulator s to poly: String#+ with a poly operand was typed TY_POLY.
Ruby's String#+ / String#* require a String / Integer operand and always
yield a String, so the result type is String regardless of the operand's
static type.
Three coordinated changes:
- infer_call's poly-operand short-circuit (rt/a0 poly) now excludes
str + pandstr * p(already excludedstr % p), letting them reach the rt==TY_STRING arith path. - That path returns TY_STRING for
+/*instead of poly-on-poly-operand. - codegen coerces a poly operand to a C string (sp_poly_to_s) / int (sp_poly_to_i) for sp_str_concat / sp_str_repeat.
Surfaced compiling the legacy compiler with spinelc: collect_ptypes_str
built its result via result + infer_type(...) (infer_type's method return
is poly), poisoning it to poly, which propagated to @meth_param_types
(poly_array) and broke every ptypes.join. Self-host gaps 25 -> 6.
Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all green.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- f8ad2fae3a36
- Parent
- cc4a3f07b5f6
src/analyze_infer.cmodifiedView file
/* poly receiver / poly operand: result type of operations on sp_RbVal */ if (recv >= 0 && (rt == TY_POLY || a0 == TY_POLY)) { /* `str % poly_args` is printf-style formatting (result string), not a poly arithmetic modulo -- defer to the rt==TY_STRING path below. */ if (!(rt == TY_STRING && !strcmp(name, "%")) && /* String operators with a poly operand are NOT poly arithmetic: `str % x` is printf formatting, `str + x` is concatenation, `str * n` is repeat -- all yield a string. Defer them to the rt==TY_STRING path below. */ if (!(rt == TY_STRING && (!strcmp(name, "%") || !strcmp(name, "+") || !strcmp(name, "*"))) && (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") || !strcmp(name, "/") || !strcmp(name, "%") || !strcmp(name, "**"))) return TY_POLY; if (rt == TY_STRING) { if (!strcmp(name, "%")) return TY_STRING; /* sprintf (array or single value) */ if (!strcmp(name, "+") || !strcmp(name, "*")) { if (a0 == TY_POLY) return TY_POLY; /* codegen uses sp_poly_add for poly operand */ /* `str + x` / `str * n` always yield a String; a poly operand (which holds a string at runtime) is coerced via sp_poly_to_s in codegen. */ return TY_STRING; } return TY_UNKNOWN;src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…