Skip to content

Commit cc9f5ce

Browse files
committed
[CSGen] Detect and diagnose invalid pack expansion expressions
Detect that pack expansion expression doesn't have any pack references during constraint generation. Resolves: rdar://107835215
1 parent dc546f3 commit cc9f5ce

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,8 @@ DestructureTupleToMatchPackExpansionParameter::create(
27592759

27602760
bool AllowValueExpansionWithoutPackReferences::diagnose(
27612761
const Solution &solution, bool asNote) const {
2762-
return false;
2762+
ValuePackExpansionWithoutPackReferences failure(solution, getLocator());
2763+
return failure.diagnose(asNote);
27632764
}
27642765

27652766
AllowValueExpansionWithoutPackReferences *

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,6 +3081,12 @@ namespace {
30813081
// pack expansion expression through the shape type variable.
30823082
SmallVector<ASTNode, 2> expandedPacks;
30833083
expr->getExpandedPacks(expandedPacks);
3084+
3085+
if (expandedPacks.empty()) {
3086+
(void)CS.recordFix(AllowValueExpansionWithoutPackReferences::create(
3087+
CS, CS.getConstraintLocator(expr)));
3088+
}
3089+
30843090
for (auto pack : expandedPacks) {
30853091
Type packType;
30863092
if (auto *elementExpr = getAsExpr<PackElementExpr>(pack)) {

test/Constraints/pack-expansion-expressions.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,26 @@ do {
447447
// expected-error@-1 {{pack reference 'each T' can only appear in pack expansion}}
448448
}
449449
}
450+
451+
// rdar://107835215 - failed to produce a diagnostic for invalid pack expansion expression
452+
do {
453+
func test1(x: Int) {
454+
repeat x
455+
// expected-error@-1:5 {{value pack expansion must contain at least one pack reference}}
456+
}
457+
458+
func test2<T: Numeric>(_ x: T) {
459+
repeat print(x * 2)
460+
// expected-error@-1:5 {{value pack expansion must contain at least one pack reference}}
461+
}
462+
463+
struct S<T> {
464+
init(_: T) {}
465+
}
466+
467+
func test<each T>(x: repeat each T, y: Int) {
468+
func f<each A, each B>(_: repeat each A, y: repeat each B) {}
469+
f(repeat each x, y: repeat [S(y)])
470+
// expected-error@-1:25 {{value pack expansion must contain at least one pack reference}}
471+
}
472+
}

0 commit comments

Comments
 (0)