Skip to content

[6.0][Concurrency] Consider isolated key-path components when computing the required isolation of a stored property initializer. #75101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3943,6 +3943,12 @@ namespace {
if (result == ActorReferenceResult::SameConcurrencyDomain)
break;

// An isolated key-path component requires being formed in the same
// isolation domain. Record the required isolation here if we're
// computing the isolation of a stored property initializer.
if (refineRequiredIsolation(isolation))
break;

LLVM_FALLTHROUGH;
}

Expand Down
39 changes: 37 additions & 2 deletions test/Concurrency/isolated_default_arguments.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// RUN: %empty-directory(%t)

// 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
// 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
// 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

// REQUIRES: concurrency
// REQUIRES: asserts
Expand Down Expand Up @@ -282,3 +281,39 @@ struct InitAccessors {
struct CError: Error, RawRepresentable {
var rawValue: CInt
}

// Consider isolated key-paths when computing initializer isolation

@MainActor
class UseIsolatedKeyPath {
let kp: KeyPath<UseIsolatedKeyPath, Nested> = \.x // okay

// expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}}
let kp2: KeyPath<UseIsolatedKeyPath, Bool> = \.x.y // okay

var x: Nested = .init()

class Nested {
@SomeGlobalActor var y: Bool = true
}
}

@MainActor
protocol InferMainActor {}

struct UseIsolatedPropertyWrapperInit: InferMainActor {
@Wrapper(\.value) var value: Int // okay

// 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}}
@Wrapper(\.otherValue) var otherValue: Int
}

@propertyWrapper struct Wrapper<T> {
init(_: KeyPath<Values, T>) {}
var wrappedValue: T { fatalError() }
}

struct Values {
@MainActor var value: Int { 0 }
@SomeGlobalActor var otherValue: Int { 0 }
}