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
@nd_count = 0 @root_id = 0 # Issue: unresolved-call warnings deduped by "<mname>:<recv_type>" # so a hot call site that fails to resolve emits one warning, not N. @unresolved_call_warnings = "".split(",") # ---- Top-level methods (parallel arrays) ---- @meth_names = "".split(",") @meth_param_names = "".split(",") 0 end # Print a stderr warning the first time we see an unresolved call to # `mname` with the given receiver-type tag. Subsequent identical # warnings are suppressed so a silent-fallthrough call inside a hot # loop emits one line, not a torrent. The warning is informational # only — codegen continues and emits `0` for the call's C expression # (the historical silent-no-op behaviour) so existing tests/benches # whose outputs happen to coincide with `0` keep compiling. def warn_unresolved_call(mname, recv_tag) key = mname + ":" + recv_tag i = 0 while i < @unresolved_call_warnings.length if @unresolved_call_warnings[i] == key return end i = i + 1 end @unresolved_call_warnings.push(key) $stderr.puts "warning: cannot resolve call to '" + mname + "' on " + recv_tag + " (emitting 0)" end # Walk every class's parent chain. A cycle anywhere on the chain is # a fatal program error: bail with a clear message instead of letting # the recursive helpers loop forever. Self-inheritance (`class A < A`) return r end # Unresolved method call on a known receiver. None of the dispatch # branches above claimed the shape — typical causes: typo in the # method name, missing def on the receiver class, or a Ruby idiom # Spinel doesn't support yet. Warn loud at codegen so the user # sees the problem instead of just getting a silently-zero-valued # binary; emit `0` as the C expression so existing call sites that # genuinely rely on the silent fallback (the instance_eval # trampoline body, partially-implemented features whose bench/test # outputs happen to coincide with `0`) keep compiling. Hard fail # would catch more typos but tear up those existing patterns. warn_unresolved_call(mname, base_type(recv_type)) "0" end rk = rk + 1 end end return "0" # Unresolved bare-name call (`foobar(0)`, `foobar`). CRuby would # raise NoMethodError; Spinel can't, but warning at codegen so the # user sees the problem is far better than a silently-empty binary. # See the matching warn at the receiver-form fallthrough above for # why this is a warn-and-emit-0 rather than a hard error. warn_unresolved_call(mname, "(no receiver)") "0" end