fix #20067, fix #18976 [backport] (#20069)

(cherry picked from commit 685bf944aa)
This commit is contained in:
metagn
2022-07-22 10:04:07 +03:00
committed by narimiran
parent 5771a0f9c4
commit 7d0bfc6725
2 changed files with 31 additions and 1 deletions

View File

@@ -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, "$"

28
tests/macros/t20067.nim Normal file
View File

@@ -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)