Skip to content

Commit

Permalink
fix new type inference for noreturn [backport] (#22182)
Browse files Browse the repository at this point in the history
fixes #22180

Backported since apparently the new type inference was backported
  • Loading branch information
metagn authored Jun 28, 2023
1 parent d195877 commit b35942e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
21 changes: 14 additions & 7 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ proc semIf(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil):
it[0] = forceBool(c, semExprWithType(c, it[0], expectedType = getSysType(c.graph, n.info, tyBool)))
it[1] = semExprBranch(c, it[1], flags, expectedType)
typ = commonType(c, typ, it[1])
expectedType = typ
if not endsInNoReturn(it[1]):
expectedType = typ
closeScope(c)
elif it.len == 1:
hasElse = true
it[0] = semExprBranchScope(c, it[0], expectedType)
typ = commonType(c, typ, it[0])
expectedType = typ
if not endsInNoReturn(it[0]):
expectedType = typ
else: illFormedAst(it, c.config)
if isEmptyType(typ) or typ.kind in {tyNil, tyUntyped} or
(not hasElse and efInTypeof notin flags):
Expand Down Expand Up @@ -234,7 +236,8 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
var expectedType = expectedType
n[0] = semExprBranchScope(c, n[0], expectedType)
typ = commonType(c, typ, n[0].typ)
expectedType = typ
if not endsInNoReturn(n[0]):
expectedType = typ

var last = n.len - 1
var catchAllExcepts = 0
Expand Down Expand Up @@ -295,7 +298,8 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
if a.kind != nkFinally:
a[^1] = semExprBranchScope(c, a[^1], expectedType)
typ = commonType(c, typ, a[^1])
expectedType = typ
if not endsInNoReturn(a[^1]):
expectedType = typ
else:
a[^1] = semExprBranchScope(c, a[^1])
dec last
Expand Down Expand Up @@ -1145,7 +1149,8 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
var last = x.len-1
x[last] = semExprBranchScope(c, x[last], expectedType)
typ = commonType(c, typ, x[last])
expectedType = typ
if not endsInNoReturn(x[last]):
expectedType = typ
of nkElifBranch:
if hasElse: invalidOrderOfBranches(x)
chckCovered = false
Expand All @@ -1154,13 +1159,15 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
x[0] = forceBool(c, semExprWithType(c, x[0], expectedType = getSysType(c.graph, n.info, tyBool)))
x[1] = semExprBranch(c, x[1], expectedType = expectedType)
typ = commonType(c, typ, x[1])
expectedType = typ
if not endsInNoReturn(x[1]):
expectedType = typ
closeScope(c)
of nkElse:
checkSonsLen(x, 1, c.config)
x[0] = semExprBranchScope(c, x[0], expectedType)
typ = commonType(c, typ, x[0])
expectedType = typ
if not endsInNoReturn(x[0]):
expectedType = typ
if (chckCovered and covered == toCover(c, n[0].typ)) or hasElse:
message(c.config, x.info, warnUnreachableElse)
hasElse = true
Expand Down
35 changes: 35 additions & 0 deletions tests/types/ttopdowninference.nim
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,38 @@ block: # bug #21377
{:}

doAssert b(0) == {:}

block: # bug #22180
type A = object
proc j() = discard

let x =
if false:
(ref A)(nil)
else:
if false:
quit 1
else:
if true:
j()
nil # compiles with (ref A)(nil) here
else:
(ref A)(nil)
doAssert x.isNil

let y =
case true
of false:
(ref A)(nil)
else:
case true
of false:
quit 1
else:
case true
of true:
j()
nil # compiles with (ref A)(nil) here
else:
(ref A)(nil)
doAssert y.isNil

0 comments on commit b35942e

Please sign in to comment.