Skip to content

untyped macro nested in template: getting sym instead of idents #11091

Open
@mratsim

Description

@mratsim

When an untyped macro is nested in a template, the identifiers are transformed into nnkSym instead of being left as raw nnkIdent. We also get the OpenSymChoice instead of nnkBracketExpr.

Test case:

import macros

macro foo(body: untyped): untyped =
  echo body.treerepr
  body.expectKind nnkStmtList
  body[0].expectKind nnkProcDef
  body[0][3].expectKind nnkFormalParams
  body[0][3][1].expectKind nnkIdentDefs
  assert body[0][3][1][0].eqIdent "a"
  body[0][3][1][0].expectKind nnkIdent

  echo "Success!\n##############\n"
  result = newStmtList()


foo:
  proc bar[T](a, b: seq[T]): seq[T]

####################################

template baz(): untyped =
  foo:
    proc bar[T](a, b: seq[T]): seq[T]

baz()

First parsing output:

StmtList
  ProcDef
    Ident "bar"
    Empty
    GenericParams
      IdentDefs
        Ident "T"
        Empty
        Empty
    FormalParams
      BracketExpr
        Ident "seq"
        Ident "T"
      IdentDefs
        Ident "a"
        Ident "b"
        BracketExpr
          Ident "seq"
          Ident "T"
        Empty
    Empty
    Empty
    Empty

Second:

StmtList
  ProcDef
    Ident "bar"
    Empty
    GenericParams
      IdentDefs
        Ident "T"
        Empty
        Empty
    FormalParams
      Call
        OpenSymChoice
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
          Sym "[]"
        Sym "seq"
        Ident "T"
      IdentDefs
        Sym "a"
        Sym "b"
        Call
          OpenSymChoice
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
            Sym "[]"
          Sym "seq"
          Ident "T"
        Empty
    Empty
    Empty
    Empty

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions