Skip to content

Commit

Permalink
allow qualifying macro pragmas (#23985)
Browse files Browse the repository at this point in the history
fixes #12696
  • Loading branch information
metagn authored Aug 20, 2024
1 parent eed9cb0 commit 6320b0c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
10 changes: 5 additions & 5 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,15 @@ proc setVarType(c: PContext; v: PSym, typ: PType) =
proc isPossibleMacroPragma(c: PContext, it: PNode, key: PNode): bool =
# make sure it's not a normal pragma, and calls an identifier
# considerQuotedIdent below will fail on non-identifiers
result = whichPragma(it) == wInvalid and key.kind in nkIdentKinds
result = whichPragma(it) == wInvalid and key.kind in nkIdentKinds+{nkDotExpr}
if result:
# make sure it's not a user pragma
let ident = considerQuotedIdent(c, key)
result = strTableGet(c.userPragmas, ident) == nil
if key.kind != nkDotExpr:
let ident = considerQuotedIdent(c, key)
result = strTableGet(c.userPragmas, ident) == nil
if result:
# make sure it's not a custom pragma
var amb = false
let sym = searchInScopes(c, ident, amb)
let sym = qualifiedLookUp(c, key, {})
result = sym == nil or sfCustomPragma notin sym.flags

proc copyExcept(n: PNode, i: int): PNode =
Expand Down
11 changes: 7 additions & 4 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1770,12 +1770,15 @@ proc applyTypeSectionPragmas(c: PContext; pragmas, operand: PNode): PNode =
discard "builtin pragma"
else:
trySuggestPragmas(c, key)
let ident = considerQuotedIdent(c, key)
if strTableGet(c.userPragmas, ident) != nil:
let ident =
if key.kind in nkIdentKinds:
considerQuotedIdent(c, key)
else:
nil
if ident != nil and strTableGet(c.userPragmas, ident) != nil:
discard "User-defined pragma"
else:
var amb = false
let sym = searchInScopes(c, ident, amb)
let sym = qualifiedLookUp(c, key, {})
# XXX: What to do here if amb is true?
if sym != nil and sfCustomPragma in sym.flags:
discard "Custom user pragma"
Expand Down
10 changes: 10 additions & 0 deletions tests/pragmas/mqualifiedmacro.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
template t*(x:untyped): untyped =
echo "template t"

import macros
macro m*(name: static string, x: untyped): untyped =
let newName = ident(name)
result = quote do:
type `newName` = object
if result.kind == nnkStmtList:
result = result[^1]
14 changes: 14 additions & 0 deletions tests/pragmas/tqualifiedmacro.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
discard """
output: '''
template t
'''
"""

# issue #12696

import mqualifiedmacro
proc p() {. mqualifiedmacro.t .} = # errors with identifier expected but a.t found
echo "proc p"

type Foo {. mqualifiedmacro.m("Bar") .} = object
doAssert Bar is object

0 comments on commit 6320b0c

Please sign in to comment.