From 7d0bfc672527ae372effeb3f2c8eaa642eb1005a Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Fri, 22 Jul 2022 10:04:07 +0300 Subject: [PATCH] fix #20067, fix #18976 [backport] (#20069) (cherry picked from commit 685bf944aac0b2ea7ce1360b56080f3d07037fc2) --- lib/core/macros.nim | 4 +++- tests/macros/t20067.nim | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/macros/t20067.nim diff --git a/lib/core/macros.nim b/lib/core/macros.nim index f6ac3aa817..b0f385f022 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -1352,7 +1352,9 @@ proc `$`*(node: NimNode): string = of nnkOpenSymChoice, nnkClosedSymChoice: result = $node[0] of nnkAccQuoted: - result = $node[0] + result = "" + for i in 0 ..< node.len: + result.add(repr(node[i])) else: badNodeKind node, "$" diff --git a/tests/macros/t20067.nim b/tests/macros/t20067.nim new file mode 100644 index 0000000000..0ee3a47123 --- /dev/null +++ b/tests/macros/t20067.nim @@ -0,0 +1,28 @@ +discard """ + output: ''' +b.defaultVal = foo +$c.defaultVal = bar +''' +""" + +import macros + +# #18976 + +macro getString(identifier): string = + result = newLit($identifier) +doAssert getString(abc) == "abc" +doAssert getString(`a b c`) == "abc" + +# #20067 + +template defaultVal*(value : typed) {.pragma.} + +type A = ref object + b {.defaultVal: "foo".}: string + `$c` {.defaultVal: "bar".}: string + +let a = A(b: "a", `$c`: "b") + +echo "b.defaultVal = " & a.b.getCustomPragmaVal(defaultVal) +echo "$c.defaultVal = " & a.`$c`.getCustomPragmaVal(defaultVal)