@@ -829,6 +829,46 @@ PotentialBindings ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
829
829
return bindings;
830
830
}
831
831
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
+
832
872
Optional<PotentialBinding>
833
873
PotentialBindings::inferFromRelational (Constraint *constraint) {
834
874
assert (constraint->getClassification () ==
@@ -942,8 +982,7 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
942
982
type = fnTy->withExtInfo (fnTy->getExtInfo ().withNoEscape (false ));
943
983
944
984
// 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)) {
947
986
type = *boundType;
948
987
if (type->hasTypeVariable ()) {
949
988
SmallVector<TypeVariableType *, 4 > referencedVars;
@@ -1325,50 +1364,6 @@ void PotentialBindings::dump(llvm::raw_ostream &out, unsigned indent) const {
1325
1364
}
1326
1365
}
1327
1366
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
-
1372
1367
// Given a possibly-Optional type, return the direct superclass of the
1373
1368
// (underlying) type wrapped in the same number of optional levels as
1374
1369
// type.
@@ -1491,7 +1486,7 @@ bool TypeVarBindingProducer::computeNext() {
1491
1486
if (binding.Kind == BindingKind::Supertypes) {
1492
1487
for (auto supertype : enumerateDirectSupertypes (type)) {
1493
1488
// 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))
1495
1490
addNewBinding (binding.withType (*simplifiedSuper));
1496
1491
}
1497
1492
}
0 commit comments