Skip to content

[Concurrency] Allow witnesses to adopt concurrency before requirements #77434

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
Nov 8, 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
61 changes: 32 additions & 29 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5412,35 +5412,38 @@ bool ConstraintSystem::repairFailures(

if (auto *VD = getAsDecl<ValueDecl>(anchor)) {
// Matching a witness to an protocol requirement.
if (isa<ProtocolDecl>(VD->getDeclContext()) &&
VD->isProtocolRequirement() &&
VD->preconcurrency() &&
path[0].is<LocatorPathElt::Witness>() &&
// Note that the condition below is very important,
// we need to wait until the very last moment to strip
// the concurrency annotations from the innermost type.
conversionsOrFixes.empty()) {
// Allow requirements to introduce `swift_attr` and other
// concurrency related annotations (e.g. `& Sendable` or `@Sendable`)
// (note that `swift_attr` in type contexts weren't supported
// before) and for witnesses to adopt them gradually by matching
// with a warning in non-strict concurrency mode.
if (!(Context.isSwiftVersionAtLeast(6) ||
Context.LangOpts.StrictConcurrencyLevel ==
StrictConcurrency::Complete)) {
auto strippedLHS = lhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);
auto strippedRHS = rhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);

// If nothing got stripped there is no reason to re-match
// the types.
if (!strippedLHS->isEqual(lhs) || !strippedRHS->isEqual(rhs)) {
auto result = matchTypes(strippedLHS, strippedRHS, matchKind,
flags | TMF_ApplyingFix, locator);
if (!result.isFailure()) {
increaseScore(SK_MissingSynthesizableConformance, locator);
return true;
if (auto witnessElt = path[0].getAs<LocatorPathElt::Witness>()) {
if (isa<ProtocolDecl>(VD->getDeclContext()) &&
VD->isProtocolRequirement()) {
auto *witness = witnessElt->getDecl();
if ((VD->preconcurrency() || witness->preconcurrency()) &&
// Note that the condition below is very important,
// we need to wait until the very last moment to strip
// the concurrency annotations from the innermost type.
conversionsOrFixes.empty()) {
// Allow requirements/witnesses to introduce `swift_attr` and other
// concurrency related annotations (e.g. `& Sendable` or `@Sendable`)
// (note that `swift_attr` in type contexts weren't supported
// before) and for witnesses to adopt them gradually by matching
// with a warning in non-strict concurrency mode.
if (!(Context.isSwiftVersionAtLeast(6) ||
Context.LangOpts.StrictConcurrencyLevel ==
StrictConcurrency::Complete)) {
auto strippedLHS = lhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);
auto strippedRHS = rhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);

// If nothing got stripped there is no reason to re-match
// the types.
if (!strippedLHS->isEqual(lhs) || !strippedRHS->isEqual(rhs)) {
auto result = matchTypes(strippedLHS, strippedRHS, matchKind,
flags | TMF_ApplyingFix, locator);
if (!result.isFailure()) {
increaseScore(SK_MissingSynthesizableConformance, locator);
return true;
}
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/Concurrency/witness_matching_with_sendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,18 @@ struct S4 : Q { // expected-error {{type 'S4' does not conform to protocol 'Q'}}
func test(_: [() -> Any?]) {}
// expected-note@-1 {{candidate has non-matching type '([() -> Any?]) -> ()'}}
}

// Test that client is allowed to adopt concurrency first.
protocol PreConcurrency {
var prop: [String: Any] { get } // expected-swift6-note {{protocol requires property 'prop' with type '[String : Any]'}}
func req(_: [String: Any], _: ((Any)?) -> Void) // expected-swift6-note {{protocol requires function 'req' with type '([String : Any], (Any?) -> Void) -> ()'}}
}

class Adopter {
@preconcurrency var prop: [String: any Sendable] = [:] // expected-swift6-note {{candidate has non-matching type '[String : any Sendable]'}}
}

extension Adopter : PreConcurrency { // expected-swift6-error {{type 'Adopter' does not conform to protocol 'PreConcurrency'}} expected-swift6-note {{add stubs for conformance}}
@preconcurrency func req(_: [String: any Sendable], _: ((any Sendable)?) -> Void) {}
// expected-swift6-note@-1 {{candidate has non-matching type '([String : any Sendable], ((any Sendable)?) -> Void) -> ()'}}
}