Files
swift-mirror/test/Concurrency/sendable_conformance_checking.swift
Holly Borla 85b66d1dc2 [ConformanceLookup] Always prefer unavailable Sendable conformances from the
defining module, and diagnose redundant Sendable conformances.

We still allow re-stating inherited unchecked Sendable conformances in
subclasses because inherited Sendable conformances are surprising when
they opt out of static checking. Otherwise, warning on redundant Sendable
conformances nudges programmers toward cleaning up unnecessary retroactive
Sendable conformances over time as libraries incrementally add the
conformances directly.
2024-07-11 20:33:24 -07:00

190 lines
7.0 KiB
Swift

// RUN: %target-swift-frontend -emit-sil %s -o /dev/null -verify
// RUN: %target-swift-frontend -emit-sil %s -o /dev/null -verify -strict-concurrency=targeted
// RUN: %target-swift-frontend -emit-sil %s -o /dev/null -verify -verify-additional-prefix complete-and-tns- -strict-concurrency=complete
// RUN: %target-swift-frontend -emit-sil %s -o /dev/null -verify -verify-additional-prefix complete-and-tns- -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation
// REQUIRES: concurrency
// REQUIRES: asserts
@available(SwiftStdlib 5.1, *)
class NotSendable { // expected-note 9{{class 'NotSendable' does not conform to the 'Sendable' protocol}}
}
@available(SwiftStdlib 5.1, *)
@available(*, unavailable)
extension NotSendable: Sendable { }
@available(SwiftStdlib 5.1, *)
protocol IsolatedWithNotSendableRequirements: Actor {
func f() -> NotSendable
var prop: NotSendable { get }
func fAsync() async -> NotSendable
}
// Okay, everything is isolated the same way
@available(SwiftStdlib 5.1, *)
actor A1: IsolatedWithNotSendableRequirements {
func f() -> NotSendable { NotSendable() }
var prop: NotSendable { NotSendable() }
func fAsync() async -> NotSendable { NotSendable() }
}
// Okay, sendable checking occurs when calling through the protocol
// and also inside the bodies.
@available(SwiftStdlib 5.1, *)
actor A2: IsolatedWithNotSendableRequirements {
nonisolated func f() -> NotSendable { NotSendable() }
nonisolated var prop: NotSendable { NotSendable() }
nonisolated func fAsync() async -> NotSendable { NotSendable() }
// expected-warning@-1{{non-sendable type 'NotSendable' returned by nonisolated instance method 'fAsync()' satisfying protocol requirement cannot cross actor boundary}}
}
@available(SwiftStdlib 5.1, *)
protocol AsyncProtocolWithNotSendable {
func f() async -> NotSendable
var prop: NotSendable { get async }
}
// Sendable checking required because calls through protocol cross into the
// actor's domain.
@available(SwiftStdlib 5.1, *)
actor A3: AsyncProtocolWithNotSendable {
func f() async -> NotSendable { NotSendable() } // expected-warning{{non-sendable type 'NotSendable' returned by actor-isolated instance method 'f()' satisfying protocol requirement cannot cross actor boundary}}
var prop: NotSendable { // expected-warning{{non-sendable type 'NotSendable' in conformance of actor-isolated property 'prop' to protocol requirement cannot cross actor boundary}}
get async {
NotSendable()
}
}
}
// Sendable checking required because calls through protocol cross into the
// actor's domain.
@available(SwiftStdlib 5.1, *)
actor A4: AsyncProtocolWithNotSendable {
func f() -> NotSendable { NotSendable() } // expected-warning{{non-sendable type 'NotSendable' returned by actor-isolated instance method 'f()' satisfying protocol requirement cannot cross actor boundary}}
var prop: NotSendable { // expected-warning{{non-sendable type 'NotSendable' in conformance of actor-isolated property 'prop' to protocol requirement cannot cross actor boundary}}
get {
NotSendable()
}
}
}
// Sendable checking not required because we never cross into the actor's
// domain.
@available(SwiftStdlib 5.1, *)
actor A5: AsyncProtocolWithNotSendable {
nonisolated func f() async -> NotSendable { NotSendable() }
nonisolated var prop: NotSendable {
get async {
NotSendable()
}
}
}
// Sendable checking not required because we never cross into the actor's
// domain.
@available(SwiftStdlib 5.1, *)
actor A6: AsyncProtocolWithNotSendable {
nonisolated func f() -> NotSendable { NotSendable() }
nonisolated var prop: NotSendable {
get {
NotSendable()
}
}
}
@available(SwiftStdlib 5.1, *)
protocol AsyncThrowingProtocolWithNotSendable {
func f() async throws -> NotSendable
var prop: NotSendable { get async throws }
}
// Sendable checking required because calls through protocol cross into the
// actor's domain.
@available(SwiftStdlib 5.1, *)
actor A7: AsyncThrowingProtocolWithNotSendable {
func f() async -> NotSendable { NotSendable() } // expected-warning{{non-sendable type 'NotSendable' returned by actor-isolated instance method 'f()' satisfying protocol requirement cannot cross actor boundary}}
var prop: NotSendable { // expected-warning{{non-sendable type 'NotSendable' in conformance of actor-isolated property 'prop' to protocol requirement cannot cross actor boundary}}
get async {
NotSendable()
}
}
}
// Sendable checking required because calls through protocol cross into the
// actor's domain.
@available(SwiftStdlib 5.1, *)
actor A8: AsyncThrowingProtocolWithNotSendable {
func f() -> NotSendable { NotSendable() } // expected-warning{{non-sendable type 'NotSendable' returned by actor-isolated instance method 'f()' satisfying protocol requirement cannot cross actor boundary}}
var prop: NotSendable { // expected-warning{{non-sendable type 'NotSendable' in conformance of actor-isolated property 'prop' to protocol requirement cannot cross actor boundary}}
get {
NotSendable()
}
}
}
// Sendable checking not required because we never cross into the actor's
// domain.
@available(SwiftStdlib 5.1, *)
actor A9: AsyncThrowingProtocolWithNotSendable {
nonisolated func f() async -> NotSendable { NotSendable() }
nonisolated var prop: NotSendable {
get async {
NotSendable()
}
}
}
// Sendable checking not required because we never cross into the actor's
// domain.
@available(SwiftStdlib 5.1, *)
actor A10: AsyncThrowingProtocolWithNotSendable {
nonisolated func f() -> NotSendable { NotSendable() }
nonisolated var prop: NotSendable {
get {
NotSendable()
}
}
}
// rdar://86653457 - Crash due to missing Sendable conformances.
// expected-warning @+1 {{non-final class 'Klass' cannot conform to 'Sendable'; use '@unchecked Sendable'}}
class Klass<Output: Sendable>: Sendable {}
// expected-complete-and-tns-warning @+1 {{type 'S' does not conform to the 'Sendable' protocol}}
final class SubKlass: Klass<[S]> {}
// expected-complete-and-tns-note @+1 {{consider making struct 'S' conform to the 'Sendable' protocol}}
public struct S {}
// rdar://88700507 - redundant conformance of @MainActor-isolated subclass to 'Sendable'
@available(SwiftStdlib 5.1, *)
@MainActor class MainSuper {}
@available(SwiftStdlib 5.1, *)
class MainSub: MainSuper, @unchecked Sendable {}
class SendableSuper: @unchecked Sendable {}
class SendableSub: SendableSuper, @unchecked Sendable {}
class SendableExtSub: SendableSuper {}
extension SendableExtSub: @unchecked Sendable {}
// Still want to know about same-class redundancy
class MultiConformance: @unchecked Sendable {} // expected-note {{'MultiConformance' declares conformance to protocol 'Sendable' here}}
extension MultiConformance: @unchecked Sendable {} // expected-warning {{redundant conformance of 'MultiConformance' to protocol 'Sendable'}}
@available(SwiftStdlib 5.1, *)
actor MyActor {
// expected-warning@+1 {{non-final class 'Nested' cannot conform to 'Sendable'; use '@unchecked Sendable'; this is an error in the Swift 6 language mode}}
class Nested: Sendable {}
}