Commit b634f441
b634f4416e86Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: infer_type for IfNode now unifies then/else branch types
Previously only the then-branch type was used, ignoring the else-branch entirely. Now both branches are collected and unified via unify_return_type, correctly producing nullable types (T?) when one branch returns T and the other nil, instead of silently dropping the else type.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- b634f4416e86
- Parent
- 79fb2c44eabe
spinel_codegen.rbmodifiedView file
@@ -906,14 +906,34 @@
906906
return infer_call_type(nid)907907
end908908
if t == "IfNode"909+
then_type = "nil"909910
body = @nd_body[nid]910911
if body >= 0911912
stmts = get_stmts(body)912913
if stmts.length > 0913-
return infer_type(stmts.last)914+
then_type = infer_type(stmts.last)915+
end916+
end917+
else_type = "nil"918+
sub = @nd_subsequent[nid]919+
if sub >= 0920+
if @nd_type[sub] == "ElseNode"921+
ebody = @nd_body[sub]922+
if ebody >= 0923+
es = get_stmts(ebody)924+
if es.length > 0925+
else_type = infer_type(es.last)926+
end927+
end928+
else929+
# elsif chain — recurse930+
else_type = infer_type(sub)914931
end915932
end916-
return "void"933+
types = "".split(",")934+
types.push(then_type)935+
types.push(else_type)936+
return unify_return_type(types)917937
end918938
if t == "CaseMatchNode"919939
# Infer from first in branch