@@ -2273,6 +2273,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
2273
2273
case ConstraintKind::PackElementOf:
2274
2274
case ConstraintKind::ShapeOf:
2275
2275
case ConstraintKind::ExplicitGenericArguments:
2276
+ case ConstraintKind::SameShape:
2276
2277
llvm_unreachable("Bad constraint kind in matchTupleTypes()");
2277
2278
}
2278
2279
@@ -2486,13 +2487,9 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
2486
2487
ConstraintKind kind, TypeMatchOptions flags,
2487
2488
ConstraintLocatorBuilder locator) {
2488
2489
// The count types of two pack expansion types must have the same shape.
2489
- auto *shapeLoc = getConstraintLocator(
2490
- locator.withPathElement(ConstraintLocator::PackShape));
2491
- auto *shapeTypeVar = createTypeVariable(shapeLoc, TVO_CanBindToPack);
2492
- addConstraint(ConstraintKind::ShapeOf,
2493
- expansion1->getCountType(), shapeTypeVar, shapeLoc);
2494
- addConstraint(ConstraintKind::ShapeOf,
2495
- expansion2->getCountType(), shapeTypeVar, shapeLoc);
2490
+ addConstraint(ConstraintKind::SameShape, expansion1->getCountType(),
2491
+ expansion2->getCountType(),
2492
+ locator.withPathElement(ConstraintLocator::PackShape));
2496
2493
2497
2494
auto pattern1 = expansion1->getPatternType();
2498
2495
auto pattern2 = expansion2->getPatternType();
@@ -2652,6 +2649,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
2652
2649
case ConstraintKind::PackElementOf:
2653
2650
case ConstraintKind::ShapeOf:
2654
2651
case ConstraintKind::ExplicitGenericArguments:
2652
+ case ConstraintKind::SameShape:
2655
2653
return true;
2656
2654
}
2657
2655
@@ -3159,6 +3157,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
3159
3157
case ConstraintKind::PackElementOf:
3160
3158
case ConstraintKind::ShapeOf:
3161
3159
case ConstraintKind::ExplicitGenericArguments:
3160
+ case ConstraintKind::SameShape:
3162
3161
llvm_unreachable("Not a relational constraint");
3163
3162
}
3164
3163
@@ -6819,6 +6818,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
6819
6818
case ConstraintKind::PackElementOf:
6820
6819
case ConstraintKind::ShapeOf:
6821
6820
case ConstraintKind::ExplicitGenericArguments:
6821
+ case ConstraintKind::SameShape:
6822
6822
llvm_unreachable("Not a relational constraint");
6823
6823
}
6824
6824
}
@@ -13195,6 +13195,18 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
13195
13195
return SolutionKind::Solved;
13196
13196
}
13197
13197
13198
+ static bool hasUnresolvedPackVars(Type type) {
13199
+ // We can't compute a reduced shape if the input type still
13200
+ // contains type variables that might bind to pack archetypes
13201
+ // or pack expansions.
13202
+ SmallPtrSet<TypeVariableType *, 2> typeVars;
13203
+ type->getTypeVariables(typeVars);
13204
+ return llvm::any_of(typeVars, [](const TypeVariableType *typeVar) {
13205
+ return typeVar->getImpl().canBindToPack() ||
13206
+ typeVar->getImpl().isPackExpansion();
13207
+ });
13208
+ }
13209
+
13198
13210
ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
13199
13211
Type type1, Type type2, TypeMatchOptions flags,
13200
13212
ConstraintLocatorBuilder locator) {
@@ -13236,6 +13248,51 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyShapeOfConstraint(
13236
13248
return SolutionKind::Solved;
13237
13249
}
13238
13250
13251
+ ConstraintSystem::SolutionKind ConstraintSystem::simplifySameShapeConstraint(
13252
+ Type type1, Type type2, TypeMatchOptions flags,
13253
+ ConstraintLocatorBuilder locator) {
13254
+ type1 = simplifyType(type1);
13255
+ type2 = simplifyType(type2);
13256
+
13257
+ auto formUnsolved = [&]() {
13258
+ // If we're supposed to generate constraints, do so.
13259
+ if (flags.contains(TMF_GenerateConstraints)) {
13260
+ auto *sameShape =
13261
+ Constraint::create(*this, ConstraintKind::SameShape, type1, type2,
13262
+ getConstraintLocator(locator));
13263
+
13264
+ addUnsolvedConstraint(sameShape);
13265
+ return SolutionKind::Solved;
13266
+ }
13267
+
13268
+ return SolutionKind::Unsolved;
13269
+ };
13270
+
13271
+ if (hasUnresolvedPackVars(type1) || hasUnresolvedPackVars(type2))
13272
+ return formUnsolved();
13273
+
13274
+ auto shape1 = type1->getReducedShape();
13275
+ auto shape2 = type2->getReducedShape();
13276
+
13277
+ if (shape1->isEqual(shape2))
13278
+ return SolutionKind::Solved;
13279
+
13280
+ if (shouldAttemptFixes()) {
13281
+ if (type1->hasPlaceholder() || type2->hasPlaceholder())
13282
+ return SolutionKind::Solved;
13283
+
13284
+ unsigned impact = 1;
13285
+ if (locator.endsWith<LocatorPathElt::AnyRequirement>())
13286
+ impact = assessRequirementFailureImpact(*this, shape1, locator);
13287
+
13288
+ auto *fix = SkipSameShapeRequirement::create(*this, type1, type2,
13289
+ getConstraintLocator(locator));
13290
+ return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved;
13291
+ }
13292
+
13293
+ return SolutionKind::Error;
13294
+ }
13295
+
13239
13296
ConstraintSystem::SolutionKind
13240
13297
ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
13241
13298
Type type1, Type type2, TypeMatchOptions flags,
@@ -14707,6 +14764,9 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
14707
14764
case ConstraintKind::ShapeOf:
14708
14765
return simplifyShapeOfConstraint(first, second, subflags, locator);
14709
14766
14767
+ case ConstraintKind::SameShape:
14768
+ return simplifySameShapeConstraint(first, second, subflags, locator);
14769
+
14710
14770
case ConstraintKind::ExplicitGenericArguments:
14711
14771
return simplifyExplicitGenericArgumentsConstraint(
14712
14772
first, second, subflags, locator);
@@ -14878,13 +14938,7 @@ void ConstraintSystem::addConstraint(Requirement req,
14878
14938
auto type1 = req.getFirstType();
14879
14939
auto type2 = req.getSecondType();
14880
14940
14881
- auto *shapeLoc = getConstraintLocator(
14882
- locator.withPathElement(ConstraintLocator::PackShape));
14883
- auto typeVar = createTypeVariable(shapeLoc,
14884
- TVO_CanBindToPack);
14885
-
14886
- addConstraint(ConstraintKind::ShapeOf, type1, typeVar, locator);
14887
- addConstraint(ConstraintKind::ShapeOf, type2, typeVar, locator);
14941
+ addConstraint(ConstraintKind::SameShape, type1, type2, locator);
14888
14942
return;
14889
14943
}
14890
14944
@@ -15308,6 +15362,11 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
15308
15362
constraint.getFirstType(), constraint.getSecondType(), /*flags*/ None,
15309
15363
constraint.getLocator());
15310
15364
15365
+ case ConstraintKind::SameShape:
15366
+ return simplifySameShapeConstraint(constraint.getFirstType(),
15367
+ constraint.getSecondType(),
15368
+ /*flags*/ None, constraint.getLocator());
15369
+
15311
15370
case ConstraintKind::ExplicitGenericArguments:
15312
15371
return simplifyExplicitGenericArgumentsConstraint(
15313
15372
constraint.getFirstType(), constraint.getSecondType(),
0 commit comments