Skip to content

Commit e678429

Browse files
committed
[ConstraintSystem] Make binding producer responsible for attemping Any last
Instead of doing that while collecting bindings, let's move `Any` to the end of the list when type variable has been selected to be attempted next.
1 parent ef673c7 commit e678429

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,6 @@ void ConstraintSystem::PotentialBindings::finalize(
566566

567567
addPotentialBinding(PotentialBinding::forHole(TypeVar, locator));
568568
}
569-
570-
// Let's always consider `Any` to be a last resort binding because
571-
// it's always better to infer concrete type and erase it if required
572-
// by the context.
573-
if (Bindings.size() > 1) {
574-
auto AnyTypePos =
575-
llvm::find_if(Bindings, [](const PotentialBinding &binding) {
576-
return binding.BindingType->isAny() &&
577-
!binding.isDefaultableBinding();
578-
});
579-
580-
if (AnyTypePos != Bindings.end()) {
581-
std::rotate(AnyTypePos, AnyTypePos + 1, Bindings.end());
582-
}
583-
}
584569
}
585570

586571
Optional<ConstraintSystem::PotentialBindings>

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5366,14 +5366,30 @@ TypeVarBindingProducer::TypeVarBindingProducer(
53665366
return false;
53675367
};
53685368

5369+
// A binding to `Any` which should always be considered as a last resort.
5370+
Optional<Binding> Any;
5371+
53695372
for (const auto &binding : bindings.Bindings) {
5373+
auto type = binding.BindingType;
5374+
5375+
if (!CanBeNil && type->isAny()) {
5376+
Any.emplace(binding);
5377+
continue;
5378+
}
5379+
53705380
// Adjust optionality of existing bindings based on presence of
53715381
// `ExpressibleByNilLiteral` requirement.
53725382
if (CanBeNil && requiresOptionalAdjustment(binding)) {
5373-
Bindings.push_back(
5374-
binding.withType(OptionalType::get(binding.BindingType)));
5383+
Bindings.push_back(binding.withType(OptionalType::get(type)));
53755384
} else {
53765385
Bindings.push_back(binding);
53775386
}
53785387
}
5388+
5389+
// Let's always consider `Any` to be a last resort binding because
5390+
// it's always better to infer concrete type and erase it if required
5391+
// by the context.
5392+
if (Any) {
5393+
Bindings.push_back(*Any);
5394+
}
53795395
}

0 commit comments

Comments
 (0)