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
matches) yields a typed nil/0 placeholder instead of aborting. In practice such a call is guarded by a runtime-nil receiver (e.g. an optional hook that is never installed), so it never executes; emitting the inferred-type default keeps codegen going without changing observable behaviour. A concrete object receiver still errors -- that is a genuine missing method. */ default keeps codegen going without changing observable behaviour. TY_STRING is included for the same reason: a String is a builtin with a closed method table, so an unresolved call on it (e.g. `s.each`, which is a real NoMethodError in Ruby) is the String analogue of the poly/int case, not a user-class typo. The motivating shape is a `String|Hash` parameter that this closed-world program only ever calls with a String: the `if x.is_a?(String) ... else x.each end` Hash branch is then statically dead, and CRuby never reaches its NoMethodError, so a runtime-nil stub matches observable behaviour (#1434). A concrete user-object receiver still errors -- that is a genuine missing method worth catching at compile time. */ if (recv >= 0) { TyKind grt = comp_ntype(c, recv); if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN) { if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN || grt == TY_STRING) { TyKind ret = comp_ntype(c, id); /* Opt-in visibility: this call silently becomes nil/0 where CRuby would raise NoMethodError. The degradation is deliberate (a dead poly-dispatchtest/string_param_dead_each_branch.rbaddedView file
Loading diff…
test/string_param_dead_each_branch.rb.expectedaddedView file
Loading diff…