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 reuse typed argument which has lambda with auto return type #16975

Closed
metagn opened this issue Feb 9, 2021 · 3 comments
Closed

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

metagn opened this issue Feb 9, 2021 · 3 comments

Comments

@metagn
Copy link
Collaborator

metagn commented Feb 9, 2021

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:
@metagn metagn changed the title expandMacros gives illformed AST with auto return type on lambda Cannot reuse typed argument which has lambda with auto return type Feb 9, 2021
@metagn metagn changed the title Cannot reuse typed argument which has lambda with auto return type Cannot reuse typed argument which has lambda with auto return type Feb 9, 2021
@saem
Copy link
Contributor

saem commented Mar 24, 2021

I have a partial fix for this in this branch (https://github.com/nim-lang/Nim/compare/devel...saem:saem-look-for-formal-param-sem-issues?expand=1), but getting a code gen issue because something else isn't being handled.

@saem
Copy link
Contributor

saem commented Mar 24, 2021

I believe this and #16906 are effectively the same issue.

@metagn
Copy link
Collaborator Author

metagn commented Apr 21, 2023

Closing as duplicate or subproblem of #16906

@metagn metagn closed this as completed Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants