Skip to content

Commit 9f257e4

Browse files
committed
Treat references to enum cases with associated values as @Sendable functions
When `InferSendableFromCaptures`, make sure to treat references to enum cases that have associated values as `@Sendable` functions.
1 parent d4f8339 commit 9f257e4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,16 @@ FunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
17101710
});
17111711

17121712
if (Context.LangOpts.hasFeature(Feature::InferSendableFromCaptures)) {
1713+
DeclContext *DC = nullptr;
17131714
if (auto *FD = dyn_cast<AbstractFunctionDecl>(decl)) {
1714-
auto *DC = FD->getDeclContext();
1715+
DC = FD->getDeclContext();
1716+
} else if (auto EED = dyn_cast<EnumElementDecl>(decl)) {
1717+
if (EED->hasAssociatedValues()) {
1718+
DC = EED->getDeclContext();
1719+
}
1720+
}
1721+
1722+
if (DC) {
17151723
// All global functions should be @Sendable
17161724
if (DC->isModuleScopeContext()) {
17171725
if (!adjustedTy->getExtInfo().isSendable()) {

test/Concurrency/sendable_methods.swift

Lines changed: 14 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,9 @@ 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)

0 commit comments

Comments
 (0)