Files
swift-mirror/test/Concurrency/self_escapes_deinit.swift
Holly Borla 18b747c181 [Concurrency] Diagnose captures of self in a task created in deinit.
This is done by diagnosing captures of `self` in escaping `sending` or
`@Sendable` closures inside a deinit, which almost certainly means `self`
will outlive deinit at runtime, which is a fatal error. This is a common
mistake to make when creating isolated tasks inside nonisolated deinits
to workaround the lack of synchrnous isolated deinits in Swift 6.
2024-07-12 21:49:35 -07:00

38 lines
982 B
Swift

// RUN: %target-typecheck-verify-swift -strict-concurrency=complete -disable-availability-checking
@MainActor
class C {
let x: Int = 0
deinit {
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
Task { @MainActor in
_ = self
}
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
Task {
_ = x
}
}
}
func enqueueSomewhereElse(_ closure: @escaping @Sendable () -> Void) {}
@MainActor
class C2 {
let x: Int = 0
deinit {
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
enqueueSomewhereElse {
_ = self
}
// expected-warning@+1 {{capture of 'self' in a closure that outlives deinit; this is an error in the Swift 6 language mode}}
enqueueSomewhereElse {
_ = self.x
}
}
}