diff options
-rw-r--r-- | compiler/semstmts.nim | 13 | ||||
-rw-r--r-- | compiler/semtempl.nim | 16 | ||||
-rw-r--r-- | tests/template/taliassyntax.nim | 11 |
3 files changed, 28 insertions, 12 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 63b3382a8..d493364ea 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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) diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index 74865ad58..8882e14f3 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -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) diff --git a/tests/template/taliassyntax.nim b/tests/template/taliassyntax.nim index 969a9d144..2d8237d93 100644 --- a/tests/template/taliassyntax.nim +++ b/tests/template/taliassyntax.nim @@ -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" |