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