Commit 1e1854d5

1e1854d5ab11Merged

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

Description

Extend the NoMethodError gate to String receivers

An unresolved method call on a String receiver -- one no builtin String method matches, e.g. s.each (a genuine NoMethodError in Ruby) -- now lowers to the same typed nil/0 stub the gate already emits for poly / nil / int / unknown receivers, instead of aborting compilation with "unsupported call".

The motivating shape (#1434): a String|Hash parameter that the whole closed-world program only ever calls with a String. opts is then monomorphically String -- the correct inference, not a collapse -- so the if opts.is_a?(String) ... else opts.each end Hash branch is statically dead. CRuby always takes the String branch and never reaches the would-be NoMethodError, so a runtime-nil stub matches observable behaviour; the prior hard error rejected a program that is correct Ruby. This also covers the sibling pairs = s.split(...); pairs.each surface when pairs is String-typed for the same reason.

A String is a builtin with a closed method table, so an unresolved call on it is the String analogue of the poly/int case rather than a user-class typo; a concrete user-object receiver still errors, which is a genuine missing method worth catching. SPINEL_WARN_UNRESOLVED lists every gated site for auditing. Full gate green (tests, bench, optcarrot checksum 59662); the full #1434 serve.tep.rb reproducer now compiles through to link (only external FFI objects are missing).

Refs #1434

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
1e1854d5ab11
Parent
81833b44c7eb

src/codegen_call.cmodifiedView file

@@ -8755,11 +8755,21 @@
87558755 matches) yields a typed nil/0 placeholder instead of aborting. In practice
87568756 such a call is guarded by a runtime-nil receiver (e.g. an optional hook that
87578757 is never installed), so it never executes; emitting the inferred-type
8758- default keeps codegen going without changing observable behaviour. A
8759- concrete object receiver still errors -- that is a genuine missing method. */
8758+ default keeps codegen going without changing observable behaviour.
8759+
8760+ TY_STRING is included for the same reason: a String is a builtin with a
8761+ closed method table, so an unresolved call on it (e.g. `s.each`, which is a
8762+ real NoMethodError in Ruby) is the String analogue of the poly/int case,
8763+ not a user-class typo. The motivating shape is a `String|Hash` parameter
8764+ that this closed-world program only ever calls with a String: the
8765+ `if x.is_a?(String) ... else x.each end` Hash branch is then statically
8766+ dead, and CRuby never reaches its NoMethodError, so a runtime-nil stub
8767+ matches observable behaviour (#1434). A concrete user-object receiver still
8768+ errors -- that is a genuine missing method worth catching at compile time. */
87608769 if (recv >= 0) {
87618770 TyKind grt = comp_ntype(c, recv);
8762- if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN) {
8771+ if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN ||
8772+ grt == TY_STRING) {
87638773 TyKind ret = comp_ntype(c, id);
87648774 /* Opt-in visibility: this call silently becomes nil/0 where CRuby would
87658775 raise NoMethodError. The degradation is deliberate (a dead poly-dispatch