allow qualifying macro pragmas (#23985)

fixes #12696
This commit is contained in:
metagn
2024-08-20 22:27:55 +03:00
committed by GitHub
parent eed9cb0d3f
commit 6320b0cd5b
4 changed files with 36 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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