Skip to content

Generic type parameters not recoverable in macro with alias #22937

Open
@arnetheduck

Description

@arnetheduck

Description

In this snippet, I'm looking to extract E from the given instance - however, when going via an alias, there seems to be no way for a macro to recover it - the functions either return the generic type without the specialisation or the alias sym but nothing in between.

import std/[macros]

type
  Future2[T, E] = object

type Y = Future2[void, (int, )]
var v: Y

macro internalCheckComplete*[T, E](f: Future2[T, E]): untyped =
  # for v2, we can get the generic type from getTypeInst(f) - for v (with the
  # alias), this returns `nnkSym("Y")` and nesting combinations of calls all end
  # up in nnkObjectTy - there's no way to get the specialised generic parameters
  echo astGenRepr(getTypeInst(f))
  echo astGenRepr(getType(getTypeInst(f)))
  echo astGenRepr(getType(f))
  echo astGenRepr(getTypeImpl(f))

internalCheckComplete(v)

var v2: Future2[void, (int, )]
internalCheckComplete(v2)

Nim Version

1.6.16, devel

Current Output

newSymNode("Y")
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  nnkRecList.newTree(
  )
)
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  nnkRecList.newTree(
  )
)
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  newEmptyNode()
)
nnkBracketExpr.newTree(
  newSymNode("Future2"),
  newSymNode("void"),
  nnkTupleConstr.newTree(
    newSymNode("int")
  )
)
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  nnkRecList.newTree(
  )
)
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  nnkRecList.newTree(
  )
)
nnkObjectTy.newTree(
  newEmptyNode(),
  newEmptyNode(),
  newEmptyNode()
)

Expected Output

No response

Possible Solution

No response

Additional Information

No response

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