summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAdam Strzelecki <ono@java.pl>2015-10-30 00:27:33 +0100
committerAdam Strzelecki <ono@java.pl>2015-10-30 12:05:02 +0100
commitf838c1baa48a22ef74f431d9b691af179d1c632d (patch)
treef23afb9efab9f4bf1b1d126fe591512c1e9549ed
parent8e4b5e10ba2945f880a857872862bcb28f625594 (diff)
downloadNim-f838c1baa48a22ef74f431d9b691af179d1c632d.tar.gz
fixes #3498
Previously it was not possible to use template arguments in template body as
the symbols were not resolved correctly leading to Error: undeclared
identifier: 'XX', eg.:

   template defaultOf[T](t: T): expr = (var d: T; d)
   echo defaultOf(1) #<- invalid identifier, but should output 0
-rw-r--r--compiler/evaltempl.nim3
-rw-r--r--compiler/semtempl.nim5
-rw-r--r--tests/generics/tgenerictmpl.nim25
3 files changed, 20 insertions, 13 deletions
diff --git a/compiler/evaltempl.nim b/compiler/evaltempl.nim
index c33e5be86..01233274b 100644
--- a/compiler/evaltempl.nim
+++ b/compiler/evaltempl.nim
@@ -38,7 +38,8 @@ proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
     if s.owner.id == c.owner.id:
       if s.kind == skParam and sfGenSym notin s.flags:
         handleParam actual.sons[s.position]
-      elif s.kind == skGenericParam:
+      elif s.kind == skGenericParam or
+           s.kind == skType and s.typ != nil and s.typ.kind == tyGenericParam:
         handleParam actual.sons[s.owner.typ.len + s.position - 1]
       else:
         internalAssert sfGenSym in s.flags
diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim
index 2dda8276d..0dc600983 100644
--- a/compiler/semtempl.nim
+++ b/compiler/semtempl.nim
@@ -228,10 +228,7 @@ proc semTemplSymbol(c: PContext, n: PNode, s: PSym): PNode =
   of skParam:
     result = n
   of skType:
-    if (s.typ != nil) and (s.typ.kind != tyGenericParam):
-      result = newSymNodeTypeDesc(s, n.info)
-    else:
-      result = n
+    result = newSymNodeTypeDesc(s, n.info)
   else:
     result = newSymNode(s, n.info)
 
diff --git a/tests/generics/tgenerictmpl.nim b/tests/generics/tgenerictmpl.nim
index a749e6570..c71ce4e2e 100644
--- a/tests/generics/tgenerictmpl.nim
+++ b/tests/generics/tgenerictmpl.nim
@@ -1,12 +1,21 @@
+discard """
+  output: '''0
+123'''
+"""
 
-template tmp[T](x: var seq[T]) =
-  #var yz: T  # XXX doesn't work yet
-  x = @[1, 2, 3]
+# bug #3498
+
+template defaultOf[T](t: T): expr = (var d: T; d)
+
+echo defaultOf(1) #<- excpected 0
 
-macro tmp2[T](x: var seq[T]): stmt =
-  nil
+# assignment using template
+
+template tassign[T](x: var seq[T]) =
+  x = @[1, 2, 3]
 
 var y: seq[int]
-tmp(y)
-tmp(y)
-echo y.repr
+tassign(y) #<- x is expected = @[1, 2, 3]
+tassign(y)
+
+echo y[0], y[1], y[2]