Commit 3a1e23c9
3a1e23c985adMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
codegen: raise NameError for .new on an unresolved constant
A .new on a constant Spinel can't resolve -- a genuine undefined constant, or
a real stdlib class Spinel doesn't implement (Pathname, OpenStruct, IPAddr) --
used to degrade to an inert 0. The object's methods then returned nil, so a
program that actually used it (e.g. Pathname.new(p).to_s) diverged from CRuby
silently: empty output, no error. That is the worst failure mode.
Raise NameError "uninitialized constant " instead, mirroring the value-
position read of an unresolved constant: if it can't work, it fails loudly and
is rescuable. The raise expression stays int-typed, so an ivar assigned from it
still compiles (the original reason this path emitted 0 -- avoiding an
undeclared sp_<Class> * struct field -- is preserved).
Classes Spinel models as a no-op are unaffected: Mutex (single-threaded) and
friends are special-cased before this point and never reach it, so
Mutex.new.synchronize { ... } still runs the block. User-defined classes are
unaffected. Verified: only stdlib_class_in_ivar depended on the old silent
degradation; it is rewritten to assert the raise.
Gates: 928 tests pass, 57 benchmarks pass, optcarrot checksum 59662 (emitted C unchanged), clang clean.
Refs #1280
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 3a1e23c985ad
- Parent
- 25dbd731b0ed
src/codegen_call.cmodifiedView file
buf_puts(b, ")"); return; } /* unrecognized stdlib class .new (Pathname, OpenStruct, IPAddr, etc.): emit 0. The object is inert -- methods on it later degrade to nil -- so a program that actually uses it diverges silently from CRuby. Surface the site under SPINEL_WARN_UNRESOLVED (no behaviour change). */ /* `.new` on a constant Spinel could not resolve -- not a user class, not a builtin/stdlib class handled above (Mutex, Thread, etc. return earlier). It is either a genuine undefined constant or a real stdlib class Spinel doesn't implement (Pathname, OpenStruct, IPAddr, ...). Either way the object can't work, so raise NameError rather than silently degrade to an inert 0 whose methods then return nil (a program that used it would diverge from CRuby with no signal). Mirrors the value-position read of an unresolved constant. The raise expression is int-typed, so an ivar slot assigned from it still compiles. */ if (cn) { if (warn_unresolved_pos(c, id)) fprintf(stderr, "unresolved constant '%s' in '%s.new' -> inert 0 " "(methods on the result degrade to nil)\n", cn, cn); buf_puts(b, "0LL"); TyKind nret = comp_ntype(c, id); buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant %s\"), %s)", cn, (is_scalar_ret(nret) && nret != TY_UNKNOWN) ? default_value(nret) : "sp_box_nil()"); return; } }test/stdlib_class_in_ivar.rbmodifiedView file
Loading diff…
test/stdlib_class_in_ivar.rb.expectedmodifiedView file
Loading diff…