@@ -336,8 +336,12 @@ void PotentialBindings::inferTransitiveBindings(
336
336
addLiteral (literal.second .getSource ());
337
337
338
338
// Infer transitive defaults.
339
- for (const auto &def : bindings.Defaults )
339
+ for (const auto &def : bindings.Defaults ) {
340
+ if (def.getSecond ()->getKind () == ConstraintKind::DefaultClosureType)
341
+ continue ;
342
+
340
343
addDefault (def.second );
344
+ }
341
345
342
346
// TODO: We shouldn't need this in the future.
343
347
if (entry.second ->getKind () != ConstraintKind::Subtype)
@@ -366,11 +370,45 @@ void PotentialBindings::inferTransitiveBindings(
366
370
}
367
371
}
368
372
373
+ // If potential binding type variable is a closure that has a subtype relation
374
+ // associated with argument conversion constraint located directly on an
375
+ // autoclosure parameter.
376
+ static bool
377
+ isClosureInAutoClosureArgumentConversion (PotentialBindings &bindings) {
378
+
379
+ if (!bindings.TypeVar ->getImpl ().isClosureType ())
380
+ return false ;
381
+
382
+ return llvm::any_of (
383
+ bindings.SubtypeOf ,
384
+ [](std::pair<TypeVariableType *, Constraint *> subType) {
385
+ if (subType.second ->getKind () != ConstraintKind::ArgumentConversion)
386
+ return false ;
387
+ return subType.second ->getLocator ()
388
+ ->isLastElement <LocatorPathElt::AutoclosureResult>();
389
+ });
390
+ }
391
+
369
392
void PotentialBindings::finalize (
370
393
llvm::SmallDenseMap<TypeVariableType *, PotentialBindings>
371
394
&inferredBindings) {
372
395
inferTransitiveProtocolRequirements (inferredBindings);
373
396
inferTransitiveBindings (inferredBindings);
397
+
398
+ // For autoclosure parameters if we have a closure argument which could
399
+ // default to `() -> $T`, we avoid infering defaultable binding because
400
+ // an autoclosure cannot accept a closure paramenter unless the result `$T`
401
+ // is bound to a function type via another contextual binding. Also consider
402
+ // adjacent vars because they can also default transitively.
403
+ if (isClosureInAutoClosureArgumentConversion (*this )) {
404
+ auto closureDefault = llvm::find_if (
405
+ Defaults, [](const std::pair<CanType, Constraint *> &entry) {
406
+ return entry.second ->getKind () == ConstraintKind::DefaultClosureType;
407
+ });
408
+ if (closureDefault != Defaults.end ()) {
409
+ Defaults.erase (closureDefault);
410
+ }
411
+ }
374
412
}
375
413
376
414
PotentialBindings::BindingScore
0 commit comments