Skip to content

Revert "[TypeChecker] TypeChecker::isSubtypeOf should recognize Sendable/@Sendable subtyping" #72868

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 3 commits into from
Apr 5, 2024
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: 1 addition & 15 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,22 +1068,8 @@ bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,
}

if (auto solution = cs.solveSingle()) {
const auto &score = solution->getFixedScore();
if (unwrappedIUO)
*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;
}
*unwrappedIUO = solution->getFixedScore().Data[SK_ForceUnchecked] > 0;

return true;
}
Expand Down
25 changes: 23 additions & 2 deletions test/Concurrency/sendable_keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ do {
fatalError()
}

// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
func test() -> KeyPath<String, Int> {
true ? kp() : kp() // Ok
true ? kp() : kp() // expected-error {{type of expression is ambiguous without a type annotation}}
}

func forward<T>(_ v: T) -> T { v }
let _: KeyPath<String, Int> = forward(kp()) // Ok
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
let _: KeyPath<String, Int> = forward(kp()) // expected-error {{conflicting arguments to generic parameter 'T' ('any KeyPath<String, Int> & Sendable' vs. 'KeyPath<String, Int>')}}
}

do {
Expand All @@ -236,3 +238,22 @@ do {

_ = \C<Int>.immutable as? ReferenceWritableKeyPath // Ok
}

// Should be moved back to sendable_methods.swift once ambiguities are fixed
do {
struct Test {
static func fn() {}
static func otherFn() {}
}

// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
func fnRet(cond: Bool) -> () -> Void {
cond ? Test.fn : Test.otherFn // expected-error {{type of expression is ambiguous without a type annotation}}
}

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

// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
let _: () -> Void = forward(Test.fn) // expected-error {{conflicting arguments to generic parameter 'T' ('@Sendable () -> ()' vs. '() -> Void')}}
}
26 changes: 9 additions & 17 deletions test/Concurrency/sendable_methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,6 @@ 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
}


func test_initializer_ref() {
func test<T>(_: @Sendable (T, T) -> Array<T>) {
}
Expand Down Expand Up @@ -277,3 +260,12 @@ do {
}
}
}

// rdar://125932231 - incorrect `error: type of expression is ambiguous without a type annotation`
do {
class C {}

func test(c: C) -> (any Sendable)? {
true ? nil : c // Ok
}
}