summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-01-18 14:42:03 +0100
committerAraq <rumpf_a@web.de>2015-01-18 14:44:16 +0100
commit5f6131cbdc8d1a09e91c7ef7a2268d84d6c7c4a6 (patch)
treeba795d70186557d7b5492efe68f21f09946db15b
parentd3a8d5b1736cc56fba83c185dfeb5db7b74ad6b4 (diff)
downloadNim-5f6131cbdc8d1a09e91c7ef7a2268d84d6c7c4a6.tar.gz
fixes #1944
-rw-r--r--compiler/lookups.nim10
-rw-r--r--compiler/semexprs.nim18
-rw-r--r--compiler/semtypes.nim11
-rw-r--r--tests/macros/tmacro_in_template.nim10
4 files changed, 36 insertions, 13 deletions
diff --git a/compiler/lookups.nim b/compiler/lookups.nim
index fa1837296..29be693dd 100644
--- a/compiler/lookups.nim
+++ b/compiler/lookups.nim
@@ -82,6 +82,16 @@ proc searchInScopes*(c: PContext, s: PIdent): PSym =
     if result != nil: return
   result = nil
 
+proc debugScopes*(c: PContext) {.deprecated.} =
+  var i = 0
+  for scope in walkScopes(c.currentScope):
+    echo "scope ", i
+    for h in 0 .. high(scope.symbols.data):
+      if scope.symbols.data[h] != nil:
+        echo scope.symbols.data[h].name.s
+    if i == 2: break
+    inc i
+
 proc searchInScopes*(c: PContext, s: PIdent, filter: TSymKinds): PSym =
   for scope in walkScopes(c.currentScope):
     result = strTableGet(scope.symbols, s)
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 8f958c9bf..d579abd2b 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -113,12 +113,18 @@ proc semSym(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
       if s.typ.kind == tyStatic and s.typ.n != nil:
         # XXX see the hack in sigmatch.nim ...
         return s.typ.n
-      elif sfGenSym in s.flags and c.p.wasForwarded:
-        # gensym'ed parameters that nevertheless have been forward declared
-        # need a special fixup:
-        let realParam = c.p.owner.typ.n[s.position+1]
-        internalAssert realParam.kind == nkSym and realParam.sym.kind == skParam
-        return newSymNode(c.p.owner.typ.n[s.position+1].sym, n.info)
+      elif sfGenSym in s.flags:
+        if c.p.wasForwarded:
+          # gensym'ed parameters that nevertheless have been forward declared
+          # need a special fixup:
+          let realParam = c.p.owner.typ.n[s.position+1]
+          internalAssert realParam.kind == nkSym and realParam.sym.kind == skParam
+          return newSymNode(c.p.owner.typ.n[s.position+1].sym, n.info)
+        elif c.p.owner.kind == skMacro:
+          # gensym'ed macro parameters need a similar hack (see bug #1944):
+          var u = searchInScopes(c, s.name)
+          internalAssert u != nil and u.kind == skParam and u.owner == s.owner
+          return newSymNode(u, n.info)
     result = newSymNode(s, n.info)
     # We cannot check for access to outer vars for example because it's still
     # not sure the symbol really ends up being used:
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index fba2c6246..da892dea8 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -663,25 +663,22 @@ proc findEnforcedStaticType(t: PType): PType =
       if t != nil: return t
 
 proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
-  template addDecl(x) =
-    if sfGenSym notin x.flags: addDecl(c, x)
-
   if kind == skMacro:
     let staticType = findEnforcedStaticType(param.typ)
     if staticType != nil:
       var a = copySym(param)
       a.typ = staticType.base
-      addDecl(a)
+      addDecl(c, a)
     elif param.typ.kind == tyTypeDesc:
-      addDecl(param)
+      addDecl(c, param)
     else:
       # within a macro, every param has the type PNimrodNode!
       let nn = getSysSym"PNimrodNode"
       var a = copySym(param)
       a.typ = nn.typ
-      addDecl(a)
+      addDecl(c, a)
   else:
-    addDecl(param)
+    if sfGenSym notin param.flags: addDecl(c, param)
 
 let typedescId = getIdent"typedesc"
 
diff --git a/tests/macros/tmacro_in_template.nim b/tests/macros/tmacro_in_template.nim
new file mode 100644
index 000000000..8f7753cea
--- /dev/null
+++ b/tests/macros/tmacro_in_template.nim
@@ -0,0 +1,10 @@
+
+# bug #1944
+import macros
+
+template t(e: expr): stmt =
+  macro m(eNode: expr): stmt =
+    echo eNode.treeRepr
+  m e
+
+t 5