Skip to content

Commit 5ba2e59

Browse files
authored
Merge pull request #28752 from xedin/simplify-potential-binding
[ConstraintSystem] Record originator constraint for each type variable binding
2 parents fb76496 + c4b74a3 commit 5ba2e59

14 files changed

+218
-187
lines changed

lib/AST/TypeJoinMeet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ CanType TypeJoin::visitFunctionType(CanType second) {
314314
auto secondExtInfo = secondFnTy->getExtInfo();
315315

316316
// FIXME: Properly handle these attributes.
317-
if (firstExtInfo.withNoEscape(false) != secondExtInfo.withNoEscape(false))
317+
if (firstExtInfo != secondExtInfo)
318318
return Unimplemented;
319319

320320
if (!AnyFunctionType::equalParams(firstFnTy->getParams(),

lib/Sema/CSBindings.cpp

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ConstraintSystem::determineBestBindings() {
8181
continue;
8282

8383
bindings.addPotentialBinding(
84-
{type, AllowedBindingKind::Supertypes, binding.BindingSource});
84+
binding.withSameSource(type, AllowedBindingKind::Supertypes));
8585
}
8686
}
8787

@@ -143,8 +143,8 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
143143
!binding.BindingType->hasUnresolvedType() &&
144144
!binding.BindingType->hasTypeVariable() &&
145145
!binding.BindingType->hasUnboundGenericType() &&
146-
!binding.DefaultedProtocol && !binding.isDefaultableBinding() &&
147-
allowJoinMeet) {
146+
!binding.hasDefaultedLiteralProtocol() &&
147+
!binding.isDefaultableBinding() && allowJoinMeet) {
148148
if (lastSupertypeIndex) {
149149
auto &lastBinding = Bindings[*lastSupertypeIndex];
150150
auto lastType = lastBinding.BindingType->getWithoutSpecifierType();
@@ -164,7 +164,7 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
164164
lastSupertypeIndex = Bindings.size();
165165
}
166166

167-
if (auto *literalProtocol = binding.DefaultedProtocol)
167+
if (auto *literalProtocol = binding.getDefaultedLiteralProtocol())
168168
foundLiteralBinding(literalProtocol);
169169

170170
// If the type variable can't bind to an lvalue, make sure the
@@ -371,7 +371,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
371371
kind = AllowedBindingKind::Exact;
372372
}
373373

374-
return PotentialBinding{type, kind, constraint->getKind()};
374+
return PotentialBinding{type, kind, constraint};
375375
}
376376

377377
/// Retrieve the set of potential type bindings for the given
@@ -466,9 +466,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
466466
path.back().getKind() == ConstraintLocator::ClosureResult &&
467467
binding->Kind == AllowedBindingKind::Supertypes &&
468468
exactTypes.insert(voidType).second) {
469-
result.addPotentialBinding(
470-
{voidType, binding->Kind, constraint->getKind()},
471-
/*allowJoinMeet=*/false);
469+
result.addPotentialBinding({voidType, binding->Kind, constraint},
470+
/*allowJoinMeet=*/false);
472471
}
473472
}
474473
}
@@ -551,9 +550,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
551550
if (!exactTypes.insert(defaultType->getCanonicalType()).second)
552551
continue;
553552

554-
literalBindings.push_back({defaultType, AllowedBindingKind::Subtypes,
555-
constraint->getKind(),
556-
constraint->getProtocol()});
553+
literalBindings.push_back(
554+
{defaultType, AllowedBindingKind::Subtypes, constraint});
557555
continue;
558556
}
559557

@@ -578,9 +576,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
578576

579577
if (!matched) {
580578
exactTypes.insert(defaultType->getCanonicalType());
581-
literalBindings.push_back({defaultType, AllowedBindingKind::Subtypes,
582-
constraint->getKind(),
583-
constraint->getProtocol()});
579+
literalBindings.push_back(
580+
{defaultType, AllowedBindingKind::Subtypes, constraint});
584581
}
585582

586583
break;
@@ -677,7 +674,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
677674
// might be covered by non-defaulted bindings.
678675
bool updatedBindingType = false;
679676
for (auto &literalBinding : literalBindings) {
680-
auto *protocol = literalBinding.DefaultedProtocol;
677+
auto *protocol = literalBinding.getDefaultedLiteralProtocol();
681678

682679
assert(protocol);
683680

@@ -716,7 +713,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
716713
}
717714

718715
for (auto &literalBinding : literalBindings) {
719-
auto *protocol = literalBinding.DefaultedProtocol;
716+
auto *protocol = literalBinding.getDefaultedLiteralProtocol();
720717
// For any literal type that has been covered, skip them.
721718
if (coveredLiteralProtocols.count(protocol) == 0)
722719
result.addPotentialBinding(std::move(literalBinding));
@@ -729,9 +726,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
729726
if (!exactTypes.insert(type->getCanonicalType()).second)
730727
continue;
731728

732-
result.addPotentialBinding({type, AllowedBindingKind::Exact,
733-
constraint->getKind(), nullptr,
734-
constraint->getLocator()});
729+
result.addPotentialBinding({type, AllowedBindingKind::Exact, constraint});
735730
}
736731

737732
// If there are no bindings, typeVar may be a hole.
@@ -745,9 +740,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
745740
if (locator->isLastElement<LocatorPathElt::MemberRefBase>())
746741
result.PotentiallyIncomplete = true;
747742

748-
result.addPotentialBinding({getASTContext().TheUnresolvedType,
749-
AllowedBindingKind::Exact, ConstraintKind::Defaultable, nullptr,
750-
typeVar->getImpl().getLocator()});
743+
result.addPotentialBinding(
744+
PotentialBinding::forHole(getASTContext(), locator));
751745
}
752746

753747
// Determine if the bindings only constrain the type variable from above with
@@ -918,11 +912,11 @@ bool TypeVarBindingProducer::computeNext() {
918912

919913
// If we have a protocol with a default type, look for alternative
920914
// types to the default.
921-
if (NumTries == 0 && binding.DefaultedProtocol) {
922-
auto knownKind = *(binding.DefaultedProtocol->getKnownProtocolKind());
915+
if (NumTries == 0 && binding.hasDefaultedLiteralProtocol()) {
916+
auto knownKind =
917+
*(binding.getDefaultedLiteralProtocol()->getKnownProtocolKind());
923918
for (auto altType : CS.getAlternativeLiteralTypes(knownKind)) {
924-
addNewBinding({altType, BindingKind::Subtypes, binding.BindingSource,
925-
binding.DefaultedProtocol});
919+
addNewBinding(binding.withSameSource(altType, BindingKind::Subtypes));
926920
}
927921
}
928922

@@ -939,10 +933,10 @@ bool TypeVarBindingProducer::computeNext() {
939933
if (auto otherTypeVar = objTy->getAs<TypeVariableType>()) {
940934
if (TypeVar->getImpl().canBindToLValue() ==
941935
otherTypeVar->getImpl().canBindToLValue()) {
942-
addNewBinding({objTy, binding.Kind, binding.BindingSource});
936+
addNewBinding(binding.withSameSource(objTy, binding.Kind));
943937
}
944938
} else {
945-
addNewBinding({objTy, binding.Kind, binding.BindingSource});
939+
addNewBinding(binding.withSameSource(objTy, binding.Kind));
946940
}
947941
}
948942
}
@@ -953,7 +947,7 @@ bool TypeVarBindingProducer::computeNext() {
953947
for (auto supertype : enumerateDirectSupertypes(type)) {
954948
// If we're not allowed to try this binding, skip it.
955949
if (auto simplifiedSuper = CS.checkTypeOfBinding(TypeVar, supertype))
956-
addNewBinding({*simplifiedSuper, binding.Kind, binding.BindingSource});
950+
addNewBinding(binding.withType(*simplifiedSuper));
957951
}
958952
}
959953

@@ -968,12 +962,13 @@ bool TypeVarBindingProducer::computeNext() {
968962

969963
bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
970964
auto type = Binding.BindingType;
971-
auto *locator = TypeVar->getImpl().getLocator();
965+
auto *srcLocator = Binding.getLocator();
966+
auto *dstLocator = TypeVar->getImpl().getLocator();
972967

973-
if (Binding.DefaultedProtocol) {
974-
type = cs.openUnboundGenericType(type, locator);
968+
if (Binding.hasDefaultedLiteralProtocol()) {
969+
type = cs.openUnboundGenericType(type, dstLocator);
975970
type = type->reconstituteSugar(/*recursive=*/false);
976-
} else if (Binding.BindingSource == ConstraintKind::ArgumentConversion &&
971+
} else if (srcLocator->isLastElement<LocatorPathElt::ApplyArgToParam>() &&
977972
!type->hasTypeVariable() && cs.isCollectionType(type)) {
978973
// If the type binding comes from the argument conversion, let's
979974
// instead of binding collection types directly, try to bind
@@ -984,27 +979,27 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
984979
auto UGT = UnboundGenericType::get(BGT->getDecl(), BGT->getParent(),
985980
BGT->getASTContext());
986981

987-
type = cs.openUnboundGenericType(UGT, locator);
982+
type = cs.openUnboundGenericType(UGT, dstLocator);
988983
type = type->reconstituteSugar(/*recursive=*/false);
989984
}
990985

991-
// FIXME: We want the locator that indicates where the binding came
992-
// from.
993-
cs.addConstraint(ConstraintKind::Bind, TypeVar, type, locator);
986+
cs.addConstraint(ConstraintKind::Bind, TypeVar, type, srcLocator);
994987

995988
// If this was from a defaultable binding note that.
996989
if (Binding.isDefaultableBinding()) {
997-
cs.DefaultedConstraints.push_back(Binding.DefaultableBinding);
998-
999-
if (locator->isForGenericParameter() && type->isHole()) {
1000-
// Drop `generic parameter` locator element so that all missing
1001-
// generic parameters related to the same path can be coalesced later.
1002-
auto path = locator->getPath();
1003-
auto genericParam = locator->getGenericParameter();
1004-
auto *fix = DefaultGenericArgument::create(cs, genericParam,
1005-
cs.getConstraintLocator(locator->getAnchor(), path.drop_back()));
1006-
if (cs.recordFix(fix))
1007-
return true;
990+
cs.DefaultedConstraints.push_back(srcLocator);
991+
992+
if (type->isHole()) {
993+
if (auto *GP = TypeVar->getImpl().getGenericParameter()) {
994+
auto path = dstLocator->getPath();
995+
// Drop `generic parameter` locator element so that all missing
996+
// generic parameters related to the same path can be coalesced later.
997+
auto *fix = DefaultGenericArgument::create(
998+
cs, GP,
999+
cs.getConstraintLocator(dstLocator->getAnchor(), path.drop_back()));
1000+
if (cs.recordFix(fix))
1001+
return true;
1002+
}
10081003
}
10091004
}
10101005

0 commit comments

Comments
 (0)