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

@@ -2690,15 +2690,19 @@
26902690 buf_puts(b, ")");
26912691 return;
26922692 }
2693- /* unrecognized stdlib class .new (Pathname, OpenStruct, IPAddr, etc.): emit 0.
2694- The object is inert -- methods on it later degrade to nil -- so a program
2695- that actually uses it diverges silently from CRuby. Surface the site under
2696- SPINEL_WARN_UNRESOLVED (no behaviour change). */
2693+ /* `.new` on a constant Spinel could not resolve -- not a user class, not a
2694+ builtin/stdlib class handled above (Mutex, Thread, etc. return earlier).
2695+ It is either a genuine undefined constant or a real stdlib class Spinel
2696+ doesn't implement (Pathname, OpenStruct, IPAddr, ...). Either way the
2697+ object can't work, so raise NameError rather than silently degrade to an
2698+ inert 0 whose methods then return nil (a program that used it would
2699+ diverge from CRuby with no signal). Mirrors the value-position read of an
2700+ unresolved constant. The raise expression is int-typed, so an ivar slot
2701+ assigned from it still compiles. */
26972702 if (cn) {
2698- if (warn_unresolved_pos(c, id))
2699- fprintf(stderr, "unresolved constant '%s' in '%s.new' -> inert 0 "
2700- "(methods on the result degrade to nil)\n", cn, cn);
2701- buf_puts(b, "0LL");
2703+ TyKind nret = comp_ntype(c, id);
2704+ buf_printf(b, "(sp_raise_cls(\"NameError\", \"uninitialized constant %s\"), %s)",
2705+ cn, (is_scalar_ret(nret) && nret != TY_UNKNOWN) ? default_value(nret) : "sp_box_nil()");
27022706 return;
27032707 }
27042708 }