Skip to content

fix new type inference for noreturn [backport] #22182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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