nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.38k stars 1.47k forks source link

Can't use an imported template as a pragma #14785

Open deech opened 4 years ago

deech commented 4 years ago

Example

# a.nim
template aPragma*() = ...
# b.nim
import a
proc b() {.aPragma.} = ...

I also tried"

proc b() {.a.aPragma.} = ...

Current Output

invalid pragma: aPragma

Expected Output

Successful compilation
$ nim -v
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-06-20
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 79c90b30ee5590b02f8bcfac68ed0c9ef7b38e7d
active boot switches: -d:release
ghost commented 4 years ago

@deech you need to annotate aPragma with {.pragma.} so it can be used as a pragma :)

# a.nim
template aPragma*() {.pragma.}
# b.nim
import a
proc b() {.aPragma.} = discard # works
ghost commented 4 years ago

See https://nim-lang.org/docs/manual.html#userminusdefined-pragmas-custom-annotations

deech commented 4 years ago

Yes, that works. As a side note if you want to use the template for metaprogramming and you leave out the initial argument that takes the body of the proc or you get the same error so you need to do:

template aPragma(body:untyped):untyped = body
proc p() {.aPragma.} = ...

Not totally related to my original issue but might be worth noting in the manual as well.

Another thing I noticed is qualified pragmas don't seem to work even if I have the {.pragma.} pragma, so proc p() {.a.aPragma.} fails with identifier expected but found 'a.aPragma'. Not sure if that's a bug but it surprised me so it might also be worth spelling out in the manual

In any case, thanks! This can be closed

timotheecour commented 4 years ago

it's a common error; i think we should improve the error message before closing