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

Compiler cannot find injected symbol when both proc and template are generic #20086

Open
Clonkk opened this issue Jul 25, 2022 · 1 comment
Open

Comments

@Clonkk
Copy link
Contributor

Clonkk commented Jul 25, 2022

Example

type
  Foo = object
    k: int

proc init*(t: typedesc[Foo]): Foo =
  result = Foo()

type
  Bar = object

template withObj*[T: object](body: untyped) =
  block:
    var obj {.inject.} = init(typedesc[T])
    body

proc dummy1*() =
  withObj[Foo]():
    echo obj

proc dummy2*(t: typedesc[Bar]) =
  # Does not compile
  withObj[Foo]():
    echo obj

proc dummy3*[T: object]() =
  # Does not compile
  withObj[T]():
    echo obj

when isMainModule:
  dummy1()
  Bar.dummy2()
  dummy3[Foo]()

Current Output

foo.nim(23, 10) Error: undeclared identifier: 'obj'

Note that both dummy2 & dummy3 do not compile (both with the same error on different line) while dummy1 works.

Expected Output

Program should compile and run, echo-ing 3 empty Foo object

(k: 0)
(k: 0)
(k: 0)

Additional Information

This looks similar / related to #19556.
Also relevant ? nim-lang/RFCs#402

Tested with Nim release v1.6.6 and latest devel :

$ nim -v
Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-07-17
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 0d8bec695606a65c5916d0da7fcb0a976a4e1f7b
active boot switches: -d:release

@metagn
Copy link
Collaborator

metagn commented Jul 25, 2022

Changing to template withObj*(T: typedesc[object], body: untyped) is a workaround. nim-lang/RFCs#402 is not related because overloading isn't relevant here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants