Skip to content

Commit 359ea14

Browse files
committed
[TypeChecker] TypeChecker::isSubtypeOf should recognize Sendable subtyping
A sendable type can be a subtype of a non-Sendable type, i.e. `any X & Sendable` vs. `X` where `X` is a class. The solver adjusts `MissingSynthesizableConformance` to indicate that one of the types is missing `Sendable` and that needs to be recognized by `TypeChecker::isSubtypeOf`.
1 parent 5626881 commit 359ea14

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/Sema/TypeCheckConstraints.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,17 @@ bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,
10341034
}
10351035

10361036
if (auto solution = cs.solveSingle()) {
1037+
const auto &score = solution->getFixedScore();
10371038
if (unwrappedIUO)
1038-
*unwrappedIUO = solution->getFixedScore().Data[SK_ForceUnchecked] > 0;
1039+
*unwrappedIUO = score.Data[SK_ForceUnchecked] > 0;
10391040

10401041
// Make sure that Sendable vs. no-Sendable mismatches are
10411042
// failures here to establish subtyping relationship
10421043
// (unlike in the solver where they are warnings until Swift 6).
10431044
if (kind == ConstraintKind::Subtype) {
1045+
if (score.Data[SK_MissingSynthesizableConformance] > 0)
1046+
return false;
1047+
10441048
if (llvm::any_of(solution->Fixes, [](const auto *fix) {
10451049
return fix->getKind() == FixKind::AddSendableAttribute;
10461050
}))

test/Concurrency/sendable_keypaths.swift

+13
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,16 @@ do {
217217
let _: [PartialKeyPath<S>] = [\.a, \.b] // Ok
218218
let _: [any PartialKeyPath<S> & Sendable] = [\.a, \.b] // Ok
219219
}
220+
221+
do {
222+
func kp() -> KeyPath<String, Int> & Sendable {
223+
fatalError()
224+
}
225+
226+
func test() -> KeyPath<String, Int> {
227+
true ? kp() : kp() // Ok
228+
}
229+
230+
func forward<T>(_ v: T) -> T { v }
231+
let _: KeyPath<String, Int> = forward(kp()) // Ok
232+
}

0 commit comments

Comments
 (0)