@@ -129,12 +129,20 @@ static bool isPackExpansionType(Type type) {
129129
130130bool constraints::isSingleUnlabeledPackExpansionTuple(Type type) {
131131 auto *tuple = type->getRValueType()->getAs<TupleType>();
132- // TODO: drop no name requirement
133132 return tuple && (tuple->getNumElements() == 1) &&
134133 isPackExpansionType(tuple->getElementType(0)) &&
135134 !tuple->getElement(0).hasName();
136135}
137136
137+ Type constraints::getPatternTypeOfSingleUnlabeledPackExpansionTuple(Type type) {
138+ if (!isSingleUnlabeledPackExpansionTuple(type)) {
139+ return {};
140+ }
141+ auto tuple = type->getRValueType()->castTo<TupleType>();
142+ auto *expansion = tuple->getElementType(0)->castTo<PackExpansionType>();
143+ return expansion->getPatternType();
144+ }
145+
138146static bool containsPackExpansionType(ArrayRef<AnyFunctionType::Param> params) {
139147 return llvm::any_of(params, [&](const auto ¶m) {
140148 return isPackExpansionType(param.getPlainType());
@@ -2283,6 +2291,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
22832291 case ConstraintKind::ShapeOf:
22842292 case ConstraintKind::ExplicitGenericArguments:
22852293 case ConstraintKind::SameShape:
2294+ case ConstraintKind::MaterializePackExpansion:
22862295 llvm_unreachable("Bad constraint kind in matchTupleTypes()");
22872296 }
22882297
@@ -2643,6 +2652,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
26432652 case ConstraintKind::ShapeOf:
26442653 case ConstraintKind::ExplicitGenericArguments:
26452654 case ConstraintKind::SameShape:
2655+ case ConstraintKind::MaterializePackExpansion:
26462656 return true;
26472657 }
26482658
@@ -3161,6 +3171,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
31613171 case ConstraintKind::ShapeOf:
31623172 case ConstraintKind::ExplicitGenericArguments:
31633173 case ConstraintKind::SameShape:
3174+ case ConstraintKind::MaterializePackExpansion:
31643175 llvm_unreachable("Not a relational constraint");
31653176 }
31663177
@@ -6827,6 +6838,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
68276838 case ConstraintKind::ShapeOf:
68286839 case ConstraintKind::ExplicitGenericArguments:
68296840 case ConstraintKind::SameShape:
6841+ case ConstraintKind::MaterializePackExpansion:
68306842 llvm_unreachable("Not a relational constraint");
68316843 }
68326844 }
@@ -9149,13 +9161,13 @@ ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
91499161 }
91509162
91519163 if (isSingleUnlabeledPackExpansionTuple(patternType)) {
9152- auto *elementVar =
9153- createTypeVariable(getConstraintLocator(locator), /*options=*/0 );
9154- addValueMemberConstraint(
9155- patternType, DeclNameRef(getASTContext().Id_element), elementVar, DC ,
9156- FunctionRefKind::Unapplied , {},
9157- getConstraintLocator(locator, {ConstraintLocator::Member}) );
9158- patternType = elementVar ;
9164+ auto *packVar =
9165+ createTypeVariable(getConstraintLocator(locator), TVO_CanBindToPack );
9166+ addConstraint(ConstraintKind::MaterializePackExpansion, patternType,
9167+ packVar ,
9168+ getConstraintLocator(locator , {ConstraintLocator::Member}));
9169+ addConstraint(ConstraintKind::PackElementOf, elementType, packVar, locator );
9170+ return SolutionKind::Solved ;
91599171 }
91609172
91619173 // Let's try to resolve element type based on the pattern type.
@@ -9174,11 +9186,19 @@ ConstraintSystem::simplifyPackElementOfConstraint(Type first, Type second,
91749186 if (!shapeClass->is<PackArchetypeType>()) {
91759187 if (recordFix(AllowInvalidPackElement::create(*this, patternType, loc)))
91769188 return SolutionKind::Error;
9189+ } else {
9190+ auto envShape = PackExpansionEnvironments.find(loc);
9191+ if (envShape == PackExpansionEnvironments.end()) {
9192+ return SolutionKind::Error;
9193+ }
9194+ auto *fix = SkipSameShapeRequirement::create(
9195+ *this, envShape->second.second, shapeClass,
9196+ getConstraintLocator(loc, ConstraintLocator::PackShape));
9197+ if (recordFix(fix)) {
9198+ return SolutionKind::Error;
9199+ }
91779200 }
91789201
9179- // Only other posibility is that there is a shape mismatch between
9180- // elements of the pack expansion pattern which is detected separately.
9181-
91829202 recordAnyTypeVarAsPotentialHole(elementType);
91839203 return SolutionKind::Solved;
91849204 }
@@ -13551,6 +13571,37 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifySameShapeConstraint(
1355113571 return SolutionKind::Error;
1355213572}
1355313573
13574+ ConstraintSystem::SolutionKind
13575+ ConstraintSystem::simplifyMaterializePackExpansionConstraint(
13576+ Type type1, Type type2, TypeMatchOptions flags,
13577+ ConstraintLocatorBuilder locator) {
13578+ auto formUnsolved = [&]() {
13579+ // If we're supposed to generate constraints, do so.
13580+ if (flags.contains(TMF_GenerateConstraints)) {
13581+ auto *explictGenericArgs =
13582+ Constraint::create(*this, ConstraintKind::MaterializePackExpansion,
13583+ type1, type2, getConstraintLocator(locator));
13584+
13585+ addUnsolvedConstraint(explictGenericArgs);
13586+ return SolutionKind::Solved;
13587+ }
13588+
13589+ return SolutionKind::Unsolved;
13590+ };
13591+
13592+ type1 = simplifyType(type1);
13593+ if (type1->hasTypeVariable()) {
13594+ return formUnsolved();
13595+ }
13596+ if (auto patternType =
13597+ getPatternTypeOfSingleUnlabeledPackExpansionTuple(type1)) {
13598+ addConstraint(ConstraintKind::Equal, patternType, type2, locator);
13599+ return SolutionKind::Solved;
13600+ }
13601+
13602+ return SolutionKind::Error;
13603+ }
13604+
1355413605ConstraintSystem::SolutionKind
1355513606ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1355613607 Type type1, Type type2, TypeMatchOptions flags,
@@ -15178,6 +15229,10 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
1517815229 return simplifyExplicitGenericArgumentsConstraint(
1517915230 first, second, subflags, locator);
1518015231
15232+ case ConstraintKind::MaterializePackExpansion:
15233+ return simplifyMaterializePackExpansionConstraint(first, second, subflags,
15234+ locator);
15235+
1518115236 case ConstraintKind::ValueMember:
1518215237 case ConstraintKind::UnresolvedValueMember:
1518315238 case ConstraintKind::ValueWitness:
@@ -15757,6 +15812,11 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1575715812 return simplifyExplicitGenericArgumentsConstraint(
1575815813 constraint.getFirstType(), constraint.getSecondType(),
1575915814 /*flags*/ llvm::None, constraint.getLocator());
15815+
15816+ case ConstraintKind::MaterializePackExpansion:
15817+ return simplifyMaterializePackExpansionConstraint(
15818+ constraint.getFirstType(), constraint.getSecondType(),
15819+ /*flags*/ llvm::None, constraint.getLocator());
1576015820 }
1576115821
1576215822 llvm_unreachable("Unhandled ConstraintKind in switch.");
0 commit comments