From 0cf39669df6334e4aa1de274d7eac4865719b859 Mon Sep 17 00:00:00 2001 From: Bung Date: Sun, 2 Oct 2022 05:19:07 +0800 Subject: [PATCH] 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 567c3f055ded0d81d15d6d5bc18377ca8c607c6d) --- compiler/semstmts.nim | 3 +++ tests/errmsgs/t19224.nim | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tests/errmsgs/t19224.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index b628bd1c18..7b5b2f0bc9 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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: diff --git a/tests/errmsgs/t19224.nim b/tests/errmsgs/t19224.nim new file mode 100644 index 0000000000..7a9ecb2e70 --- /dev/null +++ b/tests/errmsgs/t19224.nim @@ -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