summary refs log tree commit diff stats
path: root/tests/lookups/tgensymgeneric.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lookups/tgensymgeneric.nim')
-rw-r--r--tests/lookups/tgensymgeneric.nim54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/lookups/tgensymgeneric.nim b/tests/lookups/tgensymgeneric.nim
new file mode 100644
index 000000000..6e1df4842
--- /dev/null
+++ b/tests/lookups/tgensymgeneric.nim
@@ -0,0 +1,54 @@
+discard """
+  output: '''true'''
+"""
+
+# We need to open the gensym'ed symbol again so that the instantiation
+# creates a fresh copy; but this is wrong the very first reason for gensym
+# is that scope rules cannot be used! So simply removing 'sfGenSym' does
+# not work. Copying the symbol does not work either because we're already
+# the owner of the symbol! What we need to do is to copy the symbol
+# in the generic instantiation process...
+
+type
+  TA = object
+    x: int
+  TB = object
+    x: string
+
+template genImpl() =
+  var gensymed: T
+  when T is TB:
+    gensymed.x = "abc"
+  else:
+    gensymed.x = 123
+  result = move gensymed
+
+proc gen[T](x: T): T =
+  genImpl()
+
+var
+  a: TA
+  b: TB
+let x = gen(a)
+let y = gen(b)
+
+doAssert x.x == 123
+doAssert y.x == "abc"
+
+# test symbol equality
+
+import macros
+
+static:
+  let sym1 = genSym()
+  let sym2 = genSym()
+  let sym3 = sym1
+  let nimsym = sym1.symbol
+  doAssert sym1 == sym1
+  doAssert sym2 != sym3
+  doAssert sym2.symbol != sym3.symbol
+  doAssert sym3 == sym1
+  doAssert sym1.symbol == sym1.symbol
+  doAssert nimsym == nimsym
+
+echo "true"