@@ -1318,7 +1318,30 @@ bool BindingSet::favoredOverDisjunction(Constraint *disjunction) const {
1318
1318
// Such type variables might be connected to closure as well
1319
1319
// e.g. when result type is optional, so it makes sense to
1320
1320
// open closure before attempting such disjunction.
1321
- return boundType->lookThroughAllOptionalTypes ()->is <TypeVariableType>();
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 ;
1322
1345
}
1323
1346
1324
1347
// If this is a collection literal type, it's preferrable to bind it
0 commit comments