Commit 02005f26

02005f26debaMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): emit stub body for instance_eval(&b) trampolines

PR #124's def m(&b); instance_eval(&b); end trampoline mechanism detects this exact shape at the call site (recv.m { ... }) and splices the block inline with self rebound to recv. The body of the trampoline method itself is dead at runtime — every call site bypasses it. But emit_instance_method was still compiling the body, which contains a single instance_eval(&block) call that hits the warn-and-emit-0 fallback at compile_no_recv_call_expr's tail (added in b17ec47). Result: every trampoline definition emitted one "warning: cannot resolve call to 'instance_eval'" line per codegen.

Detect trampolines at emit_instance_method time and skip body compilation. The fallback return c_return_default(rt); at the end of the function still emits a syntactically-valid C body for the linker; the body is unreachable so the return value doesn't matter.

The earlier removal of test/instance_eval_trampoline.rb (40d112e) was prompted by exactly this warning noise; with the body skipped the test runs cleanly. Restored.

Implementation note: the obvious one-liner

is_tramp = midx >= 0 && is_instance_eval_trampoline(...) == 1

binds is_tramp to TrueClass/FalseClass under CRuby semantics, which makes the subsequent is_tramp == 0 / == 1 int-compares silently false. Explicit if-block + 1/0 sentinel matches Spinel's codegen-internal idiom (helpers return integer 1/0, not booleans), so the same source compiles correctly under both CRuby and the self-hosted spinel binary.

Bootstrap (gen2 == gen3) clean; 199 tests + 56 benches pass; zero warnings across all test/.rb and benchmark/.rb.

Refs #124

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

Details

Commit
02005f26deba
Parent
51d369bb0e1c

spinel_codegen.rbmodifiedView file

@@ -11928,7 +11928,22 @@
1192811928 j = j + 1
1192911929 end
1193011930
11931- if bid >= 0
11931+ # PR #124 trampoline (`def m(&b); instance_eval(&b); end`):
11932+ # the body is dead at runtime — every call site `recv.m { ... }`
11933+ # gets splice-rewritten by try_yield_or_trampoline_dispatch.
11934+ # Skipping the body compilation avoids the warn-and-emit-0
11935+ # fallback that compile_no_recv_call_expr would hit for the dead
11936+ # `instance_eval(&block)` call inside (which would otherwise fire
11937+ # one warning per trampoline definition every codegen run). The
11938+ # fallback `return c_return_default(rt);` below still provides a
11939+ # syntactically-valid but unreachable C body for the linker.
11940+ is_tramp = 0
11941+ if midx >= 0
11942+ if is_instance_eval_trampoline(ci, midx) == 1
11943+ is_tramp = 1
11944+ end
11945+ end
11946+ if bid >= 0 && is_tramp == 0
1193211947 declare_method_locals(bid, pnames)
1193311948 if @in_gc_scope == 0
1193411949 if @needs_gc == 1