@@ -8998,67 +8998,78 @@ ConstraintSystem::simplifyBindTupleOfFunctionParamsConstraint(
8998
8998
return SolutionKind::Solved;
8999
8999
}
9000
9000
9001
- static Type lookThroughSingletonPackExpansion(Type ty) {
9002
- if (auto pack = ty->getAs<PackType>()) {
9003
- if (pack->getNumElements() == 1) {
9004
- if (auto expansion = pack->getElementType(0)->getAs<PackExpansionType>()) {
9005
- auto countType = expansion->getCountType();
9006
- if (countType->isEqual(expansion->getPatternType()))
9007
- return countType;
9008
- }
9009
- }
9010
- }
9011
- return ty;
9012
- }
9013
-
9014
9001
ConstraintSystem::SolutionKind
9015
9002
ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
9016
9003
TypeMatchOptions flags,
9017
9004
ConstraintLocatorBuilder locator) {
9018
9005
auto elementType = simplifyType(first, flags);
9019
- auto packType = simplifyType(second, flags);
9006
+ auto patternType = simplifyType(second, flags);
9020
9007
9021
- if (elementType->hasTypeVariable() || packType->hasTypeVariable() ) {
9008
+ auto formUnsolved = [&]( ) {
9022
9009
if (!flags.contains(TMF_GenerateConstraints))
9023
9010
return SolutionKind::Unsolved;
9024
9011
9025
- auto *loc = getConstraintLocator(locator);
9026
9012
addUnsolvedConstraint(
9027
- Constraint::create(*this, ConstraintKind::PackElementOf,
9028
- first, second, loc ));
9013
+ Constraint::create(*this, ConstraintKind::PackElementOf, first, second,
9014
+ getConstraintLocator(locator) ));
9029
9015
9030
9016
return SolutionKind::Solved;
9017
+ };
9018
+
9019
+ // If neither side is fully resolved yet, there is nothing we can do.
9020
+ if (elementType->hasTypeVariable() && patternType->hasTypeVariable())
9021
+ return formUnsolved();
9022
+
9023
+ if (shouldAttemptFixes()) {
9024
+ if (elementType->isPlaceholder() || patternType->isPlaceholder())
9025
+ return SolutionKind::Solved;
9031
9026
}
9032
9027
9033
- // FIXME: I'm not sure this is actually necessary; I may only be seeing
9034
- // this because of something I've screwed up in element generic
9035
- // environments.
9036
- elementType = lookThroughSingletonPackExpansion(elementType);
9028
+ // Let's try to resolve element type based on the pattern type.
9029
+ if (!patternType->hasTypeVariable()) {
9030
+ auto *loc = getConstraintLocator(locator);
9031
+ auto shapeClass = patternType->getReducedShape();
9032
+ patternType = patternType->mapTypeOutOfContext();
9033
+ auto *elementEnv = getPackElementEnvironment(loc, shapeClass);
9037
9034
9038
- // This constraint only exists to vend bindings.
9039
- auto *packEnv = DC->getGenericEnvironmentOfContext();
9035
+ // Without an opened element environment, we cannot derive the
9036
+ // element binding.
9037
+ if (!elementEnv) {
9038
+ if (!shouldAttemptFixes())
9039
+ return SolutionKind::Error;
9040
9040
9041
- // Map element archetypes to the pack context to check for equality.
9042
- if (elementType->hasElementArchetype()) {
9043
- auto mappedPack = packEnv->mapElementTypeIntoPackContext(elementType);
9044
- return (packType->isEqual(mappedPack) ?
9045
- SolutionKind::Solved : SolutionKind::Error);
9046
- }
9041
+ // `each` was applied to a concrete type.
9042
+ if (!shapeClass->is<PackArchetypeType>()) {
9043
+ if (recordFix(AllowInvalidPackElement::create(*this, patternType, loc)))
9044
+ return SolutionKind::Error;
9045
+ }
9047
9046
9048
- // Pack expansions can have concrete pattern types. In this case, the pack
9049
- // type and element type will be equal.
9050
- if (packType->isEqual(elementType)) {
9051
- return SolutionKind::Solved;
9052
- }
9047
+ // Only other posibility is that there is a shape mismatch between
9048
+ // elements of the pack expansion pattern which is detected separately.
9053
9049
9054
- if (shouldAttemptFixes()) {
9055
- auto *loc = getConstraintLocator(locator);
9056
- if (elementType->isPlaceholder() ||
9057
- !recordFix(AllowInvalidPackElement::create(*this, packType, loc)))
9050
+ recordAnyTypeVarAsPotentialHole(elementType);
9058
9051
return SolutionKind::Solved;
9052
+ }
9053
+
9054
+ auto expectedElementTy =
9055
+ elementEnv->mapPackTypeIntoElementContext(patternType);
9056
+ assert(!expectedElementTy->is<PackType>());
9057
+
9058
+ addConstraint(ConstraintKind::Equal, elementType, expectedElementTy,
9059
+ locator);
9060
+ return SolutionKind::Solved;
9059
9061
}
9060
9062
9061
- return SolutionKind::Error;
9063
+ // Otherwise we are inferred or checking pattern type.
9064
+
9065
+ auto *packEnv = DC->getGenericEnvironmentOfContext();
9066
+
9067
+ // Map element archetypes to the pack context to check for equality.
9068
+ if (elementType->hasElementArchetype())
9069
+ elementType = packEnv->mapElementTypeIntoPackContext(elementType);
9070
+
9071
+ addConstraint(ConstraintKind::Equal, elementType, patternType, locator);
9072
+ return SolutionKind::Solved;
9062
9073
}
9063
9074
9064
9075
static bool isForKeyPathSubscript(ConstraintSystem &cs,
0 commit comments