Skip to content

Commit 3a7b993

Browse files
authored
Merge pull request swiftlang#74362 from DougGregor/enum-case-sendable-fn
Treat references to enum cases with associated values as `@Sendable` functions
2 parents 75bd5a5 + 95e12ca commit 3a7b993

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,8 +1747,17 @@ FunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
17471747
});
17481748

17491749
if (Context.LangOpts.hasFeature(Feature::InferSendableFromCaptures)) {
1750+
DeclContext *DC = nullptr;
17501751
if (auto *FD = dyn_cast<AbstractFunctionDecl>(decl)) {
1751-
auto *DC = FD->getDeclContext();
1752+
DC = FD->getDeclContext();
1753+
} else if (auto EED = dyn_cast<EnumElementDecl>(decl)) {
1754+
if (EED->hasAssociatedValues() &&
1755+
!locator.endsWith<LocatorPathElt::PatternMatch>()) {
1756+
DC = EED->getDeclContext();
1757+
}
1758+
}
1759+
1760+
if (DC) {
17521761
// All global functions should be @Sendable
17531762
if (DC->isModuleScopeContext()) {
17541763
if (!adjustedTy->getExtInfo().isSendable()) {

test/Concurrency/sendable_methods.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct InferredSendableS: P {
4242

4343
enum InferredSendableE: P {
4444
case a, b
45+
case c(Int)
4546

4647
func f() { }
4748
}
@@ -60,6 +61,13 @@ struct GenericS<T> : P {
6061
func g() async { }
6162
}
6263

64+
enum GenericE<T> {
65+
case a
66+
case b(T)
67+
}
68+
69+
extension GenericE: Sendable where T: Sendable { }
70+
6371
class NonSendable {
6472
func f() {}
6573
}
@@ -265,3 +273,16 @@ do {
265273
true ? nil : c // Ok
266274
}
267275
}
276+
277+
func acceptSendableFunc<T, U>(_: @Sendable (T) -> U) { }
278+
279+
acceptSendableFunc(InferredSendableE.c)
280+
acceptSendableFunc(GenericE<Int>.b)
281+
acceptSendableFunc(GenericE<NonSendable>.b)
282+
283+
// Make sure pattern matching isn't affected by @Sendable on cases.
284+
func testPatternMatch(ge: [GenericE<Int>]) {
285+
if case .b(let a) = ge.first {
286+
_ = a
287+
}
288+
}

0 commit comments

Comments
 (0)