Files
swift-mirror/test/Concurrency/isolated_conformance_default_actor.swift
Doug Gregor 5e29333d6b [SE-0470] Enable isolated conformances by default
The IsolatedConformances feature moves to a normal, supported feature.
Remove all of the experimental-feature flags on test cases and such.

The InferIsolatedConformances feature moves to an upcoming feature for
Swift 7. This should become an adoptable feature, adding "nonisolated"
where needed.

(cherry picked from commit 3380331e7e)
2025-04-14 16:38:22 -07:00

59 lines
1.9 KiB
Swift

// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -default-isolation MainActor %s
// REQUIRES: concurrency
nonisolated
protocol P {
func f()
}
@MainActor
protocol Q {
func g()
}
class CImplicitMainActorNonisolatedConformance: nonisolated P {
func f() { } // error: explicitly nonisolated conformance
}
@MainActor
class CExplicitMainActor: P {
func f() { } // okay! conformance above is isolated
}
class CImplicitMainActor: P {
func f() { } // okay! conformance above is isolated
}
// If the protocol itself is isolated, don't do anything.
extension CExplicitMainActor: Q {
func g() { }
}
extension CImplicitMainActor: Q {
func g() { }
}
// expected-error@+3{{conformance of 'CNonIsolated' to protocol 'P' crosses into main actor-isolated code and can cause data races}}
// expected-note@+2{{turn data races into runtime errors with '@preconcurrency'}}
// expected-note@+1{{isolate this conformance to the main actor with '@MainActor'}}{{33-33=@MainActor }}
nonisolated class CNonIsolated: P {
@MainActor func f() { } // expected-note{{main actor-isolated instance method 'f()' cannot satisfy nonisolated requirement}}
}
func acceptSendablePMeta<T: Sendable & P>(_: T.Type) { }
func acceptSendableQMeta<T: Sendable & Q>(_: T.Type) { }
nonisolated func testConformancesFromNonisolated() {
let _: any P = CExplicitMainActor() // expected-error{{main actor-isolated conformance of 'CExplicitMainActor' to 'P' cannot be used in nonisolated context}}
let _: any P = CImplicitMainActor() // expected-error{{main actor-isolated conformance of 'CImplicitMainActor' to 'P' cannot be used in nonisolated context}}
let _: any P = CNonIsolated()
let _: any P = CImplicitMainActorNonisolatedConformance()
// Okay, these are nonisolated conformances.
let _: any Q = CExplicitMainActor()
let _: any Q = CImplicitMainActor()
}