Commit 5abbddde

5abbddde00c0Merged

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

Description

codegen: raise NoMethodError on unresolved method calls

The fallthrough at the bottom of method dispatch silently emitted 0 when no arm matched. With method_missing not supported, the call should fail loudly at runtime instead of producing a confused zero.

Replace the bare "0" return with a statement-expression that calls sp_raise_cls("NoMethodError", "undefined method '' for ") and yields 0. The raise only fires when the expression is actually evaluated -- chain-recognition arms (e.g. `4.times.map { ... }` which generates its own for-loop and discards the .times call) already build their result without consulting the receiver expression, so they keep working; genuine "x.unknown_method" sites surface the bug.

Existing 476-test suite still green; behaviour change is only for sites that were silently returning 0.

Fixes #727

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
5abbddde00c0
Parent
36ecd4b9c187

spinel_codegen.rbmodifiedView file

@@ -16165,15 +16165,17 @@
1616516165 # Unresolved method call on a known receiver. None of the dispatch
1616616166 # branches above claimed the shape — typical causes: typo in the
1616716167 # method name, missing def on the receiver class, or a Ruby idiom
16168- # Spinel doesn't support yet. Warn loud at codegen so the user
16169- # sees the problem instead of just getting a silently-zero-valued
16170- # binary; emit `0` as the C expression so existing call sites that
16171- # genuinely rely on the silent fallback (the instance_eval
16172- # trampoline body, partially-implemented features whose bench/test
16173- # outputs happen to coincide with `0`) keep compiling. Hard fail
16174- # would catch more typos but tear up those existing patterns.
16168+ # Spinel doesn't support yet. Warn loud at codegen and bake a
16169+ # runtime NoMethodError raise into the expression itself via a
16170+ # statement-expression. The raise only fires when the expression
16171+ # is actually evaluated -- so chain-recognition arms (e.g.
16172+ # `4.times.map { ... }` which generates its own for-loop and
16173+ # discards the times call) keep working, while genuine
16174+ # "x.unknown_method" sites surface the bug instead of returning
16175+ # a silent zero. Issue #727.
1617516176 warn_unresolved_call(mname, base_type(recv_type))
16176- "0"
16177+ @needs_exc_class_hierarchy = 1
16178+ "({ sp_raise_cls(\"NoMethodError\", \"undefined method '" + mname + "' for " + base_type(recv_type) + "\"); (mrb_int)0; })"
1617716179 end
1617816180
1617916181 def compile_block_given_expr