Skip to content

Commit 568c27f

Browse files
committed
[ConstraintSystem] Convert Opened{Existential}Types to a map vector to avoid duplicates
Same reasons as with `DefaultConstraints` but also improves code ergonomics around opened types.
1 parent cfcc852 commit 568c27f

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,8 @@ class ConstraintSystem {
24152415

24162416
/// A mapping from constraint locators to the set of opened types associated
24172417
/// with that locator.
2418-
SmallVector<std::pair<ConstraintLocator *, ArrayRef<OpenedType>>, 4>
2419-
OpenedTypes;
2418+
llvm::SmallMapVector<ConstraintLocator *, ArrayRef<OpenedType>, 4>
2419+
OpenedTypes;
24202420

24212421
/// The list of all generic requirements fixed along the current
24222422
/// solver path.
@@ -2430,11 +2430,11 @@ class ConstraintSystem {
24302430

24312431
/// A mapping from constraint locators to the opened existential archetype
24322432
/// used for the 'self' of an existential type.
2433-
SmallVector<std::pair<ConstraintLocator *, OpenedArchetypeType *>, 4>
2434-
OpenedExistentialTypes;
2433+
llvm::SmallMapVector<ConstraintLocator *, OpenedArchetypeType *, 4>
2434+
OpenedExistentialTypes;
24352435

24362436
/// The set of functions that have been transformed by a result builder.
2437-
std::vector<std::pair<AnyFunctionRef, AppliedBuilderTransform>>
2437+
llvm::MapVector<AnyFunctionRef, AppliedBuilderTransform>
24382438
resultBuilderTransformed;
24392439

24402440
/// Cache of the effects any closures visited.
@@ -4172,10 +4172,12 @@ class ConstraintSystem {
41724172
OpenedTypeMap *replacements = nullptr);
41734173

41744174
/// Retrieve a list of generic parameter types solver has "opened" (replaced
4175-
/// with a type variable) along the current path.
4176-
ArrayRef<std::pair<ConstraintLocator *, ArrayRef<OpenedType>>>
4177-
getOpenedTypes() const {
4178-
return OpenedTypes;
4175+
/// with a type variable) at the given location.
4176+
ArrayRef<OpenedType> getOpenedTypes(ConstraintLocator *locator) const {
4177+
auto substitutions = OpenedTypes.find(locator);
4178+
if (substitutions == OpenedTypes.end())
4179+
return {};
4180+
return substitutions->second;
41794181
}
41804182

41814183
private:

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,8 +1859,7 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
18591859
return elt.first == fn;
18601860
}) == resultBuilderTransformed.end() &&
18611861
"already transformed this body along this path!?!");
1862-
resultBuilderTransformed.push_back(
1863-
std::make_pair(fn, std::move(*applied)));
1862+
resultBuilderTransformed.insert(std::make_pair(fn, std::move(*applied)));
18641863

18651864
// If builder is applied to the closure expression then
18661865
// `closure body` to `closure result` matching should

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8893,13 +8893,10 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
88938893
if (builderType->hasTypeParameter()) {
88948894
// Find the opened type for this callee and substitute in the type
88958895
// parametes.
8896-
// FIXME: We should consider changing OpenedTypes to a MapVector.
8897-
for (const auto &opened : cs.getOpenedTypes()) {
8898-
if (opened.first == calleeLocator) {
8899-
OpenedTypeMap replacements(opened.second.begin(), opened.second.end());
8900-
builderType = cs.openType(builderType, replacements);
8901-
break;
8902-
}
8896+
auto substitutions = cs.getOpenedTypes(calleeLocator);
8897+
if (!substitutions.empty()) {
8898+
OpenedTypeMap replacements(substitutions.begin(), substitutions.end());
8899+
builderType = cs.openType(builderType, replacements);
89038900
}
89048901
assert(!builderType->hasTypeParameter());
89058902
}

lib/Sema/CSSolver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ void ConstraintSystem::applySolution(const Solution &solution) {
250250

251251
// Register the solution's opened types.
252252
for (const auto &opened : solution.OpenedTypes) {
253-
OpenedTypes.push_back(opened);
253+
OpenedTypes.insert(opened);
254254
}
255255

256256
// Register the solution's opened existential types.
257257
for (const auto &openedExistential : solution.OpenedExistentialTypes) {
258-
OpenedExistentialTypes.push_back(openedExistential);
258+
OpenedExistentialTypes.insert(openedExistential);
259259
}
260260

261261
// Register the defaulted type variables.
@@ -297,7 +297,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
297297
}
298298

299299
for (const auto &transformed : solution.resultBuilderTransformed) {
300-
resultBuilderTransformed.push_back(transformed);
300+
resultBuilderTransformed.insert(transformed);
301301
}
302302

303303
for (const auto &appliedWrapper : solution.appliedPropertyWrappers) {

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,8 @@ void ConstraintSystem::recordOpenedTypes(
12191219
OpenedType* openedTypes
12201220
= Allocator.Allocate<OpenedType>(replacements.size());
12211221
std::copy(replacements.begin(), replacements.end(), openedTypes);
1222-
OpenedTypes.push_back({ locatorPtr,
1223-
llvm::makeArrayRef(openedTypes,
1224-
replacements.size()) });
1222+
OpenedTypes.insert(
1223+
{locatorPtr, llvm::makeArrayRef(openedTypes, replacements.size())});
12251224
}
12261225

12271226
/// Determine how many levels of argument labels should be removed from the
@@ -1883,7 +1882,7 @@ ConstraintSystem::getTypeOfMemberReference(
18831882
}
18841883
} else if (baseObjTy->isExistentialType()) {
18851884
auto openedArchetype = OpenedArchetypeType::get(baseObjTy);
1886-
OpenedExistentialTypes.push_back(
1885+
OpenedExistentialTypes.insert(
18871886
{getConstraintLocator(locator), openedArchetype});
18881887
baseOpenedTy = openedArchetype;
18891888
}
@@ -5807,24 +5806,14 @@ getRequirementInfo(ConstraintSystem &cs, ConstraintLocator *reqLocator) {
58075806
auto newPath = path.drop_back(iter - path.rbegin() + 1);
58085807
auto *baseLoc = cs.getConstraintLocator(reqLocator->getAnchor(), newPath);
58095808

5810-
auto openedTypes = cs.getOpenedTypes();
5811-
auto substitutions = llvm::find_if(
5812-
openedTypes,
5813-
[&baseLoc](
5814-
const std::pair<ConstraintLocator *, ArrayRef<OpenedType>> &entry) {
5815-
return entry.first == baseLoc;
5816-
});
5817-
5818-
if (substitutions == openedTypes.end())
5819-
return None;
5820-
5809+
auto substitutions = cs.getOpenedTypes(baseLoc);
58215810
auto replacement =
5822-
llvm::find_if(substitutions->second, [&GP](const OpenedType &entry) {
5811+
llvm::find_if(substitutions, [&GP](const OpenedType &entry) {
58235812
auto *typeVar = entry.second;
58245813
return typeVar->getImpl().getGenericParameter() == GP;
58255814
});
58265815

5827-
if (replacement == substitutions->second.end())
5816+
if (replacement == substitutions.end())
58285817
return None;
58295818

58305819
auto *repr = cs.getRepresentative(replacement->second);

0 commit comments

Comments
 (0)