Skip to content

Commit c4f6793

Browse files
committed
[Concurrency] Consider isolated key-path components when computing the required
isolation of a stored property initializer. This allows isolated key-path components to be used in stored property initializers when the stored property itself is isolated to the same global actor.
1 parent c8d06ef commit c4f6793

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,6 +3938,12 @@ namespace {
39383938
if (result == ActorReferenceResult::SameConcurrencyDomain)
39393939
break;
39403940

3941+
// An isolated key-path component requires being formed in the same
3942+
// isolation domain. Record the required isolation here if we're
3943+
// computing the isolation of a stored property initializer.
3944+
if (refineRequiredIsolation(isolation))
3945+
break;
3946+
39413947
LLVM_FALLTHROUGH;
39423948
}
39433949

test/Concurrency/isolated_default_arguments.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %empty-directory(%t)
22

3-
// RUN: %target-swift-frontend -I %t -disable-availability-checking -strict-concurrency=complete -enable-upcoming-feature IsolatedDefaultValues -parse-as-library -emit-sil -o /dev/null -verify %s
4-
// RUN: %target-swift-frontend -I %t -disable-availability-checking -strict-concurrency=complete -parse-as-library -emit-sil -o /dev/null -verify -enable-upcoming-feature IsolatedDefaultValues -enable-upcoming-feature RegionBasedIsolation %s
3+
// RUN: %target-swift-frontend -I %t -disable-availability-checking -strict-concurrency=complete -parse-as-library -emit-sil -o /dev/null -verify -enable-upcoming-feature IsolatedDefaultValues -enable-upcoming-feature RegionBasedIsolation -enable-upcoming-feature InferSendableFromCaptures %s
54

65
// REQUIRES: concurrency
76
// REQUIRES: asserts
@@ -282,3 +281,39 @@ struct InitAccessors {
282281
struct CError: Error, RawRepresentable {
283282
var rawValue: CInt
284283
}
284+
285+
// Consider isolated key-paths when computing initializer isolation
286+
287+
@MainActor
288+
class UseIsolatedKeyPath {
289+
let kp: KeyPath<UseIsolatedKeyPath, Nested> = \.x // okay
290+
291+
// expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
292+
let kp2: KeyPath<UseIsolatedKeyPath, Bool> = \.x.y // okay
293+
294+
var x: Nested = .init()
295+
296+
class Nested {
297+
@SomeGlobalActor var y: Bool = true
298+
}
299+
}
300+
301+
@MainActor
302+
protocol InferMainActor {}
303+
304+
struct UseIsolatedPropertyWrapperInit: InferMainActor {
305+
@Wrapper(\.value) var value: Int // okay
306+
307+
// expected-warning@+1 {{global actor 'SomeGlobalActor'-isolated default value in a main actor-isolated context; this is an error in the Swift 6 language mode}}
308+
@Wrapper(\.otherValue) var otherValue: Int
309+
}
310+
311+
@propertyWrapper struct Wrapper<T> {
312+
init(_: KeyPath<Values, T>) {}
313+
var wrappedValue: T { fatalError() }
314+
}
315+
316+
struct Values {
317+
@MainActor var value: Int { 0 }
318+
@SomeGlobalActor var otherValue: Int { 0 }
319+
}

0 commit comments

Comments
 (0)