summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2022-09-28 16:05:01 +0300
committerGitHub <noreply@github.com>2022-09-28 15:05:01 +0200
commitde4b0346bdafab6c38b77d430d0e83f95da0582c (patch)
tree7ec601bf0f57ccf2f6d1ad5892f14eafb2c406df /tests
parentfdc6b0fb6e1b7272f32f177da4e44d9055c00b67 (diff)
downloadNim-de4b0346bdafab6c38b77d430d0e83f95da0582c.tar.gz
store full definition AST for consts, fix noRewrite (#20115)
* continue #9582 for consts, close #9331, fix #20114

also move extractPragma to ast to pave the way for things like {.strdefine: "abc".} etc

* changelog correctly

* fix jsgen

* update tgetimpl

* fix sighashes

* fix #19766, add comment about postfix

* fix noRewrite LOL

refs #16620

* fix changelog

* fix destructors
Diffstat (limited to 'tests')
-rw-r--r--tests/deprecated/tconst.nim8
-rw-r--r--tests/macros/t19766_20114.nim16
-rw-r--r--tests/macros/tgetimpl.nim2
-rw-r--r--tests/pragmas/tcustom_pragma.nim40
-rw-r--r--tests/trmacros/tnorewrite.nim20
-rw-r--r--tests/trmacros/trmacros_various.nim3
6 files changed, 87 insertions, 2 deletions
diff --git a/tests/deprecated/tconst.nim b/tests/deprecated/tconst.nim
new file mode 100644
index 000000000..51eb6cb0e
--- /dev/null
+++ b/tests/deprecated/tconst.nim
@@ -0,0 +1,8 @@
+discard """
+  nimout: '''
+tconst.nim(8, 9) Warning: abcd; foo is deprecated [Deprecated]
+'''
+"""
+
+const foo* {.deprecated: "abcd".} = 42
+discard foo
diff --git a/tests/macros/t19766_20114.nim b/tests/macros/t19766_20114.nim
new file mode 100644
index 000000000..ac336f150
--- /dev/null
+++ b/tests/macros/t19766_20114.nim
@@ -0,0 +1,16 @@
+discard """
+  action: compile
+  nimout: '''
+const
+  foo {.strdefine.} = "abc"
+let hey {.tddd.} = 5
+'''
+"""
+
+import macros
+
+template tddd {.pragma.}
+
+expandMacros:
+  const foo {.strdefine.} = "abc"
+  let hey {.tddd.} = 5
diff --git a/tests/macros/tgetimpl.nim b/tests/macros/tgetimpl.nim
index de132f253..66722a234 100644
--- a/tests/macros/tgetimpl.nim
+++ b/tests/macros/tgetimpl.nim
@@ -1,5 +1,5 @@
 discard """
-  nimout: '''"muhaha"
+  nimout: '''foo = "muhaha"
 proc poo(x, y: int) =
   let y = x
   echo ["poo"]'''
diff --git a/tests/pragmas/tcustom_pragma.nim b/tests/pragmas/tcustom_pragma.nim
index b624f32ba..5a68b7677 100644
--- a/tests/pragmas/tcustom_pragma.nim
+++ b/tests/pragmas/tcustom_pragma.nim
@@ -439,3 +439,43 @@ when false:
 
   # left-to-right priority/override order for getCustomPragmaVal
   assert bb.getCustomPragmaVal(hehe) == (key: "hi", val: "hu", haha: "he")
+
+{.experimental: "dynamicBindSym".}
+
+# const
+block:
+  template myAttr() {.pragma.}
+  template myAttr2(x: int) {.pragma.}
+  template myAttr3(x: string) {.pragma.}
+
+  type
+    MyObj2 = ref object
+
+  const a {.myAttr,myAttr2(2),myAttr3:"test".}: int = 0
+  const b {.myAttr,myAttr2(2),myAttr3:"test".} = 0
+
+  macro forceHasCustomPragma(x: untyped, y: typed): untyped =
+    var x = bindSym(x.repr)
+    for c in x:
+      if c.symKind == nskConst:
+        x = c
+        break
+    result = getAst(hasCustomPragma(x, y))
+
+  macro forceGetCustomPragmaVal(x: untyped, y: typed): untyped =
+    var x = bindSym(x.repr)
+    for c in x:
+      if c.symKind == nskConst:
+        x = c
+        break
+    result = getAst(getCustomPragmaVal(x, y))
+
+  template check(s: untyped) =
+    doAssert forceHasCustomPragma(s, myAttr)
+    doAssert forceHasCustomPragma(s, myAttr2)
+    doAssert forceGetCustomPragmaVal(s, myAttr2) == 2
+    doAssert forceHasCustomPragma(s, myAttr3)
+    doAssert forceGetCustomPragmaVal(s, myAttr3) == "test"
+
+  check(a)
+  check(b)
diff --git a/tests/trmacros/tnorewrite.nim b/tests/trmacros/tnorewrite.nim
new file mode 100644
index 000000000..e6769246f
--- /dev/null
+++ b/tests/trmacros/tnorewrite.nim
@@ -0,0 +1,20 @@
+block:
+  proc get(x: int): int = x
+
+  template t{get(a)}(a: int): int =
+    {.noRewrite.}:
+      get(a) + 1
+
+  doAssert get(0) == 1
+
+block:
+  var x: int
+
+  template asgn{a = b}(a: int{lvalue}, b: int) =
+    let newVal = b + 1
+    # ^ this is needed but should it be?
+    {.noRewrite.}:
+      a = newVal
+
+  x = 10
+  doAssert x == 11, $x
diff --git a/tests/trmacros/trmacros_various.nim b/tests/trmacros/trmacros_various.nim
index 74b248739..8fe51e548 100644
--- a/tests/trmacros/trmacros_various.nim
+++ b/tests/trmacros/trmacros_various.nim
@@ -33,7 +33,8 @@ block tcse:
 
 block hoist:
   template optPeg{peg(pattern)}(pattern: string{lit}): Peg =
-    var gl {.global, gensym.} = peg(pattern)
+    {.noRewrite.}:
+      var gl {.global, gensym.} = peg(pattern)
     gl
   doAssert match("(a b c)", peg"'(' @ ')'")
   doAssert match("W_HI_Le", peg"\y 'while'")