Skip to content

Commit db7586e

Browse files
committed
[Constraint System] Fix the shape class and context substitiutions for
opened element generic environments containing same-element requirements.
1 parent 5b443e5 commit db7586e

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

lib/AST/GenericSignature.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,16 @@ void GenericSignatureImpl::forEachParam(
105105

106106
for (auto req : getRequirements()) {
107107
GenericTypeParamType *gp;
108+
bool isCanonical = false;
108109
switch (req.getKind()) {
109110
case RequirementKind::SameType: {
111+
if (req.getSecondType()->isParameterPack() !=
112+
req.getFirstType()->isParameterPack()) {
113+
// This is a same-element requirement, which does not make
114+
// type parameters non-canonical.
115+
isCanonical = true;
116+
}
117+
110118
if (auto secondGP = req.getSecondType()->getAs<GenericTypeParamType>()) {
111119
// If two generic parameters are same-typed, then the right-hand one
112120
// is non-canonical.
@@ -136,7 +144,7 @@ void GenericSignatureImpl::forEachParam(
136144
}
137145

138146
unsigned index = GenericParamKey(gp).findIndexIn(genericParams);
139-
genericParamsAreCanonical[index] = false;
147+
genericParamsAreCanonical[index] = isCanonical;
140148
}
141149

142150
// Call the callback with each parameter and the result of the above analysis.

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,6 +3843,11 @@ namespace {
38433843
auto *locator = cs.getConstraintLocator(expr);
38443844
auto *environment = cs.getPackElementEnvironment(locator,
38453845
expansionTy->getCountType()->getCanonicalType());
3846+
3847+
// Assert that we have an opened element environment, otherwise we'll get
3848+
// an ASTVerifier crash when pack archetypes or element archetypes appear
3849+
// inside the pack expansion expression.
3850+
assert(environment);
38463851
expr->setGenericEnvironment(environment);
38473852

38483853
return expr;

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,18 +3125,9 @@ namespace {
31253125
auto expansionType =
31263126
CS.getType(packEnvironment)->castTo<PackExpansionType>();
31273127
CS.addConstraint(ConstraintKind::ShapeOf, expansionType->getCountType(),
3128-
elementType,
3128+
packType,
31293129
CS.getConstraintLocator(packEnvironment,
31303130
ConstraintLocator::PackShape));
3131-
auto *elementShape = CS.createTypeVariable(
3132-
CS.getConstraintLocator(expr, ConstraintLocator::PackShape),
3133-
TVO_CanBindToPack);
3134-
CS.addConstraint(
3135-
ConstraintKind::ShapeOf, elementShape, elementType,
3136-
CS.getConstraintLocator(expr, ConstraintLocator::PackShape));
3137-
CS.addConstraint(
3138-
ConstraintKind::Equal, elementShape, expansionType->getCountType(),
3139-
CS.getConstraintLocator(expr, ConstraintLocator::PackShape));
31403131
} else {
31413132
CS.recordFix(AllowInvalidPackReference::create(
31423133
CS, packType, CS.getConstraintLocator(expr->getPackRefExpr())));

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13584,6 +13584,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
1358413584
return SolutionKind::Solved;
1358513585
}
1358613586

13587+
if (isSingleUnlabeledPackExpansionTuple(packTy)) {
13588+
auto *packVar =
13589+
createTypeVariable(getConstraintLocator(locator), TVO_CanBindToPack);
13590+
addConstraint(ConstraintKind::MaterializePackExpansion, packTy,
13591+
packVar,
13592+
getConstraintLocator(locator, {ConstraintLocator::Member}));
13593+
addConstraint(ConstraintKind::ShapeOf, shapeTy, packVar, locator);
13594+
return SolutionKind::Solved;
13595+
}
13596+
1358713597
// Map element archetypes to the pack context to check for equality.
1358813598
if (packTy->hasElementArchetype()) {
1358913599
auto *packEnv = DC->getGenericEnvironmentOfContext();

test/Constraints/pack-expansion-expressions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ func outerArchetype<each T, U>(t: repeat each T, u: U) where repeat each T: P {
6363
}
6464

6565
func sameElement<each T, U>(t: repeat each T, u: U) where repeat each T: P, repeat each T == U {
66-
// FIXME
67-
// let _: (repeat each T) = (repeat (each t).f(u))
66+
let _ = (repeat (each t).f(u))
6867
}
6968

7069
func forEachEach<each C, U>(c: repeat each C, function: (U) -> Void)

0 commit comments

Comments
 (0)