Commit b3e278f1
b3e278f17a9bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): collect return types through UnlessNode arms
collect_return_types had IfNode / ElseNode / WhileNode / CaseMatchNode arms but no UnlessNode arm, so
def outer(flag)
unless flag
return { x: "y" }
end
nil
end
walked past the unless-body's return { x: "y" } and only saw
the trailing nil, declaring outer's return type as mrb_int.
The hash-returning path then failed C compile with an int-to-
pointer conversion warning.
Add the UnlessNode arm mirroring IfNode: recurse into the body
(then-branch, runs when predicate is false) and the else_clause.
Equivalent if !cond; return {hash}; end; nil already worked
because IfNode was handled — the bug was strictly the missing
sibling for the negated form.
Tests: 403 pass (+1 regression covering UnlessNode with body return + trailing nil, UnlessNode with else_clause carrying returns from both arms, and a top-level def variant), bench 52, optcarrot checksum 59662.
Fixes #449
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b3e278f17a9b
- Parent
- 951f34f4ccfd
spinel_analyze.rbmodifiedView file
end return end if @nd_type[nid] == "UnlessNode" # `unless cond; ...; end` — same shape as IfNode but the # then-branch fires when the predicate is false. body holds # the then-statements, else_clause holds the else block. # Without this arm, `unless cond; return {hash}; end; nil` # only registered the trailing nil as the function's return # path, mistyping the declared return as mrb_int and failing # C compile on the hash-returning path (#449). body = @nd_body[nid] if body >= 0 collect_return_types_nid(body, types) end ec = @nd_else_clause[nid] if ec >= 0 collect_return_types(ec, types) end return end if @nd_type[nid] == "ElseNode" body = @nd_body[nid] if body >= 0test/unless_return_path_hash.rbaddedView file
Loading diff…
test/unless_return_path_hash.rb.expectedaddedView file
Loading diff…