Skip to content

Commit bbdcada

Browse files
committed
[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. (cherry picked from commit fb54682)
1 parent 4348b59 commit bbdcada

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,14 +2359,26 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
23592359
PackExpansionType *expansion2,
23602360
ConstraintKind kind, TypeMatchOptions flags,
23612361
ConstraintLocatorBuilder locator) {
2362+
auto shapeLocator = locator.withPathElement(ConstraintLocator::PackShape);
23622363
// The count types of two pack expansion types must have the same shape.
23632364
addConstraint(ConstraintKind::SameShape, expansion1->getCountType(),
23642365
expansion2->getCountType(),
2365-
locator.withPathElement(ConstraintLocator::PackShape));
2366+
shapeLocator);
23662367

23672368
auto pattern1 = expansion1->getPatternType();
23682369
auto pattern2 = expansion2->getPatternType();
23692370

2371+
if (shouldAttemptFixes()) {
2372+
// If pack expansion types have different shapes, let's not attempt
2373+
// to match their pattern types to avoid producing any extra errors
2374+
// caused by shape differences.
2375+
if (hasFixFor(getConstraintLocator(shapeLocator))) {
2376+
recordAnyTypeVarAsPotentialHole(pattern1);
2377+
recordAnyTypeVarAsPotentialHole(pattern2);
2378+
return getTypeMatchSuccess();
2379+
}
2380+
}
2381+
23702382
// If both sides are expanded or neither side is, just match them
23712383
// directly.
23722384
if (pattern1->is<PackType>() == pattern2->is<PackType>()) {

test/Constraints/pack-expansion-expressions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,6 @@ do {
647647
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
648648
S<(Int, Int), repeat each U>().method((3, 4))
649649
// expected-error@-1 {{pack expansion requires that '(Int, Int), repeat each U' and '(Int, Int)' have the same shape}}
650-
// expected-error@-2 {{pack expansion requires that '' and 'each U' have the same shape}}
651650

652651
// FIXME: The count of '(Int, Int), repeat each U' is not statically known, but error suggests that it is 2.
653652
S<(Int, Int), repeat each U>().property((3, 4))

test/Constraints/variadic_generic_functions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func call() {
3737
func multipleSequences<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) {
3838
return (repeat each ys)
3939
// expected-error@-1 {{pack expansion requires that 'each U' and 'each T' have the same shape}}
40-
// expected-error@-2 {{cannot convert return expression of type '(repeat each U)' to return type '(repeat each T)'}}
4140
}
4241

4342
func multipleSequencesWithSameShape<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) where (repeat (each T, each U)): Any {

validation-test/Sema/type_checker_crashers_fixed/rdar112090069.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ func test<each Before,
99
return { (before: repeat each Before, after: repeat each After) in // expected-error {{no parameters may follow a variadic parameter in a closure}}
1010
return fn(repeat each before, repeat each at, repeat each after)
1111
// expected-error@-1 {{pack expansion requires that 'each At' and 'each Before' have the same shape}}
12-
// expected-error@-2 {{cannot convert value of type 'each At' to expected argument type 'each Before'}}
13-
// expected-error@-3 {{pack expansion requires that 'each After' and 'each Before' have the same shape}}
14-
// expected-error@-4 {{cannot convert value of type 'each After' to expected argument type 'each Before'}}
12+
// expected-error@-2 {{pack expansion requires that 'each After' and 'each Before' have the same shape}}
1513
}
1614
}

0 commit comments

Comments
 (0)