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 + p and str * p (already excluded str % 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

@@ -1589,9 +1589,10 @@
15891589
15901590 /* poly receiver / poly operand: result type of operations on sp_RbVal */
15911591 if (recv >= 0 && (rt == TY_POLY || a0 == TY_POLY)) {
1592- /* `str % poly_args` is printf-style formatting (result string), not a poly
1593- arithmetic modulo -- defer to the rt==TY_STRING path below. */
1594- if (!(rt == TY_STRING && !strcmp(name, "%")) &&
1592+ /* String operators with a poly operand are NOT poly arithmetic: `str % x`
1593+ is printf formatting, `str + x` is concatenation, `str * n` is repeat --
1594+ all yield a string. Defer them to the rt==TY_STRING path below. */
1595+ if (!(rt == TY_STRING && (!strcmp(name, "%") || !strcmp(name, "+") || !strcmp(name, "*"))) &&
15951596 (!strcmp(name, "+") || !strcmp(name, "-") || !strcmp(name, "*") ||
15961597 !strcmp(name, "/") || !strcmp(name, "%") || !strcmp(name, "**")))
15971598 return TY_POLY;
@@ -2034,7 +2035,8 @@
20342035 if (rt == TY_STRING) {
20352036 if (!strcmp(name, "%")) return TY_STRING; /* sprintf (array or single value) */
20362037 if (!strcmp(name, "+") || !strcmp(name, "*")) {
2037- if (a0 == TY_POLY) return TY_POLY; /* codegen uses sp_poly_add for poly operand */
2038+ /* `str + x` / `str * n` always yield a String; a poly operand (which
2039+ holds a string at runtime) is coerced via sp_poly_to_s in codegen. */
20382040 return TY_STRING;
20392041 }
20402042 return TY_UNKNOWN;