[CSSimplify] Don't match pack expansion patterns if shapes are not the same

This helps to avoid spurious failures pointing to involved pattern types
because they won't match exactly if shape types are not the same.
This commit is contained in:
Pavel Yaskevich
2024-12-12 20:41:16 -08:00
parent 0772e4cbc7
commit fb54682753
4 changed files with 14 additions and 6 deletions

View File

@@ -2388,14 +2388,26 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
PackExpansionType *expansion2,
ConstraintKind kind, TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {
auto shapeLocator = locator.withPathElement(ConstraintLocator::PackShape);
// The count types of two pack expansion types must have the same shape.
addConstraint(ConstraintKind::SameShape, expansion1->getCountType(),
expansion2->getCountType(),
locator.withPathElement(ConstraintLocator::PackShape));
shapeLocator);
auto pattern1 = expansion1->getPatternType();
auto pattern2 = expansion2->getPatternType();
if (shouldAttemptFixes()) {
// If pack expansion types have different shapes, let's not attempt
// to match their pattern types to avoid producing any extra errors
// caused by shape differences.
if (hasFixFor(getConstraintLocator(shapeLocator))) {
recordAnyTypeVarAsPotentialHole(pattern1);
recordAnyTypeVarAsPotentialHole(pattern2);
return getTypeMatchSuccess();
}
}
// If both sides are expanded or neither side is, just match them
// directly.
if (pattern1->is<PackType>() == pattern2->is<PackType>()) {

View File

@@ -647,7 +647,6 @@ do {
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
S<(Int, Int), repeat each U>().method((3, 4))
// expected-error@-1 {{pack expansion requires that '(Int, Int), repeat each U' and '(Int, Int)' have the same shape}}
// expected-error@-2 {{pack expansion requires that '' and 'each U' have the same shape}}
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
S<(Int, Int), repeat each U>().property((3, 4))

View File

@@ -37,7 +37,6 @@ func call() {
func multipleSequences<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) {
return (repeat each ys)
// expected-error@-1 {{pack expansion requires that 'each U' and 'each T' have the same shape}}
// expected-error@-2 {{cannot convert return expression of type '(repeat each U)' to return type '(repeat each T)'}}
}
func multipleSequencesWithSameShape<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) where (repeat (each T, each U)): Any {

View File

@@ -9,8 +9,6 @@ func test<each Before,
return { (before: repeat each Before, after: repeat each After) in // expected-error {{no parameters may follow a variadic parameter in a closure}}
return fn(repeat each before, repeat each at, repeat each after)
// expected-error@-1 {{pack expansion requires that 'each At' and 'each Before' have the same shape}}
// expected-error@-2 {{cannot convert value of type 'each At' to expected argument type 'each Before'}}
// expected-error@-3 {{pack expansion requires that 'each After' and 'each Before' have the same shape}}
// expected-error@-4 {{cannot convert value of type 'each After' to expected argument type 'each Before'}}
// expected-error@-2 {{pack expansion requires that 'each After' and 'each Before' have the same shape}}
}
}