Skip to content
Open
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
27 changes: 19 additions & 8 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7180,14 +7180,6 @@ static bool checkSendableInstanceStorage(
}
}

if (nominal->suppressesConformance(KnownProtocolKind::Sendable)) {
auto *conformanceDecl = dc->getAsDecl() ? dc->getAsDecl() : nominal;
if (!isImplicitSendableCheck(check)) {
conformanceDecl->diagnose(diag::non_sendable_type_suppressed);
}
return true;
}

// Stored properties of structs and classes must have
// Sendable-conforming types.
class Visitor: public StorageVisitor {
Expand Down Expand Up @@ -7462,6 +7454,25 @@ bool swift::checkSendableConformance(
// and not some (possibly constrained) extension.
if (wasImplied)
conformanceDC = nominal;

// Sendable supression allows conditional conformances only.
if (nominal->suppressesConformance(KnownProtocolKind::Sendable)) {
bool hasUnconditionalConformance = false;
if (auto *normalConf = dyn_cast<NormalProtocolConformance>(conformance)) {
hasUnconditionalConformance =
normalConf->getConditionalRequirements().empty();
}

if (hasUnconditionalConformance) {
if (!isImplicitSendableCheck(check)) {
auto *conformanceDecl =
conformanceDC->getAsDecl() ? conformanceDC->getAsDecl() : nominal;
conformanceDecl->diagnose(diag::non_sendable_type_suppressed);
}
return true;
}
}

return checkSendableInstanceStorage(nominal, conformanceDC, check);
}

Expand Down
15 changes: 15 additions & 0 deletions test/ModuleInterface/tilde_sendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ protocol P {
public struct S: P, ~Sendable {
public let x: Int
}

// CHECK: #if compiler(>=5.3) && $TildeSendable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity – is this meant to be a check for 5.3 or 6.3? if it is 5.3, why is that version relevant?

// CHECK: public struct B<T> : ~Swift.Sendable {
// CHECK: }
// CHECK: #else
// CHECK: public struct B<T> {
// CHECK: }
// CHECK: #endif
public struct B<T>: ~Sendable {
}

// CHECK: extension Library.B : Swift.Sendable where T : Swift.Sendable {
// CHECK: }
extension B: Sendable where T: Sendable {
}
39 changes: 39 additions & 0 deletions test/Sema/tilde_sendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,42 @@ do {
check(NonSendable()) // expected-warning {{type 'NonSendable' does not conform to the 'Sendable' protocol}}
check(NoInference()) // Ok
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a test for how this conditional conformance behavior works with classes and inheritance? and perhaps for completion, some enum tests should be included too?

also, when messing with whatever version is on the nightly godbolt, it also seems like this is accepted on the current snapshot (57cf4ce), which seems wrong, or at least confusing:

actor A: ~Sendable {}

presumably that is due to its being a ClassDecl or something.


edit: oh, and another case occurs to me – should we have a case to test a conditional Sendable conformance to another protocol that refines Sendable?

func takesSendable<T: Sendable>(_: T) {}

class MyValue {} // expected-note 2 {{class 'MyValue' does not conform to the 'Sendable' protocol}}

public struct D: ~Sendable {
}

extension D: Sendable {} // expected-error {{cannot both conform to and suppress conformance to 'Sendable'}}

takesSendable(D())

public struct F<T>: ~Sendable {
let x: T
}

extension F: Sendable where T: Sendable { }

takesSendable(F(x: 42))

public struct G<T, U>: ~Sendable { // expected-note {{making generic parameter 'U' conform to the 'Sendable' protocol}}
let t: T
let u: U // expected-warning {{stored property 'u' of 'Sendable'-conforming generic struct 'G' has non-Sendable type 'U'}}
}

extension G: Sendable where T: Sendable { }

takesSendable(G(t: "", u: 42))
takesSendable(G(t: MyValue(), u: 0)) // expected-warning {{type 'MyValue' does not conform to the 'Sendable' protocol}}

public struct H<T, U>: ~Sendable {
let t: T
let u: U
}

extension H: Sendable where T: Sendable, U: Sendable { }

takesSendable(H(t: "", u: 42))
takesSendable(H(t: "", u: MyValue())) // expected-warning {{type 'MyValue' does not conform to the 'Sendable' protocol}}