Skip to content

Commit

Permalink
fix no return analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
saem committed Sep 7, 2024
1 parent 3b07bd2 commit 7cf6b7d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
22 changes: 18 additions & 4 deletions compiler/ast/ast_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,25 @@ proc endsInNoReturn*(n: PNode): bool =
## etc.) or a call of a noreturn proc. This is meant to be called on a
## semmed `n`.
var it = n
while it.kind in {nkStmtList, nkStmtListExpr} and it.len > 0:
it = it.lastSon
while it.kind in {nkStmtList, nkStmtListExpr} and it.len > 0 or
it.kind in {nkIfStmt, nkCaseStmt, nkBlockStmt, nkTryStmt} and it.typ.isEmptyType:
case it.kind
of nkStmtList, nkStmtListExpr, nkBlockStmt:
it = it.lastSon
of nkIfStmt, nkCaseStmt:
it = it.lastSon.lastSon
of nkTryStmt:
it =
case it[^1].kind
of nkFinally:
it[^2]
of nkExceptBranch:
it[^1]
of nkAllNodeKinds - {nkFinally, nkExceptBranch}:
unreachable()
else:
unreachable()
result = it.kind in nkLastBlockStmts or
(it.kind in {nkIfStmt, nkTryStmt, nkCaseStmt, nkBlockStmt} and
it.typ.isEmptyType()) or
it.kind in nkCallKinds and it[0].kind == nkSym and sfNoReturn in it[0].sym.flags

type
Expand Down
14 changes: 14 additions & 0 deletions tests/lang_exprs/tnoreturn_nested_reject.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
discard """
description: '''
Ensure nested non-noreturn statements aren't treated as such
'''
"""

let x =
if true:
10

Check failure on line 9 in tests/lang_exprs/tnoreturn_nested_reject.nim

View workflow job for this annotation

GitHub Actions / Test the compiler and stdlib (Linux, batch #1)

expression '10' is of type 'int literal(10)' and has to be used (or discarded)

Check failure on line 9 in tests/lang_exprs/tnoreturn_nested_reject.nim

View workflow job for this annotation

GitHub Actions / Test the compiler and stdlib (macOS (M1), batch #1)

expression '10' is of type 'int literal(10)' and has to be used (or discarded)

Check failure on line 9 in tests/lang_exprs/tnoreturn_nested_reject.nim

View workflow job for this annotation

GitHub Actions / Test the compiler and standard library (Batch 1)

expression '10' is of type 'int literal(10)' and has to be used (or discarded)
else:
try:
discard
except:
discard

0 comments on commit 7cf6b7d

Please sign in to comment.