Skip to content

[TypeChecker] Warn if witness mismatches only on sendability until Sw… #76268

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 2 commits into from
Sep 5, 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
25 changes: 15 additions & 10 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5616,16 +5616,17 @@ bool ConstraintSystem::repairFailures(
}

if (auto *VD = getAsDecl<ValueDecl>(anchor)) {
// Matching a witness to an ObjC protocol requirement.
if (VD->isObjC() &&
isa<ProtocolDecl>(VD->getDeclContext()) &&
// 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 inner most type.
// the concurrency annotations from the innermost type.
conversionsOrFixes.empty()) {
// Allow requirements to introduce `swift_attr` annotations
// 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.
Expand All @@ -5637,11 +5638,15 @@ bool ConstraintSystem::repairFailures(
auto strippedRHS = rhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);

auto result = matchTypes(strippedLHS, strippedRHS, matchKind,
flags | TMF_ApplyingFix, locator);
if (!result.isFailure()) {
increaseScore(SK_MissingSynthesizableConformance, locator);
return 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
8 changes: 5 additions & 3 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,11 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,

// If there are no other issues, let's check whether this are
// missing Sendable conformances when matching ObjC requirements.
// This is not an error until Swift 6 because `swift_attr` wasn't
// allowed in type contexts initially.
return req->isObjC() &&
// This is not an error until Swift 6 because i.e. `swift_attr` wasn't
// allowed in type contexts initially and introducing new concurrency
// attributes shouldn't break witnesses without strict concurrency
// enabled.
return req->preconcurrency() &&
solution->getFixedScore()
.Data[SK_MissingSynthesizableConformance] > 0;
}();
Expand Down
55 changes: 55 additions & 0 deletions test/Concurrency/witness_matching_with_sendable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -verify-additional-prefix swift5-
// RUN: %target-typecheck-verify-swift -swift-version 5 -strict-concurrency=complete -verify-additional-prefix swift6-
// RUN: %target-typecheck-verify-swift -swift-version 6 -verify-additional-prefix swift6-

protocol P {
@preconcurrency var prop: [String: any Sendable] { get set }
// expected-swift5-note@-1 3 {{expected sendability to match requirement here}}
// expected-swift6-note@-2 3 {{protocol requires property 'prop' with type '[String : any Sendable]'}}
@preconcurrency func reqFn(_: [String: any Sendable], _: @Sendable ((any Sendable)?) -> Void)
// expected-swift5-note@-1 2 {{expected sendability to match requirement here}}
// expected-swift6-note@-2 2 {{protocol requires function 'reqFn' with type '([String : any Sendable], @Sendable ((any Sendable)?) -> Void) -> ()'}}
}

struct S1 : P { // expected-swift6-error {{type 'S1' does not conform to protocol 'P'}} expected-swift6-note {{add stubs for conformance}}
var prop: [String: Any] = [:]
// expected-swift5-warning@-1 {{sendability of function types in property 'prop' does not match requirement in protocol 'P'; this is an error in the Swift 6 language mode}}
// expected-swift6-note@-2 {{candidate has non-matching type '[String : Any]'}}
func reqFn(_: [String: Any], _: (Any?) -> Void) {}
// expected-swift5-warning@-1 {{sendability of function types in instance method 'reqFn' does not match requirement in protocol 'P'; this is an error in the Swift 6 language mode}}
// expected-swift6-note@-2 {{candidate has non-matching type '([String : Any], (Any?) -> Void) -> ()'}}
}

struct S2 : P { // expected-swift6-error {{type 'S2' does not conform to protocol 'P'}} expected-swift6-note {{add stubs for conformance}}
var prop: [String: Any] = [:]
// expected-swift5-warning@-1 {{sendability of function types in property 'prop' does not match requirement in protocol 'P'; this is an error in the Swift 6 language mode}}
// expected-swift6-note@-2 {{candidate has non-matching type '[String : Any]'}}

func reqFn(_: [String: Any], _: ((any Sendable)?) -> Void) {}
// expected-swift5-warning@-1 {{sendability of function types in instance method 'reqFn' does not match requirement in protocol 'P'; this is an error in the Swift 6 language mode}}
// expected-swift6-note@-2 {{candidate has non-matching type '([String : Any], ((any Sendable)?) -> Void) -> ()'}}
}

struct S3 : P { // expected-swift6-error {{type 'S3' does not conform to protocol 'P'}} expected-swift6-note {{add stubs for conformance}}
var prop: [String: Any] = [:]
// expected-swift5-warning@-1 {{sendability of function types in property 'prop' does not match requirement in protocol 'P'; this is an error in the Swift 6 language mode}}
// expected-swift6-note@-2 {{candidate has non-matching type '[String : Any]'}}

func reqFn(_: [String: any Sendable], _: ((any Sendable)?) -> Void) {} // Ok
}

protocol Q {
var prop: [String: [String: any Sendable]] { get set }
// expected-note@-1 {{protocol requires property 'prop' with type '[String : [String : any Sendable]]'}}

func test(_: [() -> (any Sendable)?])
// expected-note@-1 {{protocol requires function 'test' with type '([() -> (any Sendable)?]) -> ()'}}
}

struct S4 : Q { // expected-error {{type 'S4' does not conform to protocol 'Q'}} expected-note {{add stubs for conformance}}
var prop: [String: [String: Any]] = [:]
// expected-note@-1 {{candidate has non-matching type '[String : [String : Any]]'}}

func test(_: [() -> Any?]) {}
// expected-note@-1 {{candidate has non-matching type '([() -> Any?]) -> ()'}}
}