Skip to content

Commit b35942e

Browse files
authored
fix new type inference for noreturn [backport] (#22182)
fixes #22180 Backported since apparently the new type inference was backported
1 parent d195877 commit b35942e

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

compiler/semstmts.nim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@ proc semIf(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil):
187187
it[0] = forceBool(c, semExprWithType(c, it[0], expectedType = getSysType(c.graph, n.info, tyBool)))
188188
it[1] = semExprBranch(c, it[1], flags, expectedType)
189189
typ = commonType(c, typ, it[1])
190-
expectedType = typ
190+
if not endsInNoReturn(it[1]):
191+
expectedType = typ
191192
closeScope(c)
192193
elif it.len == 1:
193194
hasElse = true
194195
it[0] = semExprBranchScope(c, it[0], expectedType)
195196
typ = commonType(c, typ, it[0])
196-
expectedType = typ
197+
if not endsInNoReturn(it[0]):
198+
expectedType = typ
197199
else: illFormedAst(it, c.config)
198200
if isEmptyType(typ) or typ.kind in {tyNil, tyUntyped} or
199201
(not hasElse and efInTypeof notin flags):
@@ -234,7 +236,8 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
234236
var expectedType = expectedType
235237
n[0] = semExprBranchScope(c, n[0], expectedType)
236238
typ = commonType(c, typ, n[0].typ)
237-
expectedType = typ
239+
if not endsInNoReturn(n[0]):
240+
expectedType = typ
238241

239242
var last = n.len - 1
240243
var catchAllExcepts = 0
@@ -295,7 +298,8 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
295298
if a.kind != nkFinally:
296299
a[^1] = semExprBranchScope(c, a[^1], expectedType)
297300
typ = commonType(c, typ, a[^1])
298-
expectedType = typ
301+
if not endsInNoReturn(a[^1]):
302+
expectedType = typ
299303
else:
300304
a[^1] = semExprBranchScope(c, a[^1])
301305
dec last
@@ -1145,7 +1149,8 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
11451149
var last = x.len-1
11461150
x[last] = semExprBranchScope(c, x[last], expectedType)
11471151
typ = commonType(c, typ, x[last])
1148-
expectedType = typ
1152+
if not endsInNoReturn(x[last]):
1153+
expectedType = typ
11491154
of nkElifBranch:
11501155
if hasElse: invalidOrderOfBranches(x)
11511156
chckCovered = false
@@ -1154,13 +1159,15 @@ proc semCase(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil
11541159
x[0] = forceBool(c, semExprWithType(c, x[0], expectedType = getSysType(c.graph, n.info, tyBool)))
11551160
x[1] = semExprBranch(c, x[1], expectedType = expectedType)
11561161
typ = commonType(c, typ, x[1])
1157-
expectedType = typ
1162+
if not endsInNoReturn(x[1]):
1163+
expectedType = typ
11581164
closeScope(c)
11591165
of nkElse:
11601166
checkSonsLen(x, 1, c.config)
11611167
x[0] = semExprBranchScope(c, x[0], expectedType)
11621168
typ = commonType(c, typ, x[0])
1163-
expectedType = typ
1169+
if not endsInNoReturn(x[0]):
1170+
expectedType = typ
11641171
if (chckCovered and covered == toCover(c, n[0].typ)) or hasElse:
11651172
message(c.config, x.info, warnUnreachableElse)
11661173
hasElse = true

tests/types/ttopdowninference.nim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,38 @@ block: # bug #21377
290290
{:}
291291

292292
doAssert b(0) == {:}
293+
294+
block: # bug #22180
295+
type A = object
296+
proc j() = discard
297+
298+
let x =
299+
if false:
300+
(ref A)(nil)
301+
else:
302+
if false:
303+
quit 1
304+
else:
305+
if true:
306+
j()
307+
nil # compiles with (ref A)(nil) here
308+
else:
309+
(ref A)(nil)
310+
doAssert x.isNil
311+
312+
let y =
313+
case true
314+
of false:
315+
(ref A)(nil)
316+
else:
317+
case true
318+
of false:
319+
quit 1
320+
else:
321+
case true
322+
of true:
323+
j()
324+
nil # compiles with (ref A)(nil) here
325+
else:
326+
(ref A)(nil)
327+
doAssert y.isNil

0 commit comments

Comments
 (0)