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 instantiate" error when template uses generic type #5926

Closed
endragor opened this issue Jun 1, 2017 · 4 comments
Closed

"Cannot instantiate" error when template uses generic type #5926

endragor opened this issue Jun 1, 2017 · 4 comments
Assignees

Comments

@endragor
Copy link
Contributor

endragor commented Jun 1, 2017

Sample:

type
  SomeObj[T] = object

template useSomeObj[T]() =
  var retObj: SomeObj[T]

useSomeObj[void]()
useSomeObj[int]()

Output:

Error: cannot instantiate SomeObj
got: (void)
but expected: (T)

The error occurs both for void and int.

Git hash: da52ade

/cc @zah

@endragor endragor changed the title "Cannot instantiate" error when template returns generic "Cannot instantiate" error when template uses generic type Jun 1, 2017
@zah zah self-assigned this Jun 1, 2017
@windgo-inc
Copy link

windgo-inc commented Feb 22, 2018

Hello, I just wanted to say that I got this error too trying to implement a reverse type lookup system to correlate related types. I was able to get a macro which looks up the family of related types based on a backend name string working no problem, but upon getting the reverse lookup to work, generating a MWE near exactly like the above resulted in precisely the same error message.

If you are interested, look at this. The test at line 361 is what reproduces this same error. I had the problem in both the 0.17.2 release and devel 1d1ba44.

Note that in order to get the error message, one has to remove the compiles(()) checks, which are in there to monitor for when this issue may be fixed.

  proc genericallyGetTheType[T](x: T): string =
    result = SliceType(T).name

  test "CT^2-DB reverse type lookup":
    var x: AmBackendCpu[int]

    check compiles((SliceType(AmBackendCpu[int]).name))
    check compiles((genericallyGetTheType(x)))

Has there been any progress in determining the cause of this issue?

@bluenote10
Copy link
Contributor

I just ran into this issue as well. The problem still exists although there is a minor difference in the error reporting now. For me the problem seems to occur with any instantiation of T not just int and void:

type
  Data*[T] = object
    x: T

template test*[T](xxx: T) =
  let data = Data[T](x: xxx)

test(1)

Traceback:

template_instantiation.nim(9, 5) template/generic instantiation of `test` from here
template_instantiation.nim(7, 18) Error: cannot instantiate Data
got: <int>
but expected: <T>
Traceback (most recent call last)
nim.nim(108)             nim
nim.nim(72)              handleCmdLine
cmdlinehelper.nim(91)    loadConfigsAndRunMainCommand
main.nim(163)            mainCommand
main.nim(74)             commandCompileToC
modules.nim(126)         compileProject
modules.nim(71)          compileModule
passes.nim(194)          processModule
passes.nim(86)           processTopLevelStmt
sem.nim(600)             myProcess
sem.nim(568)             semStmtAndGenerateGenerics
semstmts.nim(1973)       semStmt
semexprs.nim(913)        semExprNoType
semexprs.nim(2534)       semExpr
semstmts.nim(1914)       semStmtList
semexprs.nim(2427)       semExpr
semexprs.nim(895)        semDirectOp
semexprs.nim(789)        afterCallActions
semexprs.nim(31)         semTemplateExpr
sem.nim(411)             semAfterMacroCall
semstmts.nim(1973)       semStmt
semexprs.nim(913)        semExprNoType
semexprs.nim(2534)       semExpr
semstmts.nim(1914)       semStmtList
semexprs.nim(2537)       semExpr
semstmts.nim(438)        semVarOrLet
semexprs.nim(59)         semExprWithType
semexprs.nim(2501)       semExpr
semobjconstr.nim(258)    semObjConstr
semtypes.nim(1458)       semTypeNode
semtypes.nim(1578)       semTypeNode
semtypes.nim(1246)       semGeneric
msgs.nim(519)            localError
msgs.nim(504)            liMessage
msgs.nim(332)            handleError
msgs.nim(317)            quit

I also found a work-around:

template test*[T](xxx: T) =
  type TT = T
  let data = Data[TT](x: xxx)

@krux02
Copy link
Contributor

krux02 commented Nov 4, 2018

@endragor I inserted a workaround in your issue. I hope you are ok with it.

@ringabout
Copy link
Member

It works in Nim 1.0.6

D:\QQPCmgr\Desktop\Nim\test.nim(5, 7) Hint: 'retObj`gensym134029' is declared but not used [XDeclaredButNotUsed]
D:\QQPCmgr\Desktop\Nim\test.nim(5, 7) Hint: 'retObj`gensym134020' is declared but not used [XDeclaredButNotUsed]
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: test.nim
Hint:  [Link]
Hint: operation successful (14489 lines compiled; 2.070 sec total; 20.715MiB peakmem; Debug Build) [SuccessX]
Hint: D:\QQPCmgr\Desktop\Nim\test.exe  [Exec]

https://play.nim-lang.org/#ix=2rfV

Clyybber pushed a commit that referenced this issue Jul 11, 2020
Varriount pushed a commit that referenced this issue Jul 12, 2020
* add testcase for #5926

* fix #12759
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

8 participants