From d8969465a546826d093dce74b97c8e43eb75a167 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 29 Oct 2022 08:20:04 +0300 Subject: [PATCH] alternate fix + test for #12094, refs #13804 (#20686) (cherry picked from commit cb3af8ad397b986699a36d1fae9052bf11250b06) --- compiler/semstmts.nim | 3 +-- compiler/semtempl.nim | 2 +- tests/template/tunderscore1.nim | 11 +++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 tests/template/tunderscore1.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 826b9dead9aca..7b5b2f0bc9b8e 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -360,8 +360,7 @@ proc addToVarSection(c: PContext; result: PNode; orig, identDefs: PNode) = result.add identDefs proc isDiscardUnderscore(v: PSym): bool = - # template generated underscore symbol name starts with _`gensym - if v.name.s == "_" or v.name.s.startsWith("_`"): + if v.name.s == "_": v.flags.incl(sfGenSym) result = true diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index 4ef20db5d1d69..c7bad18603904 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -227,7 +227,7 @@ proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) = closeScope(c) let ident = getIdentNode(c, n) if not isTemplParam(c, ident): - if n.kind != nkSym: + if n.kind != nkSym and not (n.kind == nkIdent and n.ident.s == "_"): let local = newGenSym(k, ident, c) addPrelimDecl(c.c, local) styleCheckDef(c.c, n.info, local) diff --git a/tests/template/tunderscore1.nim b/tests/template/tunderscore1.nim new file mode 100644 index 0000000000000..71af39501e955 --- /dev/null +++ b/tests/template/tunderscore1.nim @@ -0,0 +1,11 @@ +discard """ + errormsg: "undeclared identifier: '_'" +""" + +# issue #12094, #13804 + +template foo = + let _ = 1 + echo _ + +foo()