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 end
908908 if t == "IfNode"
909+ then_type = "nil"
909910 body = @nd_body[nid]
910911 if body >= 0
911912 stmts = get_stmts(body)
912913 if stmts.length > 0
913- return infer_type(stmts.last)
914+ then_type = infer_type(stmts.last)
915+ end
916+ end
917+ else_type = "nil"
918+ sub = @nd_subsequent[nid]
919+ if sub >= 0
920+ if @nd_type[sub] == "ElseNode"
921+ ebody = @nd_body[sub]
922+ if ebody >= 0
923+ es = get_stmts(ebody)
924+ if es.length > 0
925+ else_type = infer_type(es.last)
926+ end
927+ end
928+ else
929+ # elsif chain — recurse
930+ else_type = infer_type(sub)
914931 end
915932 end
916- return "void"
933+ types = "".split(",")
934+ types.push(then_type)
935+ types.push(else_type)
936+ return unify_return_type(types)
917937 end
918938 if t == "CaseMatchNode"
919939 # Infer from first in branch