summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorVindaar <basti90@gmail.com>2022-11-10 21:42:53 +0100
committerGitHub <noreply@github.com>2022-11-10 15:42:53 -0500
commitcc2b0f01725d69d617ed214cf89b647936b93f80 (patch)
tree8a82d17ce637cd42835cb94a7dc4d447f101ff73 /tests
parenta15872ba9ecd118dc69f2346285d5c039e1efd42 (diff)
downloadNim-cc2b0f01725d69d617ed214cf89b647936b93f80.tar.gz
[sugar] handle HiddenDeref in capture, error at CT if unsupported nnk (#20680)
* [sugar] handle HiddenDeref in capture, error at CT if unsupported nnk

Instead of running into trouble of the `.strVal` access failing, it's
better to error at CT.

* [tests] remove unnecessary import in test case

* improve ident extraction & extend test cases

* [tests] improve tests for `capture` of different types & act. check
Diffstat (limited to 'tests')
-rw-r--r--tests/stdlib/tsugar.nim60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/stdlib/tsugar.nim b/tests/stdlib/tsugar.nim
index 9c1213901..6ef3ae519 100644
--- a/tests/stdlib/tsugar.nim
+++ b/tests/stdlib/tsugar.nim
@@ -6,6 +6,10 @@ x + y = 30
 import std/[sugar, algorithm, random, sets, tables, strutils]
 import std/[syncio, assertions]
 
+type # for capture test, ref #20679
+  FooCapture = ref object
+    x: int
+
 template main() =
   block: # `=>`
     block:
@@ -96,6 +100,62 @@ template main() =
         let foo = i + 1
         doAssert p() == foo
 
+    block: # issue #20679
+      # this should compile. Previously was broken as `var int` is an `nnkHiddenDeref`
+      # which was not handled correctly
+
+      block:
+        var x = 5
+        var s1 = newSeq[proc (): int](2)
+        proc function(data: var int) =
+          for i in 0 ..< 2:
+            data = (i+1) * data
+            capture data:
+              s1[i] = proc(): int = data
+        function(x)
+        doAssert s1[0]() == 5
+        doAssert s1[1]() == 10
+
+
+      block:
+        var y = @[5, 10]
+        var s2 = newSeq[proc (): seq[int]](2)
+        proc functionS(data: var seq[int]) =
+          for i in 0 ..< 2:
+            data.add (i+1) * 5
+            capture data:
+              s2[i] = proc(): seq[int] = data
+        functionS(y)
+        doAssert s2[0]() == @[5, 10, 5]
+        doAssert s2[1]() == @[5, 10, 5, 10]
+
+
+      template typeT(typ, val: untyped): untyped =
+        var x = val
+        var s = newSeq[proc (): typ](2)
+
+        proc functionT[T](data: var T) =
+          for i in 0 ..< 2:
+            if i == 1:
+              data = default(T)
+            capture data:
+              s[i] = proc (): T = data
+
+        functionT(x)
+        doAssert s[0]() == val
+        doAssert s[1]() == x # == default
+        doAssert s[1]() == default(typ)
+
+      block:
+        var x = 1.1
+        typeT(float, x)
+      block:
+        var x = "hello"
+        typeT(string, x)
+      block:
+        var f = FooCapture(x: 5)
+        typeT(FooCapture, f)
+
   block: # dup
     block dup_with_field:
       type