summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-10-01 08:53:35 +0200
committerGitHub <noreply@github.com>2018-10-01 08:53:35 +0200
commit3d60f1b0af6fc3d8d9446a87bd26ff054562fa20 (patch)
tree61fd6089fe9e4ff03bce388e635777a1d0b0b1cc
parent5676e032bda9d86232b003797bdb67070af43e28 (diff)
parent6d4503325cc667dcfd18c53cee6ef0d245d9f870 (diff)
downloadNim-3d60f1b0af6fc3d8d9446a87bd26ff054562fa20.tar.gz
Merge pull request #9131 from LemonBoy/fix-9130
Fix regression with runnableExamples in generic expr
-rw-r--r--compiler/semgnrc.nim10
-rw-r--r--tests/generics/t8694.nim31
-rw-r--r--tests/generics/t9130.nim33
3 files changed, 69 insertions, 5 deletions
diff --git a/compiler/semgnrc.nim b/compiler/semgnrc.nim
index 7be0610a2..e1a8390e1 100644
--- a/compiler/semgnrc.nim
+++ b/compiler/semgnrc.nim
@@ -225,7 +225,7 @@ proc semGenericStmt(c: PContext, n: PNode,
     var mixinContext = false
     if s != nil:
       incl(s.flags, sfUsed)
-      mixinContext = s.magic in {mDefined, mDefinedInScope, mCompiles, mRunnableExamples}
+      mixinContext = s.magic in {mDefined, mDefinedInScope, mCompiles}
       let sc = symChoice(c, fn, s, if s.isMixedIn: scForceOpen else: scOpen)
       case s.kind
       of skMacro:
@@ -255,11 +255,11 @@ proc semGenericStmt(c: PContext, n: PNode,
         discard
       of skProc, skFunc, skMethod, skIterator, skConverter, skModule:
         result.sons[0] = sc
-        # do not check of 's.magic==mRoof' here because it might be some
-        # other '^' but after overload resolution the proper one:
-        if ctx.bracketExpr != nil and n.len == 2 and s.name.s == "^":
-          result.add ctx.bracketExpr
         first = 1
+        # We're not interested in the example code during this pass so let's
+        # skip it
+        if s.magic == mRunnableExamples:
+          inc first
       of skGenericParam:
         result.sons[0] = newSymNodeTypeDesc(s, fn.info)
         styleCheckUse(fn.info, s)
diff --git a/tests/generics/t8694.nim b/tests/generics/t8694.nim
new file mode 100644
index 000000000..da6c6dbed
--- /dev/null
+++ b/tests/generics/t8694.nim
@@ -0,0 +1,31 @@
+discard """
+  output: '''
+true
+true
+true
+'''
+"""
+
+when true:
+  # Error: undeclared identifier: '|'
+  proc bar[T](t:T): bool =
+    runnableExamples:
+      type Foo = int | float
+    true
+  echo bar(0)
+
+when true:
+  # ok
+  proc bar(t:int): bool =
+    runnableExamples:
+      type Foo = int | float
+    true
+  echo bar(0)
+
+when true:
+  # Error: undeclared identifier: '|'
+  proc bar(t:typedesc): bool =
+    runnableExamples:
+      type Foo = int | float
+    true
+  echo bar(int)
diff --git a/tests/generics/t9130.nim b/tests/generics/t9130.nim
new file mode 100644
index 000000000..a993bc6b2
--- /dev/null
+++ b/tests/generics/t9130.nim
@@ -0,0 +1,33 @@
+when true:
+  # stack overflow
+  template baz1*(iter: untyped): untyped =
+    runnableExamples:
+      import sugar
+      proc fun(a: proc(x:int): int) = discard
+      baz1(fun(x:int => x))
+    discard
+
+  proc foo1[A](ts: A) =
+    baz1(ts)
+
+when true:
+  # ok
+  template baz2*(iter: untyped): untyped =
+    runnableExamples:
+      import sugar
+      proc fun(a: proc(x:int): int) = discard
+      baz2(fun(x:int => x))
+    discard
+
+  proc foo2(ts: int) =
+    baz2(ts)
+
+when true:
+  # stack overflow
+  template baz3*(iter: untyped): untyped =
+    runnableExamples:
+      baz3(fun(x:int => x))
+    discard
+
+  proc foo3[A](ts: A) =
+    baz3(ts)