Skip to content

Commit 4c16757

Browse files
authored
fix type checker bug with conditional expressions (#6324)
This commit fixes a bug where a conditional expression was causing a fatal error if either branch of the conditional had a type error. The fix is to report an error only when both branches have errors.
1 parent ecdb021 commit 4c16757

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/semantic/checker.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,17 @@ func (c *checker) expr(typ super.Type, e sem.Expr) super.Type {
319319
return c.callFunc(e, types)
320320
case *sem.CondExpr:
321321
c.boolean(e.Cond, c.expr(typ, e.Cond))
322-
return c.fuse([]super.Type{c.expr(typ, e.Then), c.expr(typ, e.Else)})
322+
c.pushErrs()
323+
thenType := c.expr(typ, e.Then)
324+
thenErrs := c.popErrs()
325+
c.pushErrs()
326+
elseType := c.expr(typ, e.Else)
327+
elseErrs := c.popErrs()
328+
if len(thenErrs) != 0 && len(elseErrs) != 0 {
329+
c.error(thenErrs[0].loc, fmt.Errorf("no valid conditional branch found: %w", thenErrs[0].err))
330+
c.error(elseErrs[0].loc, fmt.Errorf("no valid conditional branch found: %w", elseErrs[0].err))
331+
}
332+
return c.fuse([]super.Type{thenType, elseType})
323333
case *sem.DotExpr:
324334
typ, _ := c.deref(e.Node, c.expr(typ, e.LHS), e.RHS)
325335
return typ
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spq: |
2+
values {key:0,val:1}
3+
| values key=0 ? val : val2
4+
5+
output: |
6+
1

0 commit comments

Comments
 (0)