Skip to content

Commit 5d4162e

Browse files
committed
Diagnose that where clause is not supported
1 parent 4702edd commit 5d4162e

File tree

8 files changed

+78
-0
lines changed

8 files changed

+78
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7715,5 +7715,12 @@ ERROR(referencebindings_binding_must_be_to_lvalue,none,
77157715
ERROR(result_depends_on_no_result,none,
77167716
"Incorrect use of %0 with no result", (StringRef))
77177717

7718+
//------------------------------------------------------------------------------
7719+
// MARK: Pack Iteration Diagnostics
7720+
//------------------------------------------------------------------------------
7721+
7722+
ERROR(pack_iteration_where_clause_not_supported, none,
7723+
"'where' clause in pack iteration is not supported", ())
7724+
77187725
#define UNDEFINE_DIAGNOSTIC_MACROS
77197726
#include "DefineDiagnosticMacros.h"

include/swift/Sema/CSFix.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ enum class FixKind : uint8_t {
440440
/// Allow pack expansion expressions in a context that does not support them.
441441
AllowInvalidPackExpansion,
442442

443+
/// Ignore `where` clause in a for-in loop with a pack expansion expression.
444+
IgnoreWhereClauseInPackIteration,
445+
443446
/// Allow a pack expansion parameter of N elements to be matched
444447
/// with a single tuple literal argument of the same arity.
445448
DestructureTupleToMatchPackExpansionParameter,
@@ -2223,6 +2226,26 @@ class AllowInvalidPackExpansion final : public ConstraintFix {
22232226
}
22242227
};
22252228

2229+
class IgnoreWhereClauseInPackIteration final : public ConstraintFix {
2230+
IgnoreWhereClauseInPackIteration(ConstraintSystem &cs,
2231+
ConstraintLocator *locator)
2232+
: ConstraintFix(cs, FixKind::IgnoreWhereClauseInPackIteration, locator) {}
2233+
2234+
public:
2235+
std::string getName() const override {
2236+
return "ignore where clause in pack iteration";
2237+
}
2238+
2239+
bool diagnose(const Solution &solution, bool asNote = false) const override;
2240+
2241+
static IgnoreWhereClauseInPackIteration *create(ConstraintSystem &cs,
2242+
ConstraintLocator *locator);
2243+
2244+
static bool classof(const ConstraintFix *fix) {
2245+
return fix->getKind() == FixKind::IgnoreWhereClauseInPackIteration;
2246+
}
2247+
};
2248+
22262249
class CollectionElementContextualMismatch final
22272250
: public ContextualMismatch,
22282251
private llvm::TrailingObjects<CollectionElementContextualMismatch,

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6408,6 +6408,11 @@ bool InvalidPackExpansion::diagnoseAsError() {
64086408
return true;
64096409
}
64106410

6411+
bool InvalidWhereClauseInPackIteration::diagnoseAsError() {
6412+
emitDiagnostic(diag::pack_iteration_where_clause_not_supported);
6413+
return true;
6414+
}
6415+
64116416
bool CollectionElementContextualFailure::diagnoseAsError() {
64126417
auto anchor = getRawAnchor();
64136418
auto *locator = getLocator();

lib/Sema/CSDiagnostics.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,16 @@ class InvalidPackExpansion final : public FailureDiagnostic {
19031903
bool diagnoseAsError() override;
19041904
};
19051905

1906+
/// Diagnose that a `where` clause is not supported with pack iteration.
1907+
class InvalidWhereClauseInPackIteration final : public FailureDiagnostic {
1908+
public:
1909+
InvalidWhereClauseInPackIteration(const Solution &solution,
1910+
ConstraintLocator *locator)
1911+
: FailureDiagnostic(solution, locator) {}
1912+
1913+
bool diagnoseAsError() override;
1914+
};
1915+
19061916
/// Diagnose a contextual mismatch between expected collection element type
19071917
/// and the one provided (e.g. source of the assignment or argument to a call)
19081918
/// e.g.:

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,18 @@ AllowInvalidPackExpansion::create(ConstraintSystem &cs,
14381438
return new (cs.getAllocator()) AllowInvalidPackExpansion(cs, locator);
14391439
}
14401440

1441+
bool IgnoreWhereClauseInPackIteration::diagnose(const Solution &solution,
1442+
bool asNote) const {
1443+
InvalidWhereClauseInPackIteration failure(solution, getLocator());
1444+
return failure.diagnose(asNote);
1445+
}
1446+
1447+
IgnoreWhereClauseInPackIteration *
1448+
IgnoreWhereClauseInPackIteration::create(ConstraintSystem &cs,
1449+
ConstraintLocator *locator) {
1450+
return new (cs.getAllocator()) IgnoreWhereClauseInPackIteration(cs, locator);
1451+
}
1452+
14411453
bool CollectionElementContextualMismatch::diagnose(const Solution &solution,
14421454
bool asNote) const {
14431455
CollectionElementContextualFailure failure(

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,6 +4675,11 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
46754675
if (!patternType)
46764676
return llvm::None;
46774677

4678+
if (auto whereClause = stmt->getWhere()) {
4679+
cs.recordFix(IgnoreWhereClauseInPackIteration::create(
4680+
cs, cs.getConstraintLocator(whereClause)));
4681+
}
4682+
46784683
auto packIterationInfo =
46794684
generateForEachStmtConstraints(cs, dc, expansion, patternType);
46804685
if (!packIterationInfo) {

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14895,6 +14895,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1489514895
case FixKind::AllowInvalidPackElement:
1489614896
case FixKind::AllowInvalidPackReference:
1489714897
case FixKind::AllowInvalidPackExpansion:
14898+
case FixKind::IgnoreWhereClauseInPackIteration:
1489814899
case FixKind::MacroMissingPound:
1489914900
case FixKind::AllowGlobalActorMismatch:
1490014901
case FixKind::AllowAssociatedValueMismatch:

test/stmt/foreach.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,18 @@ do {
316316
}
317317
}
318318
}
319+
320+
// SE-0408
321+
do {
322+
func variadic<each T: Collection>(ts: repeat each T) {
323+
for t in repeat each ts where !ts.isEmpty {}
324+
// expected-error@-1 {{'where' clause in pack iteration is not supported}}
325+
326+
func test(_: () -> Void) {}
327+
328+
test {
329+
for t in repeat each ts where !ts.isEmpty {}
330+
// expected-error@-1 {{'where' clause in pack iteration is not supported}}
331+
}
332+
}
333+
}

0 commit comments

Comments
 (0)