Skip to content

Cannot reuse typed argument which has lambda with auto return type #16975

Closed
@metagn

Description

@metagn

If a macro takes in a typed argument with a lambda in it, then uses that typed argument as a result; if it has to be checked again (in this case, because the return type is auto); it will give an "illformed AST" error. The error is most likely because the name of the lambda node is set to Sym ":anonymous".

Replace auto with void, int in the example, it will work. Whether or not there are parameters does not change the result, neither does where you use the lambda.

This breaks expandMacros for macros like capture.

Example

import macros

expandMacros:
  (proc: auto = discard)()
  # or discard (proc: auto = 1)()
  # or let _ = proc: auto = discard
  # as long as the return type is auto

Current Output

proc (): auto = discard ()
/usercode/in.nim(3, 1) template/generic instantiation of `expandMacros` from here
/usercode/in.nim(4, 4) Error: illformed AST: proc (): auto = discard

Expected Output

proc (): auto = discard ()

(this render is incorrect, but that's unrelated)

More basic example:

macro foo(x: typed): untyped =
  result = x

foo:
  (proc: auto = discard)()

This fixes it:

import macros

macro foo(x: typed): untyped =
  result = copy x # first copy
  result[0] = newEmptyNode() # then delete name

foo:
  (proc: auto = discard)()
$ nim -v
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-02-09
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 4576cf20af3df84a077427faf69d6bfb47821897
active boot switches:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions