Skip to content

Commit c159715

Browse files
authored
Merge pull request #73608 from hborla/ungate-struct-nonisolated-var
[Concurrency] Un-gate implicit `nonisolated` access to struct vars and isolated subclassing.
2 parents 9796751 + 29b8e6e commit c159715

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,14 @@ static bool varIsSafeAcrossActors(const ModuleDecl *fromModule,
512512

513513
if (!var->isLet()) {
514514
ASTContext &ctx = var->getASTContext();
515-
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability)) {
516-
// A mutable storage of a value type accessed from within the module is
517-
// okay.
518-
if (dyn_cast_or_null<StructDecl>(var->getDeclContext()->getAsDecl()) &&
519-
!var->isStatic() &&
520-
var->hasStorage() &&
521-
var->getTypeInContext()->isSendableType() &&
522-
accessWithinModule) {
523-
return true;
524-
}
515+
// A mutable storage of a value type accessed from within the module is
516+
// okay.
517+
if (dyn_cast_or_null<StructDecl>(var->getDeclContext()->getAsDecl()) &&
518+
!var->isStatic() &&
519+
var->hasStorage() &&
520+
var->getTypeInContext()->isSendableType() &&
521+
accessWithinModule) {
522+
return true;
525523
}
526524
// Otherwise, must be immutable.
527525
return false;
@@ -4808,12 +4806,7 @@ static bool checkClassGlobalActorIsolation(
48084806
case ActorIsolation::Unspecified:
48094807
case ActorIsolation::Nonisolated:
48104808
case ActorIsolation::NonisolatedUnsafe: {
4811-
auto &ctx = classDecl->getASTContext();
4812-
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability))
4813-
return false;
4814-
4815-
downgradeToWarning = true;
4816-
break;
4809+
return false;
48174810
}
48184811

48194812
case ActorIsolation::Erased:
@@ -6054,8 +6047,8 @@ ProtocolConformance *swift::deriveImplicitSendableConformance(
60546047

60556048
// Classes that add global actor isolation to non-Sendable
60566049
// superclasses cannot be 'Sendable'.
6057-
if (ctx.LangOpts.hasFeature(Feature::GlobalActorIsolatedTypesUsability) &&
6058-
nominal->getGlobalActorAttr()) {
6050+
auto superclassDecl = classDecl->getSuperclassDecl();
6051+
if (nominal->getGlobalActorAttr() && !superclassDecl->isNSObject()) {
60596052
return nullptr;
60606053
}
60616054
}

test/ClangImporter/objc_async.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class BarFrame: PictureFrame {
298298
@available(SwiftStdlib 5.5, *)
299299
@SomeGlobalActor
300300
class BazFrame: NotIsolatedPictureFrame {
301-
// expected-warning@-1 {{global actor 'SomeGlobalActor'-isolated class 'BazFrame' has different actor isolation from nonisolated superclass 'NotIsolatedPictureFrame'; this is an error in the Swift 6 language mode}}
301+
// expected-note@-1 2 {{class 'BazFrame' does not conform to the 'Sendable' protocol}}
302302
init() {
303303
super.init(size: 0)
304304
}
@@ -322,10 +322,12 @@ func check() async {
322322
_ = await BarFrame()
323323
_ = await FooFrame()
324324
_ = await BazFrame()
325+
// expected-warning@-1 {{non-sendable type 'BazFrame' returned by call to global actor 'SomeGlobalActor'-isolated function cannot cross actor boundary; this is an error in the Swift 6 language mode}}
325326

326327
_ = await BarFrame(size: 0)
327328
_ = await FooFrame(size: 0)
328329
_ = await BazFrame(size: 0)
330+
// expected-warning@-1 {{non-sendable type 'BazFrame' returned by call to global actor 'SomeGlobalActor'-isolated function cannot cross actor boundary; this is an error in the Swift 6 language mode}}
329331
}
330332

331333
@available(SwiftStdlib 5.5, *)

test/Concurrency/sendable_checking.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func testConversionsAndSendable(a: MyActor, s: any Sendable, f: @Sendable () ->
239239

240240
@available(SwiftStdlib 5.1, *)
241241
final class NonSendable {
242-
// expected-note @-1 3 {{class 'NonSendable' does not conform to the 'Sendable' protocol}}
242+
// expected-note @-1 4 {{class 'NonSendable' does not conform to the 'Sendable' protocol}}
243243
// TransferNonSendable emits 3 fewer errors here.
244244
// expected-targeted-and-complete-note @-3 5 {{class 'NonSendable' does not conform to the 'Sendable' protocol}}
245245
// expected-complete-and-tns-note @-4 {{class 'NonSendable' does not conform to the 'Sendable' protocol}}
@@ -397,12 +397,13 @@ struct DowngradeForPreconcurrency {
397397
}
398398
}
399399

400-
var x: Int
401-
func createStream() -> AsyncStream<Int> {
402-
AsyncStream<Int> {
400+
var x: NonSendable
401+
func createStream() -> AsyncStream<NonSendable> {
402+
AsyncStream<NonSendable> {
403403
self.x
404404
// expected-warning@-1 {{expression is 'async' but is not marked with 'await'; this is an error in the Swift 6 language mode}}
405405
// expected-note@-2 {{property access is 'async'}}
406+
// expected-warning@-3 {{non-sendable type 'NonSendable' in implicitly asynchronous access to main actor-isolated property 'x' cannot cross actor boundary; this is an error in the Swift 6 language mode}}
406407
}
407408
}
408409
}

test/SILGen/hop_to_executor_async_prop.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,8 @@ struct Container {
588588
// CHECK: [[SOME_BB]]:
589589
// CHECK: [[DATA_ADDR:%[0-9]+]] = unchecked_take_enum_data_addr [[ACCESS]] : $*Optional<Container>, #Optional.some!enumelt
590590
// CHECK: [[ELEM_ADDR:%[0-9]+]] = struct_element_addr [[DATA_ADDR]] : $*Container, #Container.iso
591-
// CHECK: hop_to_executor {{%[0-9]+}} : $Cat
592591
// CHECK: {{%[0-9]+}} = load [trivial] [[ELEM_ADDR]] : $*Float
593592
// CHECK: hop_to_executor [[GENERIC_EXEC]] :
594-
// CHECK: hop_to_executor [[GENERIC_EXEC]] :
595593
// CHECK: } // end sil function '$s4test9ContainerV10getOrCrashSfyYaFZ'
596594
static func getOrCrash() async -> Float {
597595
return await this!.iso
@@ -628,7 +626,7 @@ struct Container {
628626

629627
@propertyWrapper
630628
struct StateObject<ObjectType> {
631-
@MainActor(unsafe)
629+
@preconcurrency @MainActor
632630
var wrappedValue: ObjectType {
633631
fatalError()
634632
}

0 commit comments

Comments
 (0)