Skip to content

[TypeChecker] TypeChecker::isSubtypeOf should recognize Sendable s… #70502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
16 changes: 15 additions & 1 deletion lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,22 @@ bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,
}

if (auto solution = cs.solveSingle()) {
const auto &score = solution->getFixedScore();
if (unwrappedIUO)
*unwrappedIUO = solution->getFixedScore().Data[SK_ForceUnchecked] > 0;
*unwrappedIUO = score.Data[SK_ForceUnchecked] > 0;

// Make sure that Sendable vs. no-Sendable mismatches are
// failures here to establish subtyping relationship
// (unlike in the solver where they are warnings until Swift 6).
if (kind == ConstraintKind::Subtype) {
if (score.Data[SK_MissingSynthesizableConformance] > 0)
return false;

if (llvm::any_of(solution->Fixes, [](const auto *fix) {
return fix->getKind() == FixKind::AddSendableAttribute;
}))
return false;
}

return true;
}
Expand Down
13 changes: 13 additions & 0 deletions test/Concurrency/sendable_keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,16 @@ do {
let _: [PartialKeyPath<S>] = [\.a, \.b] // Ok
let _: [any PartialKeyPath<S> & Sendable] = [\.a, \.b] // Ok
}

do {
func kp() -> KeyPath<String, Int> & Sendable {
fatalError()
}

func test() -> KeyPath<String, Int> {
true ? kp() : kp() // Ok
}

func forward<T>(_ v: T) -> T { v }
let _: KeyPath<String, Int> = forward(kp()) // Ok
}
16 changes: 16 additions & 0 deletions test/Concurrency/sendable_methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,19 @@ do {
}
}
}

do {
struct Test {
static func fn() {}
static func otherFn() {}
}

func fnRet(cond: Bool) -> () -> Void {
cond ? Test.fn : Test.otherFn // Ok
}

func forward<T>(_: T) -> T {
}

let _: () -> Void = forward(Test.fn) // Ok
}