Commit 99df0ee0

99df0ee0cd18Merged

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

Description

Infer an empty method body as returning nil, not void

def f; end returns nil in Ruby, so f.nil? must be true. infer_body_return typed a body with no statements (and a method with no body node at all) as "void", which the codegen lowered to a plain mrb_int return, so the caller's nil? / comparisons on the result took the wrong branch. Return "nil" for both empty-body shapes; a body with statements still returns its last expression.

Fixes #1074

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

Details

Commit
99df0ee0cd18
Parent
9142138b6caa

spinel_analyze.rbmodifiedView file

@@ -20814,12 +20814,16 @@
2081420814 end
2081520815
2081620816 def infer_body_return(body_id)
20817+ # An empty method body -- whether the parser yields no body node
20818+ # (body_id < 0) or an empty statement list -- implicitly returns nil
20819+ # in Ruby, so `f.nil?` on the result must be true. (A body that *has*
20820+ # statements returns its last expression, handled below.)
2081720821 if body_id < 0
20818- return "void"
20822+ return "nil"
2081920823 end
2082020824 stmts = get_stmts(body_id)
2082120825 if stmts.length == 0
20822- return "void"
20826+ return "nil"
2082320827 end
2082420828 # Collect all explicit return types
2082520829 types = "".split(",", -1)