Commit 36ecd4b9
36ecd4b9c187Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
codegen: warn on `def method_missing` (not dispatched at runtime)
Spinel resolves call targets statically; an unresolved method call emits 0 and prints "cannot resolve call to '' on ". A user- defined method_missing hook never fires, but the def itself compiles silently, so the user has no signal until a confused 0 surfaces in the output.
Pre-scan the AST in generate_code and emit a single warning when a DefNode named method_missing is seen. This is the first step toward #727 -- the second step (raising NoMethodError instead of emitting 0 for unresolved calls) lands separately.
Refs #727
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 36ecd4b9c187
- Parent
- 080b45215710
spinel_codegen.rbmodifiedView file
@@ -7631,6 +7631,14 @@
76317631
def generate_code76327632
stmts = get_body_stmts(@root_id)76337633
7634+
# Issue #727: warn when the program defines `method_missing`.7635+
# Spinel can't statically resolve method_missing dispatch, so any7636+
# call that would otherwise hit the user's hook gets emitted as 07637+
# (with the standard "cannot resolve call to ..." line). The hook7638+
# definition itself looks legitimate at parse time and the user7639+
# has no signal that it won't fire -- this pre-pass surfaces that.7640+
scan_for_method_missing(@root_id)7641+
76347642
emit_raw("/* Generated by Spinel AOT compiler */")76357643
emit_raw("#include \"sp_runtime.h\"")76367644
# Pre-scan for Method usage so emit_class_structs /@@ -11544,6 +11552,30 @@
1154411552
# named "ARGV"). Flips @needs_argv so emit_main's init gate1154511553
# has the right value before it runs. Single global name, so1154611554
# the scan is a flat tree walk -- linear in AST size.11555+
# Issue #727: pre-scan for `def method_missing` definitions and11556+
# warn that they won't fire at runtime. Spinel resolves call11557+
# targets statically; an unresolved call emits 0 and prints11558+
# "cannot resolve ..." rather than dispatching through11559+
# method_missing. The user's hook never runs, but the def itself11560+
# compiles silently, so without this pre-pass there's no signal11561+
# that the contract is broken until a confused 0 surfaces.11562+
def scan_for_method_missing(nid)11563+
if nid < 011564+
return11565+
end11566+
if @nd_type[nid] == "DefNode" && @nd_name[nid] == "method_missing"11567+
$stderr.puts "warning: `def method_missing' is not dispatched in spinel; unresolved calls still emit 0 and print a 'cannot resolve' warning"11568+
return11569+
end11570+
cs = []11571+
push_child_ids(nid, cs)11572+
k = 011573+
while k < cs.length11574+
scan_for_method_missing(cs[k])11575+
k = k + 111576+
end11577+
end11578+
1154711579
def scan_for_argv(nid)1154811580
if nid < 01154911581
return