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

typeof(default(T)[]) cannot be manipulated in macros #13049

Closed
mratsim opened this issue Jan 5, 2020 · 4 comments
Closed

typeof(default(T)[]) cannot be manipulated in macros #13049

mratsim opened this issue Jan 5, 2020 · 4 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 5, 2020

Building on the solution for #13048 (comment)

import macros

macro foo*(T: typedesc): typedesc =
  echo T.getTypeInst.treerepr
  echo "--------------------------------------"
  let instantiated = T.getTypeInst
  echo instantiated.treeRepr[1] # This is the char 'r'

  return getType(int)

type
  Task = ptr object
    prev, next: Task
    parent: Task
    fn: proc (param: pointer) {.nimcall.}
    # User data
    data: array[10, byte]

discard foo(typeof(default(Task)[]))

The instantiated.treeRepr returns the character 'r' which is very strange.

Output

BracketExpr
  Sym "typeDesc"
  Sym "Task:ObjectType"
--------------------------------------
r
@ghost
Copy link

ghost commented Jan 5, 2020

You just called treerepr too early. You're indexing a string, not a NimNode.

@mratsim
Copy link
Collaborator Author

mratsim commented Jan 6, 2020

That's not it.

If you look closely

  echo T.getTypeInst.treerepr
  echo "--------------------------------------"

returns

BracketExpr
  Sym "typeDesc"
  Sym "Task:ObjectType"
--------------------------------------

So instantiated[1] => T.getTypeInst[1] should return Sym "Task:ObjectType"

If you add an extra line that display the type you can confirm that it returns a char instead of a nnkSym.

import macros

macro foo*(T: typedesc): typedesc =
  echo T.getTypeInst.treerepr
  echo "--------------------------------------"
  let instantiated = T.getTypeInst
  echo typeof instantiated.treeRepr[1]
  echo instantiated.treeRepr[1] # This is the char 'r'

  return getType(int)

type
  Task = ptr object
    prev, next: Task
    parent: Task
    fn: proc (param: pointer) {.nimcall.}
    # User data
    data: array[10, byte]

discard foo(typeof(default(Task)[]))
BracketExpr
  Sym "typeDesc"
  Sym "Task:ObjectType"
--------------------------------------
char
r

@ghost
Copy link

ghost commented Jan 6, 2020

So instantiated[1] => T.getTypeInst[1] should return Sym "Task:ObjectType"

But that is not what you wrote: instantiated.treeRepr[1].
"BracketExpr..."[1] == 'r'

@mratsim
Copy link
Collaborator Author

mratsim commented Jan 7, 2020

guess I was lacking sleep, good catch.

@mratsim mratsim closed this as completed Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant