fixes #22305; Combination of generic destructor and closure fails in certain cases (#25327)

fixes #22305

It seems that the generic type is cached somehow so that no hooks are
instantiated for the generic type. There are only hooks for the
instantiated type. When `lambdalifting` tries to create type bounds for
the generic type, it cannot either find the instantiated hooks or
instantiate the generic hooks since it lacks `SemContext`. It can use
hooks for the instantiated type in this case
This commit is contained in:
ringabout
2025-12-04 00:29:45 +08:00
committed by GitHub
parent 2d0b62aa51
commit 1da0dc74d9
2 changed files with 61 additions and 0 deletions

57
tests/generics/t22305.nim Normal file
View File

@@ -0,0 +1,57 @@
discard """
joinable: false
"""
import asyncdispatch, options
proc recv*[T](tc: ptr Channel[T]): Future[T] {.async.} =
discard
type SharedBuf = object
type WorkProc[A, B] = proc(a: A): Option[B] {.nimcall.}
proc worker[TArg](p: TArg) {.thread, nimcall.} =
discard
proc readFilesThread() =
type TArg[A, B] =
tuple[r: ptr Channel[Option[A]], w: ptr Channel[Option[B]], p: WorkProc[A, B]]
var readThread: Thread[TArg[int, SharedBuf]]
proc readFilesAd() {.async.} =
var readChan: Channel[Option[int]]
type TArg[A, B] =
tuple[r: ptr Channel[Option[A]], w: ptr Channel[Option[B]], p: WorkProc[A, B]]
var readThread: Thread[TArg[int, SharedBuf]]
let test = await (addr readChan).recv()
joinThread(readThread)
waitFor readFilesAd()
type
SharedPtr[T] = object
p: ptr T
proc `=destroy`[T](self: var SharedPtr[T]) =
discard
type
SomethingObj[T] = object
Something[T] = SharedPtr[SomethingObj[T]]
proc useSomething() =
# discard Something[int]() # When you uncomment this line, it will compile successfully.
discard Something[float]()
proc fn() =
let thing = Something[int]()
proc closure() =
discard thing
closure()
fn()