Skip to content

[CSBindings] Don't prioritize closures that are assigned to overloade… #76115

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 1 commit into from
Aug 28, 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
25 changes: 24 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,30 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
// Such type variables might be connected to closure as well
// e.g. when result type is optional, so it makes sense to
// open closure before attempting such disjunction.
return boundType->lookThroughAllOptionalTypes()->is<TypeVariableType>();
if (auto *typeVar = boundType->lookThroughAllOptionalTypes()
->getAs<TypeVariableType>()) {
auto fixedType = CS.getFixedType(typeVar);
// Handles "assignment to an overloaded member" pattern where
// type variable that represents the destination is bound to an
// l-value type during constraint generation. See \c genAssignDestType.
//
// Note that in all other circumstances we won't be here if the
// type variable that presents the closure is connected to a
// disjunction because that would mark closure as "delayed".
if (fixedType && fixedType->is<LValueType>()) {
auto lvalueObjTy = fixedType->castTo<LValueType>()->getObjectType();
// Prefer closure only if it's not connected to the type variable
// that represents l-value object type of the assignment destination.
// Eagerly attempting closure first could result in missing some of
// the contextual annotations i.e. `@Sendable`.
if (auto *lvalueObjVar = lvalueObjTy->getAs<TypeVariableType>())
return !AdjacentVars.count(lvalueObjVar);
}

return true;
}

return false;
}

// If this is a collection literal type, it's preferrable to bind it
Expand Down
22 changes: 22 additions & 0 deletions test/Concurrency/concurrency_attr_inference_on_closures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-typecheck-verify-swift -swift-version 5
// RUN: %target-typecheck-verify-swift -swift-version 5 -strict-concurrency=complete
// RUN: %target-typecheck-verify-swift -swift-version 6

// rdar://131524246

protocol P: Sendable {
typealias Block = @Sendable () -> Void
var block: Block? { get }
}

extension P {
var block: Block? { nil }
}

final class Impl: P, @unchecked Sendable {
var block: Block?
}

func test(_ v: Impl) {
v.block = {} // Ok, no warnings or errors
}