Skip to content

Commit 06c4f29

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 06c4f29

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
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: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10404,8 +10404,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
1040410404
// equality instead of argument application constraint, so allowing
1040510405
// them to bind member could mean missing valid hole positions in
1040610406
// the pattern.
10407-
assignFixedType(memberTypeVar, PlaceholderType::get(getASTContext(),
10408-
memberTypeVar));
10407+
recordTypeVariablesAsHoles(memberTypeVar);
1040910408
} else {
1041010409
recordPotentialHole(memberTypeVar);
1041110410
}
@@ -14010,8 +14009,24 @@ void ConstraintSystem::recordAnyTypeVarAsPotentialHole(Type type) {
1401014009
return;
1401114010

1401214011
type.visit([&](Type type) {
14013-
if (auto *typeVar = type->getAs<TypeVariableType>())
14012+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
1401414013
typeVar->getImpl().enableCanBindToHole(getSavedBindings());
14014+
}
14015+
});
14016+
}
14017+
14018+
void ConstraintSystem::recordTypeVariablesAsHoles(Type type) {
14019+
type.visit([&](Type componentTy) {
14020+
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
14021+
// Ignore bound type variables. This can happen if a type variable
14022+
// occurs in multiple positions and/or if type hasn't been fully
14023+
// simplified before this call.
14024+
if (typeVar->getImpl().hasRepresentativeOrFixed())
14025+
return;
14026+
14027+
assignFixedType(typeVar,
14028+
PlaceholderType::get(getASTContext(), typeVar));
14029+
}
1401514030
});
1401614031
}
1401714032

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)