Commit 0fd2b126

0fd2b126507dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: CaseNode/CaseMatchNode type inference unifies all branch types

Previously only the first when/in branch was used for type inference. Now all branches plus the else clause are collected and unified via unify_return_type, matching the IfNode improvement.

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

Details

Commit
0fd2b126507d
Parent
edc12fed179c

spinel_codegen.rbmodifiedView file

@@ -936,36 +936,66 @@
936936 return unify_return_type(types)
937937 end
938938 if t == "CaseMatchNode"
939- # Infer from first in branch
939+ types = "".split(",")
940940 conds = parse_id_list(@nd_conditions[nid])
941- if conds.length > 0
942- inid = conds.first
941+ k = 0
942+ while k < conds.length
943+ inid = conds[k]
943944 if @nd_type[inid] == "InNode"
944945 ibody = @nd_body[inid]
945946 if ibody >= 0
946947 is = get_stmts(ibody)
947948 if is.length > 0
948- return infer_type(is.last)
949+ types.push(infer_type(is.last))
949950 end
950951 end
951952 end
953+ k = k + 1
954+ end
955+ ec = @nd_else_clause[nid]
956+ if ec >= 0
957+ ebody = @nd_body[ec]
958+ if ebody >= 0
959+ es = get_stmts(ebody)
960+ if es.length > 0
961+ types.push(infer_type(es.last))
962+ end
963+ end
964+ end
965+ if types.length > 0
966+ return unify_return_type(types)
952967 end
953968 return "int"
954969 end
955970 if t == "CaseNode"
956- # Infer from first when branch
971+ types = "".split(",")
957972 conds = parse_id_list(@nd_conditions[nid])
958- if conds.length > 0
959- wid = conds.first
973+ k = 0
974+ while k < conds.length
975+ wid = conds[k]
960976 if @nd_type[wid] == "WhenNode"
961977 wbody = @nd_body[wid]
962978 if wbody >= 0
963979 ws = get_stmts(wbody)
964980 if ws.length > 0
965- return infer_type(ws.last)
981+ types.push(infer_type(ws.last))
966982 end
967983 end
968984 end
985+ k = k + 1
986+ end
987+ ec = @nd_else_clause[nid]
988+ if ec >= 0
989+ ebody = @nd_body[ec]
990+ if ebody >= 0
991+ es = get_stmts(ebody)
992+ if es.length > 0
993+ types.push(infer_type(es.last))
994+ end
995+ end
996+ end
997+ if types.length > 0
998+ return unify_return_type(types)
969999 end
9701000 return "int"
9711001 end