Skip to content

Commit a5cf7d3

Browse files
committed
RequirementMachine: The reduced type of a PackExpansionType has a reduced *shape* for the count type
I don't have a test case for this but it was bound to come up eventually.
1 parent 0bece01 commit a5cf7d3

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

lib/AST/GenericSignature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ GenericSignatureImpl::lookupNestedType(Type type, Identifier name) const {
536536

537537
Type
538538
GenericSignatureImpl::getReducedShape(Type type) const {
539-
return getRequirementMachine()->getReducedShape(type);
539+
return getRequirementMachine()->getReducedShape(type, getGenericParams());
540540
}
541541

542542
bool

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ RequirementMachine::getLocalRequirements(
5757

5858
GenericSignature::LocalRequirements result;
5959
result.anchor = Map.getTypeForTerm(term, genericParams);
60-
result.packShape = getReducedShape(depType);
60+
result.packShape = getReducedShape(depType, genericParams);
6161

6262
auto *props = Map.lookUpProperties(term);
6363
if (!props)
@@ -365,6 +365,17 @@ Type RequirementMachine::getReducedType(
365365
if (!t->hasTypeParameter())
366366
return t;
367367

368+
// The reduced type of a PackExpansionType has a reduced *shape* for
369+
// the count type.
370+
if (auto *packExpansionType = t->getAs<PackExpansionType>()) {
371+
auto reducedPattern = getReducedType(packExpansionType->getPatternType(),
372+
genericParams);
373+
auto reducedShape = packExpansionType->getCountType();
374+
if (reducedShape->isParameterPack())
375+
reducedShape = getReducedShape(reducedShape, genericParams);
376+
return Type(PackExpansionType::get(reducedPattern, reducedShape));
377+
}
378+
368379
if (!t->isTypeParameter())
369380
return None;
370381

@@ -727,12 +738,12 @@ RequirementMachine::getReducedShapeTerm(Type type) const {
727738
return reducedTerm;
728739
}
729740

730-
Type RequirementMachine::getReducedShape(Type type) const {
741+
Type RequirementMachine::getReducedShape(Type type,
742+
TypeArrayView<GenericTypeParamType> genericParams) const {
731743
if (!type->isParameterPack())
732744
return Type();
733745

734-
return Map.getTypeForTerm(getReducedShapeTerm(type),
735-
getGenericParams());
746+
return Map.getTypeForTerm(getReducedShapeTerm(type), genericParams);
736747
}
737748

738749
bool RequirementMachine::haveSameShape(Type type1, Type type2) const {

lib/AST/RequirementMachine/RequirementMachine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ class RequirementMachine final {
164164
MutableTerm getReducedShapeTerm(Type type) const;
165165

166166
public:
167-
Type getReducedShape(Type type) const;
167+
Type getReducedShape(Type type,
168+
TypeArrayView<GenericTypeParamType> genericParams) const;
168169

169170
bool haveSameShape(Type type1, Type type2) const;
170171

lib/AST/Type.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4684,7 +4684,18 @@ static Type substType(Type derivedType,
46844684
boxTy->getLayout(),
46854685
newSubMap);
46864686
}
4687-
4687+
4688+
if (auto packExpansionTy = dyn_cast<PackExpansionType>(type)) {
4689+
auto patternTy = substType(packExpansionTy->getPatternType(),
4690+
substitutions, lookupConformances, options);
4691+
auto countTy = substType(packExpansionTy->getCountType(),
4692+
substitutions, lookupConformances, options);
4693+
if (auto *archetypeTy = countTy->getAs<PackArchetypeType>())
4694+
countTy = archetypeTy->getReducedShape();
4695+
4696+
return Type(PackExpansionType::get(patternTy, countTy)->expand());
4697+
}
4698+
46884699
if (auto silFnTy = dyn_cast<SILFunctionType>(type)) {
46894700
if (silFnTy->isPolymorphic())
46904701
return None;

0 commit comments

Comments
 (0)