Skip to content

Commit 715de13

Browse files
committed
[Sema] Key opened pack element generic on the shape class of the expansion.
1 parent 68ceda4 commit 715de13

File tree

7 files changed

+28
-15
lines changed

7 files changed

+28
-15
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,8 @@ class ASTContext final {
13691369
///
13701370
/// This drops the parameter pack bit from each generic parameter,
13711371
/// and converts same-element requirements to same-type requirements.
1372-
CanGenericSignature getOpenedElementSignature(CanGenericSignature baseGenericSig);
1372+
CanGenericSignature getOpenedElementSignature(CanGenericSignature baseGenericSig,
1373+
CanType shapeClass);
13731374

13741375
GenericSignature getOverrideGenericSignature(const ValueDecl *base,
13751376
const ValueDecl *derived);

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3987,7 +3987,8 @@ class ConstraintSystem {
39873987
void addPackElementEnvironment(PackExpansionExpr *expr);
39883988

39893989
/// Get the opened element generic environment for the given locator.
3990-
GenericEnvironment *getPackElementEnvironment(ConstraintLocator *locator);
3990+
GenericEnvironment *getPackElementEnvironment(ConstraintLocator *locator,
3991+
CanType shapeClass);
39913992

39923993
/// Retrieve the constraint locator for the given anchor and
39933994
/// path, uniqued and automatically infer the summary flags

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ struct ASTContext::Implementation {
356356
llvm::DenseMap<std::pair<CanType, const GenericSignatureImpl *>, CanGenericSignature>
357357
ExistentialSignatures;
358358

359-
/// The element signature for a generic signature, constructed by dropping
360-
/// the parameter pack bit from generic parameters.
361-
llvm::DenseMap<const GenericSignatureImpl *,
359+
/// The element signature for a generic signature, which contains a clone
360+
/// of the context generic signature with new type parameters and requirements
361+
/// for opened pack elements in the given shape equivalence class.
362+
llvm::DenseMap<std::pair<CanType, const GenericSignatureImpl *>,
362363
CanGenericSignature> ElementSignatures;
363364

364365
/// Overridden declarations.
@@ -5624,9 +5625,11 @@ ASTContext::getOpenedExistentialSignature(Type type, GenericSignature parentSig)
56245625
}
56255626

56265627
CanGenericSignature
5627-
ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig) {
5628+
ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
5629+
CanType shapeClass) {
56285630
auto &sigs = getImpl().ElementSignatures;
5629-
auto found = sigs.find(baseGenericSig.getPointer());
5631+
auto key = std::make_pair(shapeClass, baseGenericSig.getPointer());
5632+
auto found = sigs.find(key);
56305633
if (found != sigs.end())
56315634
return found->second;
56325635

@@ -5718,7 +5721,7 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig) {
57185721
auto elementSig = buildGenericSignature(
57195722
*this, GenericSignature(), genericParams, requirements)
57205723
.getCanonicalSignature();
5721-
sigs[baseGenericSig.getPointer()] = elementSig;
5724+
sigs[key] = elementSig;
57225725
return elementSig;
57235726
}
57245727

lib/IRGen/GenPack.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ static void emitPackExpansionType(IRGenFunction &IGF,
174174
auto genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
175175

176176
// Create an opened element signature and environment.
177-
auto elementSig = IGF.IGM.Context.getOpenedElementSignature(genericSig);
177+
auto elementSig = IGF.IGM.Context.getOpenedElementSignature(
178+
genericSig, expansionTy.getCountType());
178179
auto *elementEnv = GenericEnvironment::forOpenedElement(
179180
elementSig, UUID::fromTime(), subMap);
180181

@@ -325,4 +326,4 @@ void irgen::cleanupTypeMetadataPack(IRGenFunction &IGF,
325326
IGF.Builder.CreateLifetimeEnd(pack.getAddress(),
326327
IGF.IGM.getPointerSize() * (*elementCount));
327328
}
328-
}
329+
}

lib/Sema/CSApply.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,11 +3818,16 @@ namespace {
38183818
}
38193819

38203820
Expr *visitPackExpansionExpr(PackExpansionExpr *expr) {
3821+
simplifyExprType(expr);
3822+
3823+
// Set the opened pack element environment for this pack expansion.
3824+
auto expansionTy = cs.getType(expr)->castTo<PackExpansionType>();
38213825
auto *locator = cs.getConstraintLocator(expr);
3822-
auto *environment = cs.getPackElementEnvironment(locator);
3826+
auto *environment = cs.getPackElementEnvironment(locator,
3827+
expansionTy->getCountType()->getCanonicalType());
38233828
expr->setGenericEnvironment(environment);
38243829

3825-
return simplifyExprType(expr);
3830+
return expr;
38263831
}
38273832

38283833
Expr *visitPackElementExpr(PackElementExpr *expr) {

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,8 @@ void PotentialBindings::infer(Constraint *constraint) {
14241424
// Produce a potential binding to the opened element archetype corresponding
14251425
// to the pack type.
14261426
packType = packType->mapTypeOutOfContext();
1427-
auto *elementEnv = CS.getPackElementEnvironment(constraint->getLocator());
1427+
auto *elementEnv = CS.getPackElementEnvironment(constraint->getLocator(),
1428+
packType->getReducedShape());
14281429
auto elementType = elementEnv->mapPackTypeIntoElementContext(packType);
14291430
addPotentialBinding({elementType, AllowedBindingKind::Exact, constraint});
14301431

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,15 +643,16 @@ void ConstraintSystem::addPackElementEnvironment(PackExpansionExpr *expr) {
643643
}
644644

645645
GenericEnvironment *
646-
ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator) {
646+
ConstraintSystem::getPackElementEnvironment(ConstraintLocator *locator,
647+
CanType shapeClass) {
647648
auto result = PackExpansionEnvironments.find(locator);
648649
if (result == PackExpansionEnvironments.end())
649650
return nullptr;
650651

651652
auto uuid = result->second;
652653
auto &ctx = getASTContext();
653654
auto elementSig = ctx.getOpenedElementSignature(
654-
DC->getGenericSignatureOfContext().getCanonicalSignature());
655+
DC->getGenericSignatureOfContext().getCanonicalSignature(), shapeClass);
655656
auto *contextEnv = DC->getGenericEnvironmentOfContext();
656657
auto contextSubs = contextEnv->getForwardingSubstitutionMap();
657658
return GenericEnvironment::forOpenedElement(elementSig, uuid, contextSubs);

0 commit comments

Comments
 (0)