Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot instantiate generic type when a macro reuses a OpenSymChoice #19670

Closed
Menduist opened this issue Apr 1, 2022 · 1 comment · Fixed by #24028
Closed

Cannot instantiate generic type when a macro reuses a OpenSymChoice #19670

Menduist opened this issue Apr 1, 2022 · 1 comment · Fixed by #24028

Comments

@Menduist
Copy link
Contributor

Menduist commented Apr 1, 2022

Example

import macros

type
  Past[Z] = object
  OpenObject = object

macro rewriter(prc: untyped): untyped =
  prc.body.add(nnkCall.newTree(
    prc.params[0]
  ))
  prc
  
macro macroAsync(name, restype: untyped): untyped =
  quote do:
    proc `name`(): Past[seq[`restype`]] {.rewriter.} = discard
    
macroAsync(testMacro, OpenObject)

Current Output

Error: cannot instantiate Past [type declared in /tmp/test.nim(4, 3)]
got: <T>
but expected: <Z>

Expected Output

Compiles correctly

Additional Information

On every nim version I tested. Caused problems in chronos, worked around with: status-im/nim-chronos#261, which allows to cleanup the opensyms:

proc cleanupOpenSymChoice(node: NimNode): NimNode {.compileTime.} =
  if node.kind in nnkCallKinds and
    node[0].kind == nnkOpenSymChoice and node[0].eqIdent("[]"):
    result = newNimNode(nnkBracketExpr)
    for child in node[1..^1]:
      result.add(cleanupOpenSymChoice(child))
  else:
    result = node.copyNimNode()
    for child in node:
      result.add(cleanupOpenSymChoice(child))

With

  prc.body.add(nnkCall.newTree(
    cleanupOpenSymChoice(prc.params[0])

The first code now compiles correctly.

@metagn
Copy link
Collaborator

metagn commented Aug 23, 2024

Works in devel probably because of #23983

metagn added a commit to metagn/Nim that referenced this issue Aug 29, 2024
@Araq Araq closed this as completed in fc853cb Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants