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

quote do captures non literal local variables #9607

Closed
timotheecour opened this issue Nov 2, 2018 · 4 comments · Fixed by #17426 or #21433
Closed

quote do captures non literal local variables #9607

timotheecour opened this issue Nov 2, 2018 · 4 comments · Fixed by #17426 or #21433

Comments

@timotheecour
Copy link
Member

  • following code gives BUG1:
???(0, 0) Error: internal error: genLiteral: ty is nil
import macros

proc fun1*(info:LineInfo): string = "bar"
proc fun2*(info:int): string = "bar"

macro echoL*(args: varargs[untyped]): untyped =
  let info = args.lineInfoObj
  let fun1 = bindSym"fun1"
  let fun2 = bindSym"fun2"

  # this would work instead
  # result = newCall(bindSym"fun2", info.line.newLit)

  result = quote do:

    # BUG1: ???(0, 0) Error: internal error: genLiteral: ty is nil
    `fun1`(`info`)

    # BUG2: Error: object constructor needs an object type
    # `fun2`(`info`.line)

echo echoL()
  • uncomment line below BUG2 gives Error: object constructor needs an object type

  • it works if we instead use result = newCall(bindSym"fun2", info.line.newLit)

@krux02 krux02 changed the title Error: internal error: genLiteral: ty is nil in macro quote do captures non literal local variables Nov 4, 2018
@krux02
Copy link
Contributor

krux02 commented Nov 4, 2018

Honestliy I hate how quote do tries to capture every local symbol and then fails misably at later compilation stage.

This code works btw:

import macros

proc fun1*(info:LineInfo): string = "bar"
proc fun2*(info:int): string = "bar"

macro echoL*(args: varargs[untyped]): untyped =
  let info = args.lineInfoObj
  let infoLit = info.newLit
  let infoLineLit = info.line.newLit
  let fun1 = bindSym"fun1"
  let fun2 = bindSym"fun2"

  # this would work instead
  # result = newCall(bindSym"fun2", info.line.newLit)

  result = quote do:

    # BUG1: ???(0, 0) Error: internal error: genLiteral: ty is nil
    discard `fun1`(`infoLit`)

    # BUG2: Error: object constructor needs an object type
    discard `fun2`(`infoLineLit`)

echoL()

timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 13, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 14, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 15, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 26, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Jul 30, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Aug 23, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Aug 25, 2019
timotheecour added a commit to timotheecour/Nim that referenced this issue Jan 23, 2020
@narimiran
Copy link
Member

Currently (Nim devel 1.1.1):

  • `fun1`(`info`) ("Bug1") produces: Error: unhandled exception: 'sons' is not accessible using discriminant 'kind' of type 'TNode' [FieldError]
  • `fun2`(`info`.line) ("Bug2") produces:macros.nim(533, 6) Error: type mismatch: got <varargs[untyped]> but expected 'string'

@krux02
Copy link
Contributor

krux02 commented Feb 5, 2020

@narimiran I tried to verify your output. I only get this: SIGSEGV: Illegal storage access. (Attempt to read from nil?) for bug1.

@timotheecour
Copy link
Member Author

timotheecour commented Feb 5, 2020

that's why i usually use git hashes in bug reports; it's more precise than "devel"
in latest devel 574f613 1.1.1 i get the same as #9607 (comment)

timotheecour added a commit to timotheecour/Nim that referenced this issue Mar 20, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Mar 20, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Mar 22, 2021
timotheecour added a commit to timotheecour/Nim that referenced this issue Apr 2, 2021
Araq pushed a commit that referenced this issue Apr 2, 2021
* new `macros.genAst`: fixes all issues with `quote do`
* add changelog entry
* add workaround for #2465 (comment)
* add test for #9607
* add kNoExposeLocalInjects option
* add test case for nested application of genAst
* genAst: automatically call newLit when needed
* allow skipping `{}`: genAst: foo
* add test that shows this fixes #11986
* add examples showing mixin; add examples showing passing types, macros, templates
* move to std/genasts
* improve docs
@ringabout ringabout reopened this Feb 26, 2023
@Araq Araq closed this as completed in 9948fed Mar 2, 2023
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
…nim-lang#9607; rework quote do; `getAst` uses type info to annotate the type of quoted variables; no more type erasures for quoted variables (nim-lang#21433)

* fixes nim-lang#21326; getAst uses type info to annotateType quoted variables

* simplify logics; sem types first

* fixes important packages

* add testcases

* tiny
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
…nim-lang#9607; rework quote do; `getAst` uses type info to annotate the type of quoted variables; no more type erasures for quoted variables (nim-lang#21433)

* fixes nim-lang#21326; getAst uses type info to annotateType quoted variables

* simplify logics; sem types first

* fixes important packages

* add testcases

* tiny
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment