Skip to content

Commit b321374

Browse files
committed
[ConstraintSystem] NFC: Standardize the way of type holefication
Introduce `ConstraintSystem::recordTypeVariablesAsHoles` as a standard way to record that unbound type variables found in a type are holes in the given constraint system. (cherry picked from commit 8a26df8)
1 parent ddf2d09 commit b321374

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,6 +3353,13 @@ class ConstraintSystem {
33533353
void recordPotentialHole(TypeVariableType *typeVar);
33543354
void recordAnyTypeVarAsPotentialHole(Type type);
33553355

3356+
/// Record all unbound type variables that occur in the given type
3357+
/// as being bound to "hole" type represented by \c PlaceholderType
3358+
/// in this constraint system.
3359+
///
3360+
/// \param type The type on which to holeify.
3361+
void recordTypeVariablesAsHoles(Type type);
3362+
33563363
void recordMatchCallArgumentResult(ConstraintLocator *locator,
33573364
MatchCallArgumentResult result);
33583365

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,13 +1174,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
11741174

11751175
if (auto *closure =
11761176
getAsExpr<ClosureExpr>(fn.getAbstractClosureExpr())) {
1177-
auto closureTy = getClosureType(closure);
1178-
simplifyType(closureTy).visit([&](Type componentTy) {
1179-
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
1180-
assignFixedType(typeVar,
1181-
PlaceholderType::get(getASTContext(), typeVar));
1182-
}
1183-
});
1177+
recordTypeVariablesAsHoles(getClosureType(closure));
11841178
}
11851179

11861180
return getTypeMatchSuccess();

lib/Sema/CSSimplify.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6230,14 +6230,7 @@ bool ConstraintSystem::repairFailures(
62306230
// unrelated fixes, let's proactively bind all of the pattern
62316231
// elemnts to holes here.
62326232
if (lhs->isAny()) {
6233-
rhs.visit([&](Type type) {
6234-
if (auto *typeVar = type->getAs<TypeVariableType>()) {
6235-
if (!getFixedType(typeVar)) {
6236-
assignFixedType(typeVar,
6237-
PlaceholderType::get(getASTContext(), typeVar));
6238-
}
6239-
}
6240-
});
6233+
recordTypeVariablesAsHoles(rhs);
62416234
}
62426235

62436236
conversionsOrFixes.push_back(CollectionElementContextualMismatch::create(
@@ -10560,8 +10553,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
1056010553
// equality instead of argument application constraint, so allowing
1056110554
// them to bind member could mean missing valid hole positions in
1056210555
// the pattern.
10563-
assignFixedType(memberTypeVar, PlaceholderType::get(getASTContext(),
10564-
memberTypeVar));
10556+
recordTypeVariablesAsHoles(memberTypeVar);
1056510557
} else {
1056610558
recordPotentialHole(memberTypeVar);
1056710559
}
@@ -14348,8 +14340,24 @@ void ConstraintSystem::recordAnyTypeVarAsPotentialHole(Type type) {
1434814340
return;
1434914341

1435014342
type.visit([&](Type type) {
14351-
if (auto *typeVar = type->getAs<TypeVariableType>())
14343+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
1435214344
typeVar->getImpl().enableCanBindToHole(getSavedBindings());
14345+
}
14346+
});
14347+
}
14348+
14349+
void ConstraintSystem::recordTypeVariablesAsHoles(Type type) {
14350+
type.visit([&](Type componentTy) {
14351+
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
14352+
// Ignore bound type variables. This can happen if a type variable
14353+
// occurs in multiple positions and/or if type hasn't been fully
14354+
// simplified before this call.
14355+
if (typeVar->getImpl().hasRepresentativeOrFixed())
14356+
return;
14357+
14358+
assignFixedType(typeVar,
14359+
PlaceholderType::get(getASTContext(), typeVar));
14360+
}
1435314361
});
1435414362
}
1435514363

lib/Sema/CSStep.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,13 +1103,6 @@ void ConjunctionStep::SolverSnapshot::applySolution(const Solution &solution) {
11031103
if (score.Data[SK_Fix] == 0)
11041104
return;
11051105

1106-
auto holeify = [&](Type componentTy) {
1107-
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
1108-
CS.assignFixedType(
1109-
typeVar, PlaceholderType::get(CS.getASTContext(), typeVar));
1110-
}
1111-
};
1112-
11131106
// If this conjunction represents a closure and inference
11141107
// has failed, let's bind all of unresolved type variables
11151108
// in its interface type to holes to avoid extraneous
@@ -1118,14 +1111,13 @@ void ConjunctionStep::SolverSnapshot::applySolution(const Solution &solution) {
11181111
if (locator->directlyAt<ClosureExpr>()) {
11191112
auto closureTy =
11201113
CS.getClosureType(castToExpr<ClosureExpr>(locator->getAnchor()));
1121-
1122-
CS.simplifyType(closureTy).visit(holeify);
1114+
CS.recordTypeVariablesAsHoles(closureTy);
11231115
}
11241116

11251117
// Same for a SingleValueStmtExpr, turn any unresolved type variables present
11261118
// in its type into holes.
11271119
if (locator->isForSingleValueStmtConjunction()) {
11281120
auto *SVE = castToExpr<SingleValueStmtExpr>(locator->getAnchor());
1129-
CS.simplifyType(CS.getType(SVE)).visit(holeify);
1121+
CS.recordTypeVariablesAsHoles(CS.getType(SVE));
11301122
}
11311123
}

0 commit comments

Comments
 (0)