Skip to content

Commit 3497287

Browse files
committed
[CSBindings] Re-implement closure de-prioritization in assignment context
Re-implements #76115 in a simpler fashion now that we don't have to eagerly binding destination type on l-value.
1 parent c49390f commit 3497287

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,30 +1318,7 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
13181318
// Such type variables might be connected to closure as well
13191319
// e.g. when result type is optional, so it makes sense to
13201320
// open closure before attempting such disjunction.
1321-
if (auto *typeVar = boundType->lookThroughAllOptionalTypes()
1322-
->getAs<TypeVariableType>()) {
1323-
auto fixedType = CS.getFixedType(typeVar);
1324-
// Handles "assignment to an overloaded member" pattern where
1325-
// type variable that represents the destination is bound to an
1326-
// l-value type during constraint generation. See \c genAssignDestType.
1327-
//
1328-
// Note that in all other circumstances we won't be here if the
1329-
// type variable that presents the closure is connected to a
1330-
// disjunction because that would mark closure as "delayed".
1331-
if (fixedType && fixedType->is<LValueType>()) {
1332-
auto lvalueObjTy = fixedType->castTo<LValueType>()->getObjectType();
1333-
// Prefer closure only if it's not connected to the type variable
1334-
// that represents l-value object type of the assignment destination.
1335-
// Eagerly attempting closure first could result in missing some of
1336-
// the contextual annotations i.e. `@Sendable`.
1337-
if (auto *lvalueObjVar = lvalueObjTy->getAs<TypeVariableType>())
1338-
return !AdjacentVars.count(lvalueObjVar);
1339-
}
1340-
1341-
return true;
1342-
}
1343-
1344-
return false;
1321+
return boundType->lookThroughAllOptionalTypes()->is<TypeVariableType>();
13451322
}
13461323

13471324
// If this is a collection literal type, it's preferrable to bind it
@@ -1577,6 +1554,18 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
15771554
}
15781555
}
15791556

1557+
// Situations like `v.<member> = { ... }` where member is overloaded.
1558+
// We need to wait until member is resolved otherwise there is a risk
1559+
// of losing some of the contextual attributes important for the closure
1560+
// such as @Sendable and global actor.
1561+
if (TypeVar->getImpl().isClosureType() &&
1562+
kind == AllowedBindingKind::Subtypes) {
1563+
if (type->isTypeVariableOrMember() &&
1564+
constraint->getLocator()->directlyAt<AssignExpr>()) {
1565+
DelayedBy.push_back(constraint);
1566+
}
1567+
}
1568+
15801569
if (auto *locator = TypeVar->getImpl().getLocator()) {
15811570
// Don't allow a protocol type to get propagated from the base to the result
15821571
// type of a chain, Result should always be a concrete type which conforms

0 commit comments

Comments
 (0)