From b2b23d723afde51d54a63d1c19e0ae04d6d826c7 Mon Sep 17 00:00:00 2001 From: quantimnot <54247259+quantimnot@users.noreply.github.com> Date: Fri, 5 Mar 2021 08:41:33 -0500 Subject: Fix macros.quote custom op symbol interpolation. (#17256) Provides a workaround/fix for #7589. https://github.com/nim-lang/Nim/issues/7589 Updated docs and tutorial to reflect change. Updated runnableExamples to include an example. Co-authored-by: name --- lib/core/macros.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/core') diff --git a/lib/core/macros.nim b/lib/core/macros.nim index d5925e1ff..491235d8b 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -555,6 +555,9 @@ proc quote*(bl: typed, op = "``"): NimNode {.magic: "QuoteAst", noSideEffect.} = ## operator may be obtained by escaping it (by prefixing it with itself) when used ## as a unary operator: ## e.g. `@` is escaped as `@@`, `&%` is escaped as `&%&%` and so on; see examples. + ## + ## A custom operator interpolation needs accent quoted (``) whenever it resolves + ## to a symbol. runnableExamples: macro check(ex: untyped) = # this is a simplified version of the check macro from the @@ -593,10 +596,11 @@ proc quote*(bl: typed, op = "``"): NimNode {.magic: "QuoteAst", noSideEffect.} = runnableExamples: # custom `op` var destroyCalled = false - macro bar() = + macro bar(ident) = var x = 1.5 result = quote("@") do: type Foo = object + let `@ident` = 0 # custom op interpolated symbols need quoted (``) proc `=destroy`(a: var Foo) = doAssert @x == 1.5 doAssert compiles(@x == 1.5) @@ -607,7 +611,7 @@ proc quote*(bl: typed, op = "``"): NimNode {.magic: "QuoteAst", noSideEffect.} = destroyCalled = true block: let a = Foo() - bar() + bar(someident) doAssert destroyCalled proc `&%`(x: int): int = 1 -- cgit 1.4.1-2-gfad0