This commit is contained in:
metagn
2023-04-26 09:02:44 +03:00
committed by GitHub
parent f0ae1ed544
commit 0032322ea8
3 changed files with 28 additions and 12 deletions

View File

@@ -2019,6 +2019,9 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
s.ast = n
s.options = c.config.options
#s.scope = c.currentScope
if s.kind in {skMacro, skTemplate}:
# push noalias flag at first to prevent unwanted recursive calls:
incl(s.flags, sfNoalias)
# before compiling the proc params & body, set as current the scope
# where the proc was declared
@@ -2335,16 +2338,16 @@ proc semMacroDef(c: PContext, n: PNode): PNode =
var s = result[namePos].sym
var t = s.typ
var allUntyped = true
var requiresParams = false
var nullary = true
for i in 1..<t.n.len:
let param = t.n[i].sym
if param.typ.kind != tyUntyped: allUntyped = false
# no default value, parameters required in call
if param.ast == nil: requiresParams = true
if param.ast == nil: nullary = false
if allUntyped: incl(s.flags, sfAllUntyped)
if requiresParams or n[genericParamsPos].kind != nkEmpty:
# macro cannot be called with alias syntax
incl(s.flags, sfNoalias)
if nullary and n[genericParamsPos].kind == nkEmpty:
# macro can be called with alias syntax, remove pushed noalias flag
excl(s.flags, sfNoalias)
if n[bodyPos].kind == nkEmpty:
localError(c.config, n.info, errImplOfXexpected % s.name.s)

View File

@@ -626,6 +626,8 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
onDef(n[namePos].info, s)
# check parameter list:
#s.scope = c.currentScope
# push noalias flag at first to prevent unwanted recursive calls:
incl(s.flags, sfNoalias)
pushOwner(c, s)
openScope(c)
n[namePos] = newSymNode(s)
@@ -635,7 +637,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
setGenericParamsMisc(c, n)
# process parameters:
var allUntyped = true
var requiresParams = false
var nullary = true
if n[paramsPos].kind != nkEmpty:
semParamList(c, n[paramsPos], n[genericParamsPos], s)
# a template's parameters are not gensym'ed even if that was originally the
@@ -648,7 +650,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
param.flags.excl sfGenSym
if param.typ.kind != tyUntyped: allUntyped = false
# no default value, parameters required in call
if param.ast == nil: requiresParams = true
if param.ast == nil: nullary = false
else:
s.typ = newTypeS(tyProc, c)
# XXX why do we need tyTyped as a return type again?
@@ -660,11 +662,11 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
n[genericParamsPos] = n[miscPos][1]
n[miscPos] = c.graph.emptyNode
if allUntyped: incl(s.flags, sfAllUntyped)
if requiresParams or
n[bodyPos].kind == nkEmpty or
n[genericParamsPos].kind != nkEmpty:
# template cannot be called with alias syntax
incl(s.flags, sfNoalias)
if nullary and
n[genericParamsPos].kind == nkEmpty and
n[bodyPos].kind != nkEmpty:
# template can be called with alias syntax, remove pushed noalias flag
excl(s.flags, sfNoalias)
if n[patternPos].kind != nkEmpty:
n[patternPos] = semPattern(c, n[patternPos], s)

View File

@@ -61,3 +61,14 @@ block: # issue #13515
if not test:
doAssert false
x
import macros
block: # issue #21727
template debugAnnotation(s: typed): string =
astToStr s
macro cpsJump(x: int): untyped =
result = newLit(debugAnnotation(cpsJump))
doAssert cpsJump(13) == "cpsJump"