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