Skip to content

Commit 5626881

Browse files
committed
[TypeChecker] TypeChecker::isSubtypeOf should recognize @sendable subtyping
A sendable function can be a subtype of a non-@sendable function, that is currently established via a fix. `TypeChecker::isSubtypeOf` should recognize its presence and fail.
1 parent d540f48 commit 5626881

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/Sema/TypeCheckConstraints.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,16 @@ bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,
10371037
if (unwrappedIUO)
10381038
*unwrappedIUO = solution->getFixedScore().Data[SK_ForceUnchecked] > 0;
10391039

1040+
// Make sure that Sendable vs. no-Sendable mismatches are
1041+
// failures here to establish subtyping relationship
1042+
// (unlike in the solver where they are warnings until Swift 6).
1043+
if (kind == ConstraintKind::Subtype) {
1044+
if (llvm::any_of(solution->Fixes, [](const auto *fix) {
1045+
return fix->getKind() == FixKind::AddSendableAttribute;
1046+
}))
1047+
return false;
1048+
}
1049+
10401050
return true;
10411051
}
10421052

test/Concurrency/sendable_methods.swift

+16
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,19 @@ do {
228228
}
229229
}
230230
}
231+
232+
do {
233+
struct Test {
234+
static func fn() {}
235+
static func otherFn() {}
236+
}
237+
238+
func fnRet(cond: Bool) -> () -> Void {
239+
cond ? Test.fn : Test.otherFn // Ok
240+
}
241+
242+
func forward<T>(_: T) -> T {
243+
}
244+
245+
let _: () -> Void = forward(Test.fn) // Ok
246+
}

0 commit comments

Comments
 (0)