Skip to content

Commit e7f82bc

Browse files
committed
[ConstraintSystem] Record both the UUID and the shape class for opened pack
element environments. This allows the constraint system to ensure that for a given pack expansion locator, the given shape class is always the same when requesting the element environment. If the shape class differs, it means there's a same-shape requirement failure, which will be diagnosed via the ShapeOf constraint simplification.
1 parent db4b16e commit e7f82bc

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,8 @@ class Solution {
15051505

15061506
/// The pack expansion environment that can open pack elements for
15071507
/// a given locator.
1508-
llvm::DenseMap<ConstraintLocator *, UUID> PackExpansionEnvironments;
1508+
llvm::DenseMap<ConstraintLocator *, std::pair<UUID, Type>>
1509+
PackExpansionEnvironments;
15091510

15101511
/// The locators of \c Defaultable constraints whose defaults were used.
15111512
llvm::SmallPtrSet<ConstraintLocator *, 2> DefaultedConstraints;
@@ -3067,7 +3068,8 @@ class ConstraintSystem {
30673068
llvm::SmallMapVector<ConstraintLocator *, OpenedArchetypeType *, 4>
30683069
OpenedExistentialTypes;
30693070

3070-
llvm::SmallMapVector<ConstraintLocator *, UUID, 4> PackExpansionEnvironments;
3071+
llvm::SmallMapVector<ConstraintLocator *, std::pair<UUID, Type>, 4>
3072+
PackExpansionEnvironments;
30713073

30723074
/// The set of functions that have been transformed by a result builder.
30733075
llvm::MapVector<AnyFunctionRef, AppliedBuilderTransform>
@@ -4031,9 +4033,6 @@ class ConstraintSystem {
40314033
std::pair<Type, OpenedArchetypeType *> openExistentialType(
40324034
Type type, ConstraintLocator *locator);
40334035

4034-
/// Add the given pack expansion as an opened pack element environment.
4035-
void addPackElementEnvironment(PackExpansionExpr *expr);
4036-
40374036
/// Get the opened element generic environment for the given locator.
40384037
GenericEnvironment *getPackElementEnvironment(ConstraintLocator *locator,
40394038
CanType shapeClass);

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5147,6 +5147,7 @@ GenericEnvironment::forOpenedElement(GenericSignature signature,
51475147
if (found != openedElementEnvironments.end()) {
51485148
auto *existingEnv = found->second;
51495149
assert(existingEnv->getGenericSignature().getPointer() == signature.getPointer());
5150+
assert(existingEnv->getOpenedElementShapeClass()->isEqual(shapeClass));
51505151
assert(existingEnv->getOpenedElementUUID() == uuid);
51515152

51525153
return existingEnv;

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,6 @@ namespace {
11011101
ConstraintSystem &getConstraintSystem() const { return CS; }
11021102

11031103
void addPackElementEnvironment(PackExpansionExpr *expr) {
1104-
CS.addPackElementEnvironment(expr);
11051104
PackElementEnvironments.push_back(expr);
11061105
}
11071106

lib/Sema/ConstraintSystem.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -649,29 +649,31 @@ std::pair<Type, OpenedArchetypeType *> ConstraintSystem::openExistentialType(
649649
return {result, opened};
650650
}
651651

652-
void ConstraintSystem::addPackElementEnvironment(PackExpansionExpr *expr) {
653-
auto *locator = getConstraintLocator(expr);
654-
PackExpansionEnvironments[locator] = UUID::fromTime();
655-
}
656-
657652
GenericEnvironment *
658653
ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator,
659654
CanType shapeClass) {
655+
assert(locator->directlyAt<PackExpansionExpr>());
656+
657+
std::pair<UUID, Type> uuidAndShape;
660658
auto result = PackExpansionEnvironments.find(locator);
661-
if (result == PackExpansionEnvironments.end())
662-
return nullptr;
659+
if (result == PackExpansionEnvironments.end()) {
660+
uuidAndShape = std::make_pair(UUID::fromTime(), shapeClass);
661+
PackExpansionEnvironments[locator] = uuidAndShape;
662+
} else {
663+
uuidAndShape = result->second;
664+
}
663665

664-
if (!shapeClass->is<PackArchetypeType>())
666+
if (!shapeClass->is<PackArchetypeType>() ||
667+
!shapeClass->isEqual(uuidAndShape.second))
665668
return nullptr;
666669

667-
auto uuid = result->second;
668670
auto &ctx = getASTContext();
669671
auto elementSig = ctx.getOpenedElementSignature(
670672
DC->getGenericSignatureOfContext().getCanonicalSignature(), shapeClass);
671673
auto *contextEnv = DC->getGenericEnvironmentOfContext();
672674
auto contextSubs = contextEnv->getForwardingSubstitutionMap();
673-
return GenericEnvironment::forOpenedElement(elementSig, uuid, shapeClass,
674-
contextSubs);
675+
return GenericEnvironment::forOpenedElement(elementSig, uuidAndShape.first,
676+
shapeClass, contextSubs);
675677
}
676678

677679
/// Extend the given depth map by adding depths for all of the subexpressions

0 commit comments

Comments
 (0)