Skip to content
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

Missing type inference in loop + case + if + continue #22604

Closed
arnetheduck opened this issue Aug 31, 2023 · 1 comment · Fixed by #22612
Closed

Missing type inference in loop + case + if + continue #22604

arnetheduck opened this issue Aug 31, 2023 · 1 comment · Fixed by #22612

Comments

@arnetheduck
Copy link
Contributor

Description

broken:

for i in 0..<10:
  let x =
    case false
    of true:
      42
    of false:
      if true:
        continue
      else:
        raiseAssert "Won't get here"

  echo x

works:

for i in 0..<10:
  let x =
    case false
    of true:
      42
    of false:
      if true:
        33
      else:
        raiseAssert "Won't get here"

  echo x

Nim Version

1.6.14

Current Output

testit.nim(12, 7) Error: expression '42' is of type 'int literal(42)' and has to be used (or discarded)

Expected Output

No response

Possible Solution

No response

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Aug 31, 2023

The noreturn checks are not deep IIRC, they just check the last node in a branch, then check if it's a noreturn call or continue/raise/return/break. In this case the last node is an if statement with 2 noreturn branches, which isn't inferred to be noreturn.

Nim/compiler/sem.nim

Lines 209 to 215 in b3912c2

proc endsInNoReturn(n: PNode): bool =
# check if expr ends in raise exception or call of noreturn proc
var it = n
while it.kind in {nkStmtList, nkStmtListExpr} and it.len > 0:
it = it.lastSon
result = it.kind in nkLastBlockStmts or
it.kind in nkCallKinds and it[0].kind == nkSym and sfNoReturn in it[0].sym.flags

Araq pushed a commit that referenced this issue Sep 1, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
narimiran pushed a commit that referenced this issue Sep 15, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
(cherry picked from commit 3b206ed)
narimiran pushed a commit that referenced this issue Sep 15, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
(cherry picked from commit 3b206ed)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
(cherry picked from commit 3b206ed)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
(cherry picked from commit 3b206ed)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* Rewrite endsInNoReturn

* Handle `try` stmt again and add tests

* Fix unreachable code warning

* Remove unreachable code in semexprs again

* Check `it.len` before skip

* Move import of assertions

---------

Co-authored-by: SirOlaf <>
(cherry picked from commit 3b206ed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants