Skip to content

Commit 48afd65

Browse files
committed
[CSBindings] NFC: Convert checkTypeOfBinding into a static function
1 parent cb4e597 commit 48afd65

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4601,7 +4601,6 @@ class ConstraintSystem {
46014601
ConstraintKind bodyResultConstraintKind,
46024602
ConstraintLocatorBuilder locator);
46034603

4604-
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
46054604
Optional<PotentialBindings> determineBestBindings();
46064605

46074606
/// Infer bindings for the given type variable based on current

lib/Sema/CSBindings.cpp

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,46 @@ PotentialBindings ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
829829
return bindings;
830830
}
831831

832+
/// Check whether the given type can be used as a binding for the given
833+
/// type variable.
834+
///
835+
/// \returns the type to bind to, if the binding is okay.
836+
static Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) {
837+
// If the type references the type variable, don't permit the binding.
838+
SmallVector<TypeVariableType *, 4> referencedTypeVars;
839+
type->getTypeVariables(referencedTypeVars);
840+
if (count(referencedTypeVars, typeVar))
841+
return None;
842+
843+
// If type variable is not allowed to bind to `lvalue`,
844+
// let's check if type of potential binding has any
845+
// type variables, which are allowed to bind to `lvalue`,
846+
// and postpone such type from consideration.
847+
if (!typeVar->getImpl().canBindToLValue()) {
848+
for (auto *typeVar : referencedTypeVars) {
849+
if (typeVar->getImpl().canBindToLValue())
850+
return None;
851+
}
852+
}
853+
854+
{
855+
auto objType = type->getWithoutSpecifierType();
856+
857+
// If the type is a type variable itself, don't permit the binding.
858+
if (objType->is<TypeVariableType>())
859+
return None;
860+
861+
// Don't bind to a dependent member type, even if it's currently
862+
// wrapped in any number of optionals, because binding producer
863+
// might unwrap and try to attempt it directly later.
864+
if (objType->lookThroughAllOptionalTypes()->is<DependentMemberType>())
865+
return None;
866+
}
867+
868+
// Okay, allow the binding (with the simplified type).
869+
return type;
870+
}
871+
832872
Optional<PotentialBinding>
833873
PotentialBindings::inferFromRelational(Constraint *constraint) {
834874
assert(constraint->getClassification() ==
@@ -942,8 +982,7 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
942982
type = fnTy->withExtInfo(fnTy->getExtInfo().withNoEscape(false));
943983

944984
// Check whether we can perform this binding.
945-
// FIXME: this has a super-inefficient extraneous simplifyType() in it.
946-
if (auto boundType = CS.checkTypeOfBinding(TypeVar, type)) {
985+
if (auto boundType = checkTypeOfBinding(TypeVar, type)) {
947986
type = *boundType;
948987
if (type->hasTypeVariable()) {
949988
SmallVector<TypeVariableType *, 4> referencedVars;
@@ -1325,50 +1364,6 @@ void PotentialBindings::dump(llvm::raw_ostream &out, unsigned indent) const {
13251364
}
13261365
}
13271366

1328-
/// Check whether the given type can be used as a binding for the given
1329-
/// type variable.
1330-
///
1331-
/// \returns the type to bind to, if the binding is okay.
1332-
Optional<Type> ConstraintSystem::checkTypeOfBinding(TypeVariableType *typeVar,
1333-
Type type) const {
1334-
// Simplify the type.
1335-
type = simplifyType(type);
1336-
1337-
// If the type references the type variable, don't permit the binding.
1338-
SmallVector<TypeVariableType *, 4> referencedTypeVars;
1339-
type->getTypeVariables(referencedTypeVars);
1340-
if (count(referencedTypeVars, typeVar))
1341-
return None;
1342-
1343-
// If type variable is not allowed to bind to `lvalue`,
1344-
// let's check if type of potential binding has any
1345-
// type variables, which are allowed to bind to `lvalue`,
1346-
// and postpone such type from consideration.
1347-
if (!typeVar->getImpl().canBindToLValue()) {
1348-
for (auto *typeVar : referencedTypeVars) {
1349-
if (typeVar->getImpl().canBindToLValue())
1350-
return None;
1351-
}
1352-
}
1353-
1354-
{
1355-
auto objType = type->getWithoutSpecifierType();
1356-
1357-
// If the type is a type variable itself, don't permit the binding.
1358-
if (objType->is<TypeVariableType>())
1359-
return None;
1360-
1361-
// Don't bind to a dependent member type, even if it's currently
1362-
// wrapped in any number of optionals, because binding producer
1363-
// might unwrap and try to attempt it directly later.
1364-
if (objType->lookThroughAllOptionalTypes()->is<DependentMemberType>())
1365-
return None;
1366-
}
1367-
1368-
// Okay, allow the binding (with the simplified type).
1369-
return type;
1370-
}
1371-
13721367
// Given a possibly-Optional type, return the direct superclass of the
13731368
// (underlying) type wrapped in the same number of optional levels as
13741369
// type.
@@ -1491,7 +1486,7 @@ bool TypeVarBindingProducer::computeNext() {
14911486
if (binding.Kind == BindingKind::Supertypes) {
14921487
for (auto supertype : enumerateDirectSupertypes(type)) {
14931488
// If we're not allowed to try this binding, skip it.
1494-
if (auto simplifiedSuper = CS.checkTypeOfBinding(TypeVar, supertype))
1489+
if (auto simplifiedSuper = checkTypeOfBinding(TypeVar, supertype))
14951490
addNewBinding(binding.withType(*simplifiedSuper));
14961491
}
14971492
}

0 commit comments

Comments
 (0)