Commit 25dbd731
25dbd731b0edMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
codegen: SPINEL_WARN_UNRESOLVED surfaces calls that silently degrade to nil/0
Spinel resolves what it can at compile time and, for an unresolved call on a
dynamically-typed receiver (poly/nil/int/unknown) or a .new on an unresolved
constant, degrades to a typed nil/0 placeholder instead of raising. That keeps
codegen going -- and is sometimes deliberate (a dead poly-dispatch arm, or a
real-but-unimplemented stdlib class like Mutex/Pathname kept inert) -- but it
also hides genuine missing-method / undefined-constant errors that CRuby would
raise, the most dangerous failure mode because it is silent.
Add an opt-in diagnostic: with SPINEL_WARN_UNRESOLVED set, every such site is
reported to stderr with its Ruby file:line, so a port can be audited without
changing runtime behaviour. Covers both the NoMethodError gate (unresolved call
on a dynamic receiver) and the unresolved-constant .new degradation -- the
latter previously had no signal at all when the constant was referenced without
a (warned) require (e.g. relying on a preloaded class). Default-off: zero
codegen/runtime effect, the emitted C is unchanged.
This does not change the degradation itself. Raising NameError for a genuinely
undefined constant .new is deferred: that site cannot distinguish a typo from
a real-but-unimplemented stdlib class, so a precise raise needs an allowlist or
a require->constant map (a separate design decision).
Gates: 928 tests pass, 57 benchmarks pass, optcarrot checksum 59662, clang clean.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 25dbd731b0ed
- Parent
- 6e86ed917e73
src/codegen_call.cmodifiedView file
buf_puts(b, default_value(t));}/* Print "spinel: <file>:<line>: warning: " for node `id` when SPINEL_WARN_UNRESOLVED is set, so a call/constant that silently degrades to nil/0 (where CRuby would raise or do real work) can be audited. Returns 1 when a warning line was started -- the caller appends its message + newline. Zero runtime/codegen effect: opt-in stderr only. */static int warn_unresolved_pos(Compiler *c, int id) { if (!getenv("SPINEL_WARN_UNRESOLVED")) return 0; const NodeTable *nt = c->nt; int ln = (int)nt_int(nt, id, "node_line", 0); const char *file = nt->source_file; if (ln > 0) { const char *f = nt_file_path(nt, (int)nt_int(nt, id, "node_file", 0)); if (f && *f) file = f; } if (!file || !*file) file = "source.rb"; fprintf(stderr, "spinel: %s:%d: warning: ", file, ln); return 1;}void emit_call(Compiler *c, int id, Buf *b) { const NodeTable *nt = c->nt; /* `require` / `require_relative` is a compile-time directive: top-level ones buf_puts(b, ")"); return; } /* unrecognized stdlib class .new (Pathname, OpenStruct, IPAddr, etc.): emit 0 */ if (cn) { buf_puts(b, "0LL"); 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). */ 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"); return; } } } TyKind grt = comp_ntype(c, recv); if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN) { 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-dispatch arm or an inference gap), but it can also hide a genuine missing method. SPINEL_WARN_UNRESOLVED lists every such site so a port can be audited without changing runtime behaviour. */ if (warn_unresolved_pos(c, id)) { const char *nm = nt_str(nt, id, "name"); fprintf(stderr, "unresolved call '%s' on %s receiver -> nil " "(CRuby would raise NoMethodError)\n", nm ? nm : "?", ty_name(grt)); } buf_puts(b, (is_scalar_ret(ret) && ret != TY_UNKNOWN) ? default_value(ret) : "sp_box_nil()"); return; }