Skip to content

Commit 8b42107

Browse files
committed
[CS] Rename SyntacticElementTarget::forEachStmt -> forEachPreamble
This more closely matches what we're actually type-checking, since we do not currently include the body.
1 parent e98186b commit 8b42107

9 files changed

+54
-50
lines changed

include/swift/Sema/SyntacticElementTarget.h

+33-30
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ class SyntacticElementTarget {
7171
caseLabelItem,
7272
patternBinding,
7373
uninitializedVar,
74-
forEachStmt,
74+
75+
/// The preamble of a for-in statement, including everything except the
76+
/// body.
77+
forEachPreamble,
7578
} kind;
7679

7780
private:
@@ -245,7 +248,7 @@ class SyntacticElementTarget {
245248
SyntacticElementTarget(ForEachStmt *stmt, DeclContext *dc,
246249
bool ignoreWhereClause,
247250
GenericEnvironment *packElementEnv)
248-
: kind(Kind::forEachStmt) {
251+
: kind(Kind::forEachPreamble) {
249252
forEachStmt.stmt = stmt;
250253
forEachStmt.dc = dc;
251254
forEachStmt.ignoreWhereClause = ignoreWhereClause;
@@ -268,11 +271,11 @@ class SyntacticElementTarget {
268271
static SyntacticElementTarget
269272
forReturn(ReturnStmt *returnStmt, Type contextTy, DeclContext *dc);
270273

271-
/// Form a target for a for-in loop.
274+
/// Form a target for the preamble of a for-in loop, excluding its body.
272275
static SyntacticElementTarget
273-
forForEachStmt(ForEachStmt *stmt, DeclContext *dc,
274-
bool ignoreWhereClause = false,
275-
GenericEnvironment *packElementEnv = nullptr);
276+
forForEachPreamble(ForEachStmt *stmt, DeclContext *dc,
277+
bool ignoreWhereClause = false,
278+
GenericEnvironment *packElementEnv = nullptr);
276279

277280
/// Form a target for a property with an attached property wrapper that is
278281
/// initialized out-of-line.
@@ -311,7 +314,7 @@ class SyntacticElementTarget {
311314
return ref.getAbstractClosureExpr();
312315
}
313316

314-
case Kind::forEachStmt:
317+
case Kind::forEachPreamble:
315318
return getAsForEachStmt();
316319

317320
case Kind::stmtCondition:
@@ -339,7 +342,7 @@ class SyntacticElementTarget {
339342
case Kind::caseLabelItem:
340343
case Kind::patternBinding:
341344
case Kind::uninitializedVar:
342-
case Kind::forEachStmt:
345+
case Kind::forEachPreamble:
343346
return nullptr;
344347
}
345348
llvm_unreachable("invalid expression type");
@@ -372,7 +375,7 @@ class SyntacticElementTarget {
372375
return uninitializedVar.binding->getInitContext(uninitializedVar.index);
373376
}
374377

375-
case Kind::forEachStmt:
378+
case Kind::forEachPreamble:
376379
return forEachStmt.dc;
377380
}
378381
llvm_unreachable("invalid decl context type");
@@ -477,8 +480,8 @@ class SyntacticElementTarget {
477480
/// For a pattern initialization target, retrieve the contextual pattern.
478481
ContextualPattern getContextualPattern() const;
479482

480-
/// Whether this target is for a for-in statement.
481-
bool isForEachStmt() const { return kind == Kind::forEachStmt; }
483+
/// Whether this target is for a for-in preamble, excluding the body.
484+
bool isForEachPreamble() const { return kind == Kind::forEachPreamble; }
482485

483486
/// Whether this is an initialization for an Optional.Some pattern.
484487
bool isOptionalSomePatternInit() const {
@@ -502,7 +505,7 @@ class SyntacticElementTarget {
502505
bool shouldBindPatternVarsOneWay() const {
503506
if (kind == Kind::expression)
504507
return expression.bindPatternVarsOneWay;
505-
if (kind == Kind::forEachStmt)
508+
if (kind == Kind::forEachPreamble)
506509
return !ignoreForEachWhereClause() && forEachStmt.stmt->getWhere();
507510
return false;
508511
}
@@ -553,22 +556,22 @@ class SyntacticElementTarget {
553556
}
554557

555558
bool ignoreForEachWhereClause() const {
556-
assert(isForEachStmt());
559+
assert(isForEachPreamble());
557560
return forEachStmt.ignoreWhereClause;
558561
}
559562

560563
GenericEnvironment *getPackElementEnv() const {
561-
assert(isForEachStmt());
564+
assert(isForEachPreamble());
562565
return forEachStmt.packElementEnv;
563566
}
564567

565568
const ForEachStmtInfo &getForEachStmtInfo() const {
566-
assert(isForEachStmt());
569+
assert(isForEachPreamble());
567570
return forEachStmt.info;
568571
}
569572

570573
ForEachStmtInfo &getForEachStmtInfo() {
571-
assert(isForEachStmt());
574+
assert(isForEachPreamble());
572575
return forEachStmt.info;
573576
}
574577

@@ -595,7 +598,7 @@ class SyntacticElementTarget {
595598
return;
596599
}
597600

598-
if (kind == Kind::forEachStmt) {
601+
if (kind == Kind::forEachPreamble) {
599602
forEachStmt.pattern = pattern;
600603
return;
601604
}
@@ -621,7 +624,7 @@ class SyntacticElementTarget {
621624
case Kind::caseLabelItem:
622625
case Kind::patternBinding:
623626
case Kind::uninitializedVar:
624-
case Kind::forEachStmt:
627+
case Kind::forEachPreamble:
625628
return std::nullopt;
626629

627630
case Kind::function:
@@ -638,7 +641,7 @@ class SyntacticElementTarget {
638641
case Kind::caseLabelItem:
639642
case Kind::patternBinding:
640643
case Kind::uninitializedVar:
641-
case Kind::forEachStmt:
644+
case Kind::forEachPreamble:
642645
return std::nullopt;
643646

644647
case Kind::stmtCondition:
@@ -655,7 +658,7 @@ class SyntacticElementTarget {
655658
case Kind::stmtCondition:
656659
case Kind::patternBinding:
657660
case Kind::uninitializedVar:
658-
case Kind::forEachStmt:
661+
case Kind::forEachPreamble:
659662
return std::nullopt;
660663

661664
case Kind::caseLabelItem:
@@ -672,7 +675,7 @@ class SyntacticElementTarget {
672675
case Kind::stmtCondition:
673676
case Kind::caseLabelItem:
674677
case Kind::uninitializedVar:
675-
case Kind::forEachStmt:
678+
case Kind::forEachPreamble:
676679
return nullptr;
677680

678681
case Kind::patternBinding:
@@ -689,7 +692,7 @@ class SyntacticElementTarget {
689692
case Kind::stmtCondition:
690693
case Kind::caseLabelItem:
691694
case Kind::patternBinding:
692-
case Kind::forEachStmt:
695+
case Kind::forEachPreamble:
693696
return nullptr;
694697

695698
case Kind::uninitializedVar:
@@ -706,7 +709,7 @@ class SyntacticElementTarget {
706709
case Kind::stmtCondition:
707710
case Kind::caseLabelItem:
708711
case Kind::patternBinding:
709-
case Kind::forEachStmt:
712+
case Kind::forEachPreamble:
710713
return nullptr;
711714

712715
case Kind::uninitializedVar:
@@ -726,7 +729,7 @@ class SyntacticElementTarget {
726729
case Kind::uninitializedVar:
727730
return nullptr;
728731

729-
case Kind::forEachStmt:
732+
case Kind::forEachPreamble:
730733
return forEachStmt.stmt;
731734
}
732735
llvm_unreachable("invalid case label type");
@@ -740,7 +743,7 @@ class SyntacticElementTarget {
740743
case Kind::stmtCondition:
741744
case Kind::caseLabelItem:
742745
case Kind::patternBinding:
743-
case Kind::forEachStmt:
746+
case Kind::forEachPreamble:
744747
return nullptr;
745748

746749
case Kind::uninitializedVar:
@@ -757,7 +760,7 @@ class SyntacticElementTarget {
757760
case Kind::stmtCondition:
758761
case Kind::caseLabelItem:
759762
case Kind::patternBinding:
760-
case Kind::forEachStmt:
763+
case Kind::forEachPreamble:
761764
return nullptr;
762765

763766
case Kind::uninitializedVar:
@@ -774,7 +777,7 @@ class SyntacticElementTarget {
774777
case Kind::stmtCondition:
775778
case Kind::caseLabelItem:
776779
case Kind::patternBinding:
777-
case Kind::forEachStmt:
780+
case Kind::forEachPreamble:
778781
return 0;
779782

780783
case Kind::uninitializedVar:
@@ -837,8 +840,8 @@ class SyntacticElementTarget {
837840
return uninitializedVar.declaration.get<Pattern *>()->getSourceRange();
838841
}
839842

840-
// For-in statement target doesn't cover the body.
841-
case Kind::forEachStmt:
843+
// For-in preamble target doesn't cover the body.
844+
case Kind::forEachPreamble:
842845
auto *stmt = forEachStmt.stmt;
843846
SourceLoc startLoc = stmt->getForLoc();
844847
SourceLoc endLoc = stmt->getParsedSequence()->getEndLoc();
@@ -881,7 +884,7 @@ class SyntacticElementTarget {
881884
return uninitializedVar.declaration.get<Pattern *>()->getLoc();
882885
}
883886

884-
case Kind::forEachStmt:
887+
case Kind::forEachPreamble:
885888
return forEachStmt.stmt->getStartLoc();
886889
}
887890
llvm_unreachable("invalid target type");

lib/Sema/CSGen.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -4708,8 +4708,8 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
47084708
}
47094709

47104710
static std::optional<SyntacticElementTarget>
4711-
generateForEachStmtConstraints(ConstraintSystem &cs,
4712-
SyntacticElementTarget target) {
4711+
generateForEachPreambleConstraints(ConstraintSystem &cs,
4712+
SyntacticElementTarget target) {
47134713
ForEachStmt *stmt = target.getAsForEachStmt();
47144714
auto *forEachExpr = stmt->getParsedSequence();
47154715
auto *dc = target.getDeclContext();
@@ -4952,7 +4952,7 @@ bool ConstraintSystem::generateConstraints(
49524952
}
49534953
}
49544954

4955-
case SyntacticElementTarget::Kind::forEachStmt: {
4955+
case SyntacticElementTarget::Kind::forEachPreamble: {
49564956

49574957
// Cache the outer generic environment, if it exists.
49584958
if (target.getPackElementEnv()) {
@@ -4961,7 +4961,7 @@ bool ConstraintSystem::generateConstraints(
49614961

49624962
// For a for-each statement, generate constraints for the pattern, where
49634963
// clause, and sequence traversal.
4964-
auto resultTarget = generateForEachStmtConstraints(*this, target);
4964+
auto resultTarget = generateForEachPreambleConstraints(*this, target);
49654965
if (!resultTarget)
49664966
return true;
49674967

lib/Sema/CSSyntacticElement.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ class SyntacticElementConstraintGenerator
651651
// records it as a separate conjunction element to allow for a more
652652
// granular control over what contextual information is brought into
653653
// the scope during pattern + sequence and `where` clause solving.
654-
auto target = SyntacticElementTarget::forForEachStmt(
654+
auto target = SyntacticElementTarget::forForEachPreamble(
655655
forEachStmt, context.getAsDeclContext(),
656656
/*ignoreWhereClause=*/true);
657657

lib/Sema/ConstraintSystem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7188,7 +7188,7 @@ void ConstraintSystem::diagnoseFailureFor(SyntacticElementTarget target) {
71887188
}
71897189
} else if (auto *var = target.getAsUninitializedVar()) {
71907190
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
7191-
} else if (target.isForEachStmt()) {
7191+
} else if (target.isForEachPreamble()) {
71927192
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
71937193
} else {
71947194
// Emit a poor fallback message.

lib/Sema/PreCheckExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,7 @@ bool ConstraintSystem::preCheckTarget(SyntacticElementTarget &target,
23662366
target.setExpr(expr);
23672367
}
23682368

2369-
if (target.isForEachStmt()) {
2369+
if (target.isForEachPreamble()) {
23702370
auto *stmt = target.getAsForEachStmt();
23712371

23722372
auto *sequenceExpr = stmt->getParsedSequence();

lib/Sema/SyntacticElementTarget.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ SyntacticElementTarget::forReturn(ReturnStmt *returnStmt, Type contextTy,
193193
}
194194

195195
SyntacticElementTarget
196-
SyntacticElementTarget::forForEachStmt(ForEachStmt *stmt, DeclContext *dc,
197-
bool ignoreWhereClause,
198-
GenericEnvironment *packElementEnv) {
196+
SyntacticElementTarget::forForEachPreamble(ForEachStmt *stmt, DeclContext *dc,
197+
bool ignoreWhereClause,
198+
GenericEnvironment *packElementEnv) {
199199
SyntacticElementTarget target(stmt, dc, ignoreWhereClause, packElementEnv);
200200
return target;
201201
}
@@ -235,7 +235,7 @@ ContextualPattern SyntacticElementTarget::getContextualPattern() const {
235235
uninitializedVar.index);
236236
}
237237

238-
if (isForEachStmt()) {
238+
if (isForEachPreamble()) {
239239
return ContextualPattern::forRawPattern(forEachStmt.pattern,
240240
forEachStmt.dc);
241241
}
@@ -395,7 +395,7 @@ SyntacticElementTarget::walk(ASTWalker &walker) const {
395395
}
396396
break;
397397
}
398-
case Kind::forEachStmt: {
398+
case Kind::forEachPreamble: {
399399
// We need to skip the where clause if requested, and we currently do not
400400
// type-check a for loop's BraceStmt as part of the SyntacticElementTarget,
401401
// so we need to skip it here.

lib/Sema/TypeCheckConstraints.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ void constraints::performSyntacticDiagnosticsForTarget(
394394
return;
395395
}
396396

397-
case SyntacticElementTarget::Kind::forEachStmt: {
397+
case SyntacticElementTarget::Kind::forEachPreamble: {
398398
auto *stmt = target.getAsForEachStmt();
399399

400400
// First emit diagnostics for the main expression.
@@ -928,8 +928,8 @@ bool TypeChecker::typeCheckPatternBinding(PatternBindingDecl *PBD,
928928
return hadError;
929929
}
930930

931-
bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt,
932-
GenericEnvironment *packElementEnv) {
931+
bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt,
932+
GenericEnvironment *packElementEnv) {
933933
auto &Context = dc->getASTContext();
934934
FrontendStatsTracer statsTracer(Context.Stats, "typecheck-for-each", stmt);
935935
PrettyStackTraceStmt stackTrace(Context, "type-checking-for-each", stmt);
@@ -945,7 +945,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt,
945945
return true;
946946
};
947947

948-
auto target = SyntacticElementTarget::forForEachStmt(
948+
auto target = SyntacticElementTarget::forForEachPreamble(
949949
stmt, dc, /*ignoreWhereClause=*/false, packElementEnv);
950950
if (!typeCheckTarget(target))
951951
return failed();

lib/Sema/TypeCheckStmt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
14131413
GenericEnvironment *genericSignature =
14141414
genericSigStack.empty() ? nullptr : genericSigStack.back();
14151415

1416-
if (TypeChecker::typeCheckForEachBinding(DC, S, genericSignature))
1416+
if (TypeChecker::typeCheckForEachPreamble(DC, S, genericSignature))
14171417
return nullptr;
14181418

14191419
// Type-check the body of the loop.

lib/Sema/TypeChecker.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,12 @@ bool typeCheckPatternBinding(PatternBindingDecl *PBD, unsigned patternNumber,
750750
Type patternType = Type(),
751751
TypeCheckExprOptions options = {});
752752

753-
/// Type-check a for-each loop's pattern binding and sequence together.
753+
/// Type-check a for-each loop's pattern binding, sequence, and where clause
754+
/// together.
754755
///
755756
/// \returns true if a failure occurred.
756-
bool typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt,
757-
GenericEnvironment *packElementEnv);
757+
bool typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt,
758+
GenericEnvironment *packElementEnv);
758759

759760
/// Compute the set of captures for the given function or closure.
760761
void computeCaptures(AnyFunctionRef AFR);

0 commit comments

Comments
 (0)