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

@@ -166,6 +166,25 @@
166166 buf_puts(b, default_value(t));
167167 }
168168
169+/* Print "spinel: <file>:<line>: warning: " for node `id` when
170+ SPINEL_WARN_UNRESOLVED is set, so a call/constant that silently degrades to
171+ nil/0 (where CRuby would raise or do real work) can be audited. Returns 1
172+ when a warning line was started -- the caller appends its message + newline.
173+ Zero runtime/codegen effect: opt-in stderr only. */
174+static int warn_unresolved_pos(Compiler *c, int id) {
175+ if (!getenv("SPINEL_WARN_UNRESOLVED")) return 0;
176+ const NodeTable *nt = c->nt;
177+ int ln = (int)nt_int(nt, id, "node_line", 0);
178+ const char *file = nt->source_file;
179+ if (ln > 0) {
180+ const char *f = nt_file_path(nt, (int)nt_int(nt, id, "node_file", 0));
181+ if (f && *f) file = f;
182+ }
183+ if (!file || !*file) file = "source.rb";
184+ fprintf(stderr, "spinel: %s:%d: warning: ", file, ln);
185+ return 1;
186+}
187+
169188 void emit_call(Compiler *c, int id, Buf *b) {
170189 const NodeTable *nt = c->nt;
171190 /* `require` / `require_relative` is a compile-time directive: top-level ones
@@ -2671,8 +2690,17 @@
26712690 buf_puts(b, ")");
26722691 return;
26732692 }
2674- /* unrecognized stdlib class .new (Pathname, OpenStruct, IPAddr, etc.): emit 0 */
2675- if (cn) { buf_puts(b, "0LL"); return; }
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). */
2697+ 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");
2702+ return;
2703+ }
26762704 }
26772705 }
26782706
@@ -8694,6 +8722,17 @@
86948722 TyKind grt = comp_ntype(c, recv);
86958723 if (grt == TY_POLY || grt == TY_NIL || grt == TY_INT || grt == TY_UNKNOWN) {
86968724 TyKind ret = comp_ntype(c, id);
8725+ /* Opt-in visibility: this call silently becomes nil/0 where CRuby would
8726+ raise NoMethodError. The degradation is deliberate (a dead poly-dispatch
8727+ arm or an inference gap), but it can also hide a genuine missing method.
8728+ SPINEL_WARN_UNRESOLVED lists every such site so a port can be audited
8729+ without changing runtime behaviour. */
8730+ if (warn_unresolved_pos(c, id)) {
8731+ const char *nm = nt_str(nt, id, "name");
8732+ fprintf(stderr, "unresolved call '%s' on %s receiver -> nil "
8733+ "(CRuby would raise NoMethodError)\n",
8734+ nm ? nm : "?", ty_name(grt));
8735+ }
86978736 buf_puts(b, (is_scalar_ret(ret) && ret != TY_UNKNOWN) ? default_value(ret) : "sp_box_nil()");
86988737 return;
86998738 }