Commit 370ea01b

370ea01b2761Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

diag(codegen): warn on unresolved method calls instead of silent zero

compile_call_expr and compile_no_recv_call_expr had two fallthrough sites that returned the literal C string "0" whenever the dispatch failed to recognise a call shape. The result for the user was a successfully-compiled binary that silently did nothing:

$ cat foobar.rb
foobar(0)
$ spinel foobar.rb
foobar.rb -> foobar
$ ./foobar
$ echo $?
0

CRuby raises NoMethodError; Spinel can't, but the user reasonably expects at least a diagnostic. Print a deduped stderr warning with the method name and receiver-type tag at each fallthrough, then keep emitting 0 so the codegen continues.

Hard error was the first attempt but it tears up two existing patterns that the silent-zero was load-bearing for: the instance_eval(&block) body of a trampoline method (the call site is rewritten to splice the block, the body is dead at runtime but still emitted, and the instance_eval call there has no receiver-form dispatch — test/instance_eval_trampoline.rb documents this as "silent no-op by design"), and (-1..1).count { ... } / int.times { ... } in benchmark/life.rb (Range#count with block and Integer#times with block aren't implemented; the bench's output happens to coincide with 0 so it passed despite the broken dispatch). Warning preserves both, while still surfacing genuine typos and missing-feature paths to the user.

Dedupe key is <mname>:<recv_type> so a hot call site emits one warning, not N. spinel_codegen.rb itself self-hosts with zero warnings; 1/199 tests and 1/56 benches emit warnings, all true positives. Bootstrap (gen2 == gen3) clean; full test+bench passes.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
370ea01b2761
Parent
359b9a8d6074

spinel_codegen.rbmodifiedView file

@@ -93,6 +93,10 @@
9393 @nd_count = 0
9494 @root_id = 0
9595
96+ # Issue: unresolved-call warnings deduped by "<mname>:<recv_type>"
97+ # so a hot call site that fails to resolve emits one warning, not N.
98+ @unresolved_call_warnings = "".split(",")
99+
96100 # ---- Top-level methods (parallel arrays) ----
97101 @meth_names = "".split(",")
98102 @meth_param_names = "".split(",")
@@ -4179,6 +4183,26 @@
41794183 0
41804184 end
41814185
4186+ # Print a stderr warning the first time we see an unresolved call to
4187+ # `mname` with the given receiver-type tag. Subsequent identical
4188+ # warnings are suppressed so a silent-fallthrough call inside a hot
4189+ # loop emits one line, not a torrent. The warning is informational
4190+ # only — codegen continues and emits `0` for the call's C expression
4191+ # (the historical silent-no-op behaviour) so existing tests/benches
4192+ # whose outputs happen to coincide with `0` keep compiling.
4193+ def warn_unresolved_call(mname, recv_tag)
4194+ key = mname + ":" + recv_tag
4195+ i = 0
4196+ while i < @unresolved_call_warnings.length
4197+ if @unresolved_call_warnings[i] == key
4198+ return
4199+ end
4200+ i = i + 1
4201+ end
4202+ @unresolved_call_warnings.push(key)
4203+ $stderr.puts "warning: cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)"
4204+ end
4205+
41824206 # Walk every class's parent chain. A cycle anywhere on the chain is
41834207 # a fatal program error: bail with a clear message instead of letting
41844208 # the recursive helpers loop forever. Self-inheritance (`class A < A`)
@@ -14839,6 +14863,17 @@
1483914863 return r
1484014864 end
1484114865
14866+ # Unresolved method call on a known receiver. None of the dispatch
14867+ # branches above claimed the shape — typical causes: typo in the
14868+ # method name, missing def on the receiver class, or a Ruby idiom
14869+ # Spinel doesn't support yet. Warn loud at codegen so the user
14870+ # sees the problem instead of just getting a silently-zero-valued
14871+ # binary; emit `0` as the C expression so existing call sites that
14872+ # genuinely rely on the silent fallback (the instance_eval
14873+ # trampoline body, partially-implemented features whose bench/test
14874+ # outputs happen to coincide with `0`) keep compiling. Hard fail
14875+ # would catch more typos but tear up those existing patterns.
14876+ warn_unresolved_call(mname, base_type(recv_type))
1484214877 "0"
1484314878 end
1484414879
@@ -15161,7 +15196,12 @@
1516115196 rk = rk + 1
1516215197 end
1516315198 end
15164- return "0"
15199+ # Unresolved bare-name call (`foobar(0)`, `foobar`). CRuby would
15200+ # raise NoMethodError; Spinel can't, but warning at codegen so the
15201+ # user sees the problem is far better than a silently-empty binary.
15202+ # See the matching warn at the receiver-form fallthrough above for
15203+ # why this is a warn-and-emit-0 rather than a hard error.
15204+ warn_unresolved_call(mname, "(no receiver)")
1516515205 "0"
1516615206 end
1516715207