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 access static object field after passing it to macro #13252

Closed
nitely opened this issue Jan 24, 2020 · 3 comments · Fixed by #24525
Closed

Cannot access static object field after passing it to macro #13252

nitely opened this issue Jan 24, 2020 · 3 comments · Fixed by #24525

Comments

@nitely
Copy link
Contributor

nitely commented Jan 24, 2020

I get "Error: undeclared field: 'a'" when trying to access a static object field after passing it to a macro.

Example

import macros

type
  FooBar = object
    a: seq[int]

macro genFoobar(fb: static FooBar): untyped =
  result = newStmtList()
  for b in fb.a:
    result.add(newCall(bindSym"echo", newLit b))

proc foobar(fb: static FooBar) =
  genFoobar(fb)
  # Error: undeclared field: 'a'
  for b in fb.a:
    echo "foo" & $b

proc main() =
  const a: seq[int] = @[1, 2, 3]
  const fb = Foobar(a: a)
  foobar(fb)
main()

Current Output

Error: undeclared field: 'a'

Expected Output

1
2
3
foo1
foo2
foo3

Additional Information

  • I tried in Nim devel and v1.0.4
$ nim -v
Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-01-24
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release
@metagn
Copy link
Collaborator

metagn commented Jan 25, 2020

For some reason the error message changes when you add a single line

import macros

type
  FooBar = object
    a: seq[int]

macro genFoobar(fb: static FooBar): untyped =
  result = newStmtList()
  for b in fb.a:
    result.add(newCall(bindSym"echo", newLit b))

proc foobar(fb: static FooBar) =
  genFoobar(fb)
  echo fb.type # added line, same error when wrapped in static:
  for b in fb.a:
    echo "foo" & $b

proc main() =
  const a: seq[int] = @[1, 2, 3]
  # Error: type mismatch: got <array[0..2, int]> but expected 'seq[int]'
  const fb = Foobar(a: a)
  foobar(fb)
main()

So this is some ownership/GC problem?

@nitely
Copy link
Contributor Author

nitely commented Jul 14, 2020

Works in devel now. However, @hlaaftana code fails with: Error: type mismatch: got <array[0..2, int]> but expected 'seq[int]', replacing fb.type by type(fb) compiles.

@metagn
Copy link
Collaborator

metagn commented Nov 3, 2024

Second example also works now probably since #24224

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.

2 participants