From 0869d2a47777685d221ddde927f0175096c911b0 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Wed, 18 Nov 2020 08:14:14 +0800 Subject: [PATCH] fix #15972 (#15994) * fix #15972 * add testcase * more --- compiler/semstmts.nim | 4 ++-- .../stmt/tforloop_tuple_multiple_underscore.nim | 10 ++++++++++ tests/stmt/tforloop_tuple_underscore.nim | 16 ++++++++++++++++ tests/stmt/tforloop_underscore.nim | 6 ++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/stmt/tforloop_tuple_multiple_underscore.nim create mode 100644 tests/stmt/tforloop_tuple_underscore.nim create mode 100644 tests/stmt/tforloop_underscore.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index f83864775afef..cd7428b627ea0 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -739,7 +739,7 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode = else: v.typ = iter[i] n[0][i] = newSymNode(v) - if sfGenSym notin v.flags: addDecl(c, v) + if sfGenSym notin v.flags and not isDiscardUnderscore(v): addDecl(c, v) elif v.owner == nil: v.owner = getCurrOwner(c) else: var v = symForVar(c, n[0]) @@ -749,7 +749,7 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode = # for an example: v.typ = iterBase n[0] = newSymNode(v) - if sfGenSym notin v.flags: addDecl(c, v) + if sfGenSym notin v.flags and not isDiscardUnderscore(v): addDecl(c, v) elif v.owner == nil: v.owner = getCurrOwner(c) else: localError(c.config, n.info, errWrongNumberOfVariables) diff --git a/tests/stmt/tforloop_tuple_multiple_underscore.nim b/tests/stmt/tforloop_tuple_multiple_underscore.nim new file mode 100644 index 0000000000000..2afdb0b7706c5 --- /dev/null +++ b/tests/stmt/tforloop_tuple_multiple_underscore.nim @@ -0,0 +1,10 @@ +discard """ + errormsg: "undeclared identifier: '_'" +""" + +iterator iter(): (int, int, int) = + yield (1, 1, 2) + + +for (_, i, _) in iter(): + echo _ \ No newline at end of file diff --git a/tests/stmt/tforloop_tuple_underscore.nim b/tests/stmt/tforloop_tuple_underscore.nim new file mode 100644 index 0000000000000..8cbb0fc10905d --- /dev/null +++ b/tests/stmt/tforloop_tuple_underscore.nim @@ -0,0 +1,16 @@ +discard """ + errormsg: "undeclared identifier: '_'" +""" + +iterator iter(): (int, int) = + yield (1, 2) + yield (3, 4) + yield (1, 2) + yield (3, 4) + yield (1, 2) + yield (3, 4) + + +for (_, i) in iter(): + echo _ + diff --git a/tests/stmt/tforloop_underscore.nim b/tests/stmt/tforloop_underscore.nim new file mode 100644 index 0000000000000..c5b49da778d3c --- /dev/null +++ b/tests/stmt/tforloop_underscore.nim @@ -0,0 +1,6 @@ +discard """ + errormsg: "undeclared identifier: '_'" +""" + +for _ in ["a"]: + echo _