Skip to content

Commit 77346c7

Browse files
committed
Sema: Factor out isDirectRequirement() from two places that check this
1 parent e88c8dc commit 77346c7

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ void ConstraintGraphNode::initBindingSet() {
3838
Set.emplace(CG.getConstraintSystem(), TypeVar, Potential);
3939
}
4040

41+
static bool isDirectRequirement(ConstraintSystem &cs,
42+
TypeVariableType *typeVar,
43+
Constraint *constraint) {
44+
if (auto *other = constraint->getFirstType()->getAs<TypeVariableType>()) {
45+
return typeVar == cs.getRepresentative(other);
46+
}
47+
48+
return false;
49+
}
50+
4151
BindingSet::BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
4252
const PotentialBindings &info)
4353
: CS(CS), TypeVar(TypeVar), Info(info) {
@@ -54,10 +64,8 @@ BindingSet::BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
5464
case ConstraintKind::Defaultable:
5565
case ConstraintKind::FallbackType:
5666
// Do these in a separate pass.
57-
if (CS.getFixedTypeRecursive(constraint->getFirstType(), true)
58-
->getAs<TypeVariableType>() == TypeVar) {
67+
if (isDirectRequirement(CS, TypeVar, constraint))
5968
addDefault(constraint);
60-
}
6169
break;
6270

6371
default:
@@ -1049,16 +1057,7 @@ void BindingSet::addLiteralRequirement(Constraint *constraint) {
10491057
if (Literals.count(protocol) > 0)
10501058
return;
10511059

1052-
auto isDirectRequirement = [&](Constraint *constraint) -> bool {
1053-
if (auto *typeVar = constraint->getFirstType()->getAs<TypeVariableType>()) {
1054-
auto *repr = CS.getRepresentative(typeVar);
1055-
return repr == TypeVar;
1056-
}
1057-
1058-
return false;
1059-
};
1060-
1061-
bool isDirect = isDirectRequirement(constraint);
1060+
bool isDirect = isDirectRequirement(CS, TypeVar, constraint);
10621061

10631062
Type defaultType;
10641063
// `ExpressibleByNilLiteral` doesn't have a default type.

0 commit comments

Comments
 (0)