Skip to content

Commit

Permalink
Fix #19224 For loops over a hardcoded empty array crash the compiler (#…
Browse files Browse the repository at this point in the history
…20476)

* Fix #11684 For loops over a hardcoded empty array crash the compiler

* Update t19224.nim
  • Loading branch information
bung87 authored Oct 1, 2022
1 parent 0b16505 commit 567c3f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,9 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode =
var iterAfterVarLent = iter.skipTypes({tyGenericInst, tyAlias, tyLent, tyVar})
# n.len == 3 means that there is one for loop variable
# and thus no tuple unpacking:
if iterAfterVarLent.kind == tyEmpty:
localError(c.config, n[^2].info, "cannot infer element type of $1" %
renderTree(n[^2], {renderNoComments}))
if iterAfterVarLent.kind != tyTuple or n.len == 3:
if n.len == 3:
if n[0].kind == nkVarTuple:
Expand Down
12 changes: 12 additions & 0 deletions tests/errmsgs/t19224.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
discard """
cmd: "nim check --hints:off $file"
errormsg: ""
nimout: '''
t19224.nim(10, 10) Error: cannot infer element type of items([])
t19224.nim(12, 10) Error: cannot infer element type of items(@[])
'''
"""

for _ in []: discard

for _ in @[]: discard

0 comments on commit 567c3f0

Please sign in to comment.