Fix #19224 For loops over a hardcoded empty array crash the compiler (#20476)

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

* Update t19224.nim

(cherry picked from commit 567c3f055d)
This commit is contained in:
Bung
2022-10-02 05:19:07 +08:00
committed by narimiran
parent 512aef60ae
commit 0cf39669df
2 changed files with 15 additions and 0 deletions

View File

@@ -767,6 +767,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:

12
tests/errmsgs/t19224.nim Normal file
View File

@@ -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