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

@@ -13504,6 +13504,24 @@
1350413504 end
1350513505 return
1350613506 end
13507+ if @nd_type[nid] == "UnlessNode"
13508+ # `unless cond; ...; end` — same shape as IfNode but the
13509+ # then-branch fires when the predicate is false. body holds
13510+ # the then-statements, else_clause holds the else block.
13511+ # Without this arm, `unless cond; return {hash}; end; nil`
13512+ # only registered the trailing nil as the function's return
13513+ # path, mistyping the declared return as mrb_int and failing
13514+ # C compile on the hash-returning path (#449).
13515+ body = @nd_body[nid]
13516+ if body >= 0
13517+ collect_return_types_nid(body, types)
13518+ end
13519+ ec = @nd_else_clause[nid]
13520+ if ec >= 0
13521+ collect_return_types(ec, types)
13522+ end
13523+ return
13524+ end
1350713525 if @nd_type[nid] == "ElseNode"
1350813526 body = @nd_body[nid]
1350913527 if body >= 0