Skip to content

Commit 8a26df8

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.
1 parent b081bdb commit 8a26df8

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
@@ -3337,6 +3337,13 @@ class ConstraintSystem {
33373337
void recordPotentialHole(TypeVariableType *typeVar);
33383338
void recordAnyTypeVarAsPotentialHole(Type type);
33393339

3340+
/// Record all unbound type variables that occur in the given type
3341+
/// as being bound to "hole" type represented by \c PlaceholderType
3342+
/// in this constraint system.
3343+
///
3344+
/// \param type The type on which to holeify.
3345+
void recordTypeVariablesAsHoles(Type type);
3346+
33403347
void recordMatchCallArgumentResult(ConstraintLocator *locator,
33413348
MatchCallArgumentResult result);
33423349

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
@@ -6159,14 +6159,7 @@ bool ConstraintSystem::repairFailures(
61596159
// unrelated fixes, let's proactively bind all of the pattern
61606160
// elemnts to holes here.
61616161
if (lhs->isAny()) {
6162-
rhs.visit([&](Type type) {
6163-
if (auto *typeVar = type->getAs<TypeVariableType>()) {
6164-
if (!getFixedType(typeVar)) {
6165-
assignFixedType(typeVar,
6166-
PlaceholderType::get(getASTContext(), typeVar));
6167-
}
6168-
}
6169-
});
6162+
recordTypeVariablesAsHoles(rhs);
61706163
}
61716164

61726165
conversionsOrFixes.push_back(CollectionElementContextualMismatch::create(
@@ -10404,8 +10397,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
1040410397
// equality instead of argument application constraint, so allowing
1040510398
// them to bind member could mean missing valid hole positions in
1040610399
// the pattern.
10407-
assignFixedType(memberTypeVar, PlaceholderType::get(getASTContext(),
10408-
memberTypeVar));
10400+
recordTypeVariablesAsHoles(memberTypeVar);
1040910401
} else {
1041010402
recordPotentialHole(memberTypeVar);
1041110403
}
@@ -14010,8 +14002,24 @@ void ConstraintSystem::recordAnyTypeVarAsPotentialHole(Type type) {
1401014002
return;
1401114003

1401214004
type.visit([&](Type type) {
14013-
if (auto *typeVar = type->getAs<TypeVariableType>())
14005+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
1401414006
typeVar->getImpl().enableCanBindToHole(getSavedBindings());
14007+
}
14008+
});
14009+
}
14010+
14011+
void ConstraintSystem::recordTypeVariablesAsHoles(Type type) {
14012+
type.visit([&](Type componentTy) {
14013+
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
14014+
// Ignore bound type variables. This can happen if a type variable
14015+
// occurs in multiple positions and/or if type hasn't been fully
14016+
// simplified before this call.
14017+
if (typeVar->getImpl().hasRepresentativeOrFixed())
14018+
return;
14019+
14020+
assignFixedType(typeVar,
14021+
PlaceholderType::get(getASTContext(), typeVar));
14022+
}
1401514023
});
1401614024
}
1401714025

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)