@@ -2276,6 +2276,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
2276
2276
case ConstraintKind::PackElementOf:
2277
2277
case ConstraintKind::ShapeOf:
2278
2278
case ConstraintKind::ExplicitGenericArguments:
2279
+ case ConstraintKind::SameShape:
2279
2280
llvm_unreachable("Bad constraint kind in matchTupleTypes()");
2280
2281
}
2281
2282
@@ -2489,13 +2490,9 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
2489
2490
ConstraintKind kind, TypeMatchOptions flags,
2490
2491
ConstraintLocatorBuilder locator) {
2491
2492
// The count types of two pack expansion types must have the same shape.
2492
- auto *shapeLoc = getConstraintLocator(
2493
- locator.withPathElement(ConstraintLocator::PackShape));
2494
- auto *shapeTypeVar = createTypeVariable(shapeLoc, TVO_CanBindToPack);
2495
- addConstraint(ConstraintKind::ShapeOf,
2496
- expansion1->getCountType(), shapeTypeVar, shapeLoc);
2497
- addConstraint(ConstraintKind::ShapeOf,
2498
- expansion2->getCountType(), shapeTypeVar, shapeLoc);
2493
+ addConstraint(ConstraintKind::SameShape, expansion1->getCountType(),
2494
+ expansion2->getCountType(),
2495
+ locator.withPathElement(ConstraintLocator::PackShape));
2499
2496
2500
2497
auto pattern1 = expansion1->getPatternType();
2501
2498
auto pattern2 = expansion2->getPatternType();
@@ -2655,6 +2652,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
2655
2652
case ConstraintKind::PackElementOf:
2656
2653
case ConstraintKind::ShapeOf:
2657
2654
case ConstraintKind::ExplicitGenericArguments:
2655
+ case ConstraintKind::SameShape:
2658
2656
return true;
2659
2657
}
2660
2658
@@ -3162,6 +3160,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
3162
3160
case ConstraintKind::PackElementOf:
3163
3161
case ConstraintKind::ShapeOf:
3164
3162
case ConstraintKind::ExplicitGenericArguments:
3163
+ case ConstraintKind::SameShape:
3165
3164
llvm_unreachable("Not a relational constraint");
3166
3165
}
3167
3166
@@ -6815,6 +6814,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
6815
6814
case ConstraintKind::PackElementOf:
6816
6815
case ConstraintKind::ShapeOf:
6817
6816
case ConstraintKind::ExplicitGenericArguments:
6817
+ case ConstraintKind::SameShape:
6818
6818
llvm_unreachable("Not a relational constraint");
6819
6819
}
6820
6820
}
@@ -13190,6 +13190,18 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
13190
13190
return SolutionKind::Solved;
13191
13191
}
13192
13192
13193
+ static bool hasUnresolvedPackVars(Type type) {
13194
+ // We can't compute a reduced shape if the input type still
13195
+ // contains type variables that might bind to pack archetypes
13196
+ // or pack expansions.
13197
+ SmallPtrSet<TypeVariableType *, 2> typeVars;
13198
+ type->getTypeVariables(typeVars);
13199
+ return llvm::any_of(typeVars, [](const TypeVariableType *typeVar) {
13200
+ return typeVar->getImpl().canBindToPack() ||
13201
+ typeVar->getImpl().isPackExpansion();
13202
+ });
13203
+ }
13204
+
13193
13205
ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
13194
13206
Type type1, Type type2, TypeMatchOptions flags,
13195
13207
ConstraintLocatorBuilder locator) {
@@ -13231,6 +13243,51 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
13231
13243
return SolutionKind::Solved;
13232
13244
}
13233
13245
13246
+ ConstraintSystem::SolutionKind ConstraintSystem::simplifySameShapeConstraint(
13247
+ Type type1, Type type2, TypeMatchOptions flags,
13248
+ ConstraintLocatorBuilder locator) {
13249
+ type1 = simplifyType(type1);
13250
+ type2 = simplifyType(type2);
13251
+
13252
+ auto formUnsolved = [&]() {
13253
+ // If we're supposed to generate constraints, do so.
13254
+ if (flags.contains(TMF_GenerateConstraints)) {
13255
+ auto *sameShape =
13256
+ Constraint::create(*this, ConstraintKind::SameShape, type1, type2,
13257
+ getConstraintLocator(locator));
13258
+
13259
+ addUnsolvedConstraint(sameShape);
13260
+ return SolutionKind::Solved;
13261
+ }
13262
+
13263
+ return SolutionKind::Unsolved;
13264
+ };
13265
+
13266
+ if (hasUnresolvedPackVars(type1) || hasUnresolvedPackVars(type2))
13267
+ return formUnsolved();
13268
+
13269
+ auto shape1 = type1->getReducedShape();
13270
+ auto shape2 = type2->getReducedShape();
13271
+
13272
+ if (shape1->isEqual(shape2))
13273
+ return SolutionKind::Solved;
13274
+
13275
+ if (shouldAttemptFixes()) {
13276
+ if (type1->hasPlaceholder() || type2->hasPlaceholder())
13277
+ return SolutionKind::Solved;
13278
+
13279
+ unsigned impact = 1;
13280
+ if (locator.endsWith<LocatorPathElt::AnyRequirement>())
13281
+ impact = assessRequirementFailureImpact(*this, shape1, locator);
13282
+
13283
+ auto *fix = SkipSameShapeRequirement::create(*this, type1, type2,
13284
+ getConstraintLocator(locator));
13285
+ return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved;
13286
+ }
13287
+
13288
+ return SolutionKind::Error;
13289
+ }
13290
+
13234
13291
ConstraintSystem::SolutionKind
13235
13292
ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
13236
13293
Type type1, Type type2, TypeMatchOptions flags,
@@ -14718,6 +14775,9 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
14718
14775
case ConstraintKind::ShapeOf:
14719
14776
return simplifyShapeOfConstraint(first, second, subflags, locator);
14720
14777
14778
+ case ConstraintKind::SameShape:
14779
+ return simplifySameShapeConstraint(first, second, subflags, locator);
14780
+
14721
14781
case ConstraintKind::ExplicitGenericArguments:
14722
14782
return simplifyExplicitGenericArgumentsConstraint(
14723
14783
first, second, subflags, locator);
@@ -14889,13 +14949,7 @@ void ConstraintSystem::addConstraint(Requirement req,
14889
14949
auto type1 = req.getFirstType();
14890
14950
auto type2 = req.getSecondType();
14891
14951
14892
- auto *shapeLoc = getConstraintLocator(
14893
- locator.withPathElement(ConstraintLocator::PackShape));
14894
- auto typeVar = createTypeVariable(shapeLoc,
14895
- TVO_CanBindToPack);
14896
-
14897
- addConstraint(ConstraintKind::ShapeOf, type1, typeVar, locator);
14898
- addConstraint(ConstraintKind::ShapeOf, type2, typeVar, locator);
14952
+ addConstraint(ConstraintKind::SameShape, type1, type2, locator);
14899
14953
return;
14900
14954
}
14901
14955
@@ -15319,6 +15373,11 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
15319
15373
constraint.getFirstType(), constraint.getSecondType(), /*flags*/ None,
15320
15374
constraint.getLocator());
15321
15375
15376
+ case ConstraintKind::SameShape:
15377
+ return simplifySameShapeConstraint(constraint.getFirstType(),
15378
+ constraint.getSecondType(),
15379
+ /*flags*/ None, constraint.getLocator());
15380
+
15322
15381
case ConstraintKind::ExplicitGenericArguments:
15323
15382
return simplifyExplicitGenericArgumentsConstraint(
15324
15383
constraint.getFirstType(), constraint.getSecondType(),
0 commit comments