summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2022-10-29 08:20:04 +0300
committerGitHub <noreply@github.com>2022-10-29 07:20:04 +0200
commitcb3af8ad397b986699a36d1fae9052bf11250b06 (patch)
tree93361467b6e571410db683739cf42b79be9ac2bc
parent9c3faa449b5ff4856106792f0fa8aa80a7cb5708 (diff)
downloadNim-cb3af8ad397b986699a36d1fae9052bf11250b06.tar.gz
alternate fix + test for #12094, refs #13804 (#20686)
-rw-r--r--compiler/semstmts.nim3
-rw-r--r--compiler/semtempl.nim2
-rw-r--r--tests/template/tunderscore1.nim11
3 files changed, 13 insertions, 3 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index 81be67725..596995adb 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -370,8 +370,7 @@ proc addToVarSection(c: PContext; result: var 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 6e824195a..008226c27 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 000000000..71af39501
--- /dev/null
+++ b/tests/template/tunderscore1.nim
@@ -0,0 +1,11 @@
+discard """
+  errormsg: "undeclared identifier: '_'"
+"""
+
+# issue #12094, #13804
+
+template foo =
+  let _ = 1
+  echo _
+
+foo()