Skip to content

Commit d682168

Browse files
committed
Convert async flag to source locations and add initial try support to for await in syntax
1 parent 51fec5b commit d682168

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

include/swift/AST/Stmt.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ class RepeatWhileStmt : public LabeledStmt {
726726
/// \endcode
727727
class ForEachStmt : public LabeledStmt {
728728
SourceLoc ForLoc;
729-
bool IsAsync;
729+
SourceLoc TryLoc;
730+
SourceLoc AwaitLoc;
730731
Pattern *Pat;
731732
SourceLoc InLoc;
732733
Expr *Sequence;
@@ -742,12 +743,12 @@ class ForEachStmt : public LabeledStmt {
742743
Expr *convertElementExpr = nullptr;
743744

744745
public:
745-
ForEachStmt(LabeledStmtInfo LabelInfo, SourceLoc ForLoc, bool IsAsync, Pattern *Pat,
746-
SourceLoc InLoc, Expr *Sequence, SourceLoc WhereLoc,
746+
ForEachStmt(LabeledStmtInfo LabelInfo, SourceLoc ForLoc, SourceLoc TryLoc, SourceLoc AwaitLoc,
747+
Pattern *Pat, SourceLoc InLoc, Expr *Sequence, SourceLoc WhereLoc,
747748
Expr *WhereExpr, BraceStmt *Body, Optional<bool> implicit = None)
748749
: LabeledStmt(StmtKind::ForEach, getDefaultImplicitFlag(implicit, ForLoc),
749750
LabelInfo),
750-
ForLoc(ForLoc), IsAsync(IsAsync), Pat(nullptr), InLoc(InLoc), Sequence(Sequence),
751+
ForLoc(ForLoc), TryLoc(TryLoc), AwaitLoc(AwaitLoc), Pat(nullptr), InLoc(InLoc), Sequence(Sequence),
751752
WhereLoc(WhereLoc), WhereExpr(WhereExpr), Body(Body) {
752753
setPattern(Pat);
753754
}
@@ -780,7 +781,8 @@ class ForEachStmt : public LabeledStmt {
780781
/// getWhereLoc - Retrieve the location of the 'where' keyword.
781782
SourceLoc getWhereLoc() const { return WhereLoc; }
782783

783-
bool isAsync() const { return IsAsync; }
784+
SourceLoc getAwaitLoc() const { return AwaitLoc; }
785+
SourceLoc getTryLoc() const { return TryLoc; }
784786

785787
/// getPattern - Retrieve the pattern describing the iteration variables.
786788
/// These variables will only be visible within the body of the loop.

lib/Parse/ParseStmt.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,12 +2124,18 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
21242124
// lookahead to resolve what is going on.
21252125
bool IsCStyleFor = isStmtForCStyle(*this);
21262126
auto StartOfControl = Tok.getLoc();
2127-
bool IsAsync = false;
2127+
SourceLoc AwaitLoc;
2128+
SourceLoc TryLoc;
21282129

21292130
if (shouldParseExperimentalConcurrency() &&
21302131
Tok.isContextualKeyword("await")) {
2131-
consumeToken();
2132-
IsAsync = true;
2132+
AwaitLoc = consumeToken();
2133+
} if (shouldParseExperimentalConcurrency() &&
2134+
Tok.is(tok::kw_try)) {
2135+
TryLoc = consumeToken();
2136+
if (Tok.isContextualKeyword("await")) {
2137+
AwaitLoc = consumeToken();
2138+
}
21332139
}
21342140

21352141
// Parse the pattern. This is either 'case <refutable pattern>' or just a
@@ -2225,7 +2231,7 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
22252231

22262232
return makeParserResult(
22272233
Status,
2228-
new (Context) ForEachStmt(LabelInfo, ForLoc, IsAsync, pattern.get(), InLoc,
2234+
new (Context) ForEachStmt(LabelInfo, ForLoc, TryLoc, AwaitLoc, pattern.get(), InLoc,
22292235
Container.get(), WhereLoc, Where.getPtrOrNull(),
22302236
Body.get()));
22312237
}

lib/SILGen/SILGenStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ void StmtEmitter::visitAsyncForEachStmt(ForEachStmt *S) {
11981198
}
11991199

12001200
void StmtEmitter::visitForEachStmt(ForEachStmt *S) {
1201-
if (S->isAsync()) {
1201+
if (S->getAwaitLoc().isValid()) {
12021202
visitAsyncForEachStmt(S);
12031203
return;
12041204
}

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ class BuilderClosureVisitor
774774
// take care of this.
775775
auto sequenceProto = TypeChecker::getProtocol(
776776
dc->getASTContext(), forEachStmt->getForLoc(),
777-
forEachStmt->isAsync() ?
777+
forEachStmt->getAwaitLoc().isValid() ?
778778
KnownProtocolKind::AsyncSequence : KnownProtocolKind::Sequence);
779779
if (!sequenceProto) {
780780
if (!unhandledNode)

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3655,7 +3655,7 @@ generateForEachStmtConstraints(
36553655
ConstraintSystem &cs, SolutionApplicationTarget target, Expr *sequence) {
36563656
auto forEachStmtInfo = target.getForEachStmtInfo();
36573657
ForEachStmt *stmt = forEachStmtInfo.stmt;
3658-
bool isAsync = stmt->isAsync();
3658+
bool isAsync = stmt->getAwaitLoc().isValid();
36593659

36603660
auto locator = cs.getConstraintLocator(sequence);
36613661
auto contextualLocator =

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
563563

564564
auto sequenceProto = TypeChecker::getProtocol(
565565
dc->getASTContext(), stmt->getForLoc(),
566-
stmt->isAsync() ?
566+
stmt->getAwaitLoc().isValid() ?
567567
KnownProtocolKind::AsyncSequence : KnownProtocolKind::Sequence);
568568
if (!sequenceProto)
569569
return failed();

0 commit comments

Comments
 (0)