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 generates wrong type for set[char] #11986

Closed
zevv opened this issue Aug 20, 2019 · 5 comments · Fixed by #17426 or #21433
Closed

quote do generates wrong type for set[char] #11986

zevv opened this issue Aug 20, 2019 · 5 comments · Fixed by #17426 or #21433

Comments

@zevv
Copy link
Contributor

zevv commented Aug 20, 2019

I'm having problems passing set[char] through quote do blocks. The generated literal loses its charness, resulting in a set[uint16] instead.

The following snippet prints {97, 98} instead of {'a', 'b'}:

import macros

proc foo(): bool =   
  var s = { 'a', 'b' }
  var n = quote do:      
    `s`                 
  echo n.repr
const a = foo()
@zevv
Copy link
Contributor Author

zevv commented Aug 20, 2019

Bad example above, this shows it better:

import macros

macro foo(): untyped =
  var s = { 'a', 'b' }
  quote do:              
    let t = `s`         
    echo typeof(t)

foo()

Results in:

set[range 0..65535(int)]

@krux02
Copy link
Contributor

krux02 commented Aug 20, 2019

You forgot to use newLit.

import macros

macro foo(): untyped =
  var s = newLit({ 'a', 'b' })
  quote do:
    let t = `s`
    echo typeof(t)

foo()

The new quoteAst will report an error if you don't appliy newLit properly.

@zevv
Copy link
Contributor Author

zevv commented Aug 20, 2019 via email

@zevv zevv closed this as completed Aug 20, 2019
@krux02
Copy link
Contributor

krux02 commented Aug 20, 2019

Aaarg, again! Can't we make this mandatory somehow?

Yes. That is one of the requirements of the new quoteAst

@timotheecour
Copy link
Member

timotheecour commented Aug 23, 2019

I added a test for both of your examples in #11722, which is another proposal to replace quote do

unlike #11823, you don't need to add an intermediate variable newLit in the macro before passing to quoteAst, all it needs is replace

quote do: some code with `foo`

by

genAst(foo):  some code with foo
block: # fix https://github.com/nim-lang/Nim/issues/11986
  proc foo(): auto =
    var s = { 'a', 'b' }
    var n = genAst(s): s
    n.repr
  static: doAssert foo() == "{'a', 'b'}"

block: # also from #11986
  macro foo(): untyped =
    var s = { 'a', 'b' }
    genAst(s):
      let t = s
      $typeof(t)
  doAssert foo() == "set[char]"

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
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