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

Some typedesc don't work as ref object field types #12582

Closed
dawkot opened this issue Nov 2, 2019 · 3 comments · Fixed by #22564
Closed

Some typedesc don't work as ref object field types #12582

dawkot opened this issue Nov 2, 2019 · 3 comments · Fixed by #22564

Comments

@dawkot
Copy link

dawkot commented Nov 2, 2019

Example

import macros

macro foo(T: type): type =
  nnkBracketExpr.newTree(bindSym "array", newLit 1, T)

var
  _: foo(int) # fine

type
  Foo = object
    x: foo(int) # fine

  Bar = ref object
    x: foo(int) # error

Current Output

/usercode/in.nim(14, 11) Error: type mismatch: got <int>
but expected one of: 
macro foo(T: type): type
  first type mismatch at position: 1
  required type for T: typedesc
  but expression 'int' is of type: int

expression: foo(int)

Additional Information

  • If T in the nnkBracketExpr is replaced with bindSym "int" the code compiles, so it's not just about the macro parameter existing
$ nim -v
Nim Compiler Version 1.0.2 [Windows: amd64]
Compiled at 2019-10-22
@dawkot dawkot changed the title Some macros don't work ref object field types Some macros don't work as ref object field types Nov 2, 2019
@krux02 krux02 added the Macros label Nov 3, 2019
@dawkot
Copy link
Author

dawkot commented Nov 5, 2019

If it's of any help, this compiles

template foo(T: type): type = array[1, T]

type Bar = ref object
  x: foo(int)

static:
  assert Bar.x is array[1, int]

@krux02
Copy link
Contributor

krux02 commented Nov 6, 2019

import macros

macro foo(t: type): untyped =
  # you think you get the type `int`
  echo t.lispRepr # (Sym "int"), looks legit But nope, you don't get
  # `int`. You get a shitty typedesc object that disguises as `int`,
  # but isn't `int`. The type of that object is `typedesc[int]`. You
  # can see that here:
  var typ = t.getTypeInst
  echo typ.lispRepr # (BracketExpr (Sym "typeDesc") (Sym "int"))
  # strip shitty typedesc.
  typ = typ[1]
  # now it works
  nnkBracketExpr.newTree(bindSym "array", newLit 1, typ)

var
  _: foo(int) # fine

type
  Foo = object
    x: foo(int) # fine

  Bar = ref object
    x: foo(int) # error

@zah This is one of many reason why I hate typedesc with passion. It is a constant source of trouble, misguidance and non-working.

@krux02 krux02 changed the title Some macros don't work as ref object field types Some typedesc don't work as ref object field types Nov 8, 2019
@metagn
Copy link
Collaborator

metagn commented Aug 26, 2023

Given example compiles in 2.0

metagn added a commit to metagn/Nim that referenced this issue Aug 27, 2023
closes nim-lang#12582, closes nim-lang#19552, closes nim-lang#2465, closes nim-lang#4596, closes nim-lang#15246,
closes nim-lang#12683, closes nim-lang#7889, closes nim-lang#4547, closes nim-lang#12415, closes nim-lang#2002,
closes nim-lang#1771, closes nim-lang#5121

The test for nim-lang#5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.
Araq pushed a commit that referenced this issue Aug 27, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
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