Skip to content

[Concurrency] Implement parsing and semantic analysis of await operator #33199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ ERROR(expected_colon_after_if_question,none,
"expected ':' after '? ...' in ternary expression", ())
ERROR(expected_expr_after_if_colon,none,
"expected expression after '? ... :' in ternary expression", ())
ERROR(expected_expr_after_try, none,
"expected expression after 'try'", ())
ERROR(expected_expr_after_await, none,
"expected expression after 'await'", ())

// Cast expressions
ERROR(expected_type_after_is,none,
Expand Down
6 changes: 5 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ ERROR(missing_builtin_precedence_group,none,
(Identifier))

// If you change this, also change enum TryKindForDiagnostics.
#define TRY_KIND_SELECT(SUB) "%select{try|try!|try?}" #SUB
#define TRY_KIND_SELECT(SUB) "%select{try|try!|try?|await}" #SUB

ERROR(try_rhs,none,
"'" TRY_KIND_SELECT(0) "' cannot appear to the right of a "
Expand Down Expand Up @@ -4019,6 +4019,10 @@ NOTE(note_error_to_optional,none,
"did you mean to handle error as optional value?", ())
NOTE(note_disable_error_propagation,none,
"did you mean to disable error propagation?", ())
ERROR(async_call_without_await,none,
"call is 'async' but is not marked with 'await'", ())
WARNING(no_async_in_await,none,
"no calls to 'async' functions occur within 'await' expression", ())

WARNING(no_throw_in_try,none,
"no calls to throwing functions occur within 'try' expression", ())
Expand Down
34 changes: 31 additions & 3 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ class TryExpr : public AnyTryExpr {
/// ForceTryExpr - A 'try!' surrounding an expression, marking that
/// the expression contains code which might throw, but that the code
/// should dynamically assert if it does.
class ForceTryExpr : public AnyTryExpr {
class ForceTryExpr final : public AnyTryExpr {
SourceLoc ExclaimLoc;

public:
Expand All @@ -1983,7 +1983,7 @@ class ForceTryExpr : public AnyTryExpr {
/// A 'try?' surrounding an expression, marking that the expression contains
/// code which might throw, and that the result should be injected into an
/// Optional. If the code does throw, \c nil is produced.
class OptionalTryExpr : public AnyTryExpr {
class OptionalTryExpr final : public AnyTryExpr {
SourceLoc QuestionLoc;

public:
Expand All @@ -2002,7 +2002,6 @@ class OptionalTryExpr : public AnyTryExpr {
/// An expression node that does not affect the evaluation of its subexpression.
class IdentityExpr : public Expr {
Expr *SubExpr;

public:
IdentityExpr(ExprKind kind,
Expr *subExpr, Type ty = Type(),
Expand Down Expand Up @@ -2093,6 +2092,32 @@ class ParenExpr : public IdentityExpr {
static bool classof(const Expr *E) { return E->getKind() == ExprKind::Paren; }
};

/// AwaitExpr - An 'await' surrounding an expression, marking that the
/// expression contains code which is a coroutine that may block.
///
/// getSemanticsProvidingExpr() looks through this because it doesn't
/// provide the value and only very specific clients care where the
/// 'await' was written.
class AwaitExpr final : public IdentityExpr {
SourceLoc AwaitLoc;
public:
AwaitExpr(SourceLoc awaitLoc, Expr *sub, Type type = Type(),
bool implicit = false)
: IdentityExpr(ExprKind::Await, sub, type, implicit), AwaitLoc(awaitLoc) {
}

SourceLoc getLoc() const { return AwaitLoc; }

SourceLoc getAwaitLoc() const { return AwaitLoc; }
SourceLoc getStartLoc() const { return AwaitLoc; }
SourceLoc getEndLoc() const { return getSubExpr()->getEndLoc(); }

static bool classof(const Expr *e) {
return e->getKind() == ExprKind::Await;
}
};


/// TupleExpr - Parenthesized expressions like '(a: x+x)' and '(x, y, 4)'. Also
/// used to represent the operands to a binary operator. Note that
/// expressions like '(4)' are represented with a ParenExpr.
Expand Down Expand Up @@ -3691,6 +3716,9 @@ class AbstractClosureExpr : public DeclContext, public Expr {
/// Return whether this closure is throwing when fully applied.
bool isBodyThrowing() const;

/// \brief Return whether this closure is async when fully applied.
bool isBodyAsync() const;

/// Whether this closure consists of a single expression.
bool hasSingleExpressionBody() const;

Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/ExprNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ UNCHECKED_EXPR(Sequence, Expr)
ABSTRACT_EXPR(Identity, Expr)
EXPR(Paren, IdentityExpr)
EXPR(DotSelf, IdentityExpr)
EXPR_RANGE(Identity, Paren, DotSelf)
EXPR(Await, IdentityExpr)
EXPR_RANGE(Identity, Paren, Await)
ABSTRACT_EXPR(AnyTry, Expr)
EXPR(Try, AnyTryExpr)
EXPR(ForceTry, AnyTryExpr)
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,13 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
printRec(E->getSubExpr());
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}
void visitAwaitExpr(AwaitExpr *E) {
printCommon(E, "await_expr");
OS << '\n';
printRec(E->getSubExpr());
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitTupleExpr(TupleExpr *E) {
printCommon(E, "tuple_expr");
if (E->hasTrailingClosure())
Expand Down
12 changes: 11 additions & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
->getSubExpr()->getReferencedDecl(stopAtParenExpr);

PASS_THROUGH_REFERENCE(DotSelf, getSubExpr);
PASS_THROUGH_REFERENCE(Await, getSubExpr);
PASS_THROUGH_REFERENCE(Try, getSubExpr);
PASS_THROUGH_REFERENCE(ForceTry, getSubExpr);
PASS_THROUGH_REFERENCE(OptionalTry, getSubExpr);
Expand Down Expand Up @@ -623,6 +624,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
case ExprKind::DynamicType:
return true;

case ExprKind::Await:
case ExprKind::Try:
case ExprKind::ForceTry:
case ExprKind::OptionalTry:
Expand Down Expand Up @@ -1929,10 +1931,17 @@ Type AbstractClosureExpr::getResultType(
bool AbstractClosureExpr::isBodyThrowing() const {
if (getType()->hasError())
return false;

return getType()->castTo<FunctionType>()->getExtInfo().throws();
}

bool AbstractClosureExpr::isBodyAsync() const {
if (getType()->hasError())
return false;

return getType()->castTo<FunctionType>()->getExtInfo().async();
}

bool AbstractClosureExpr::hasSingleExpressionBody() const {
if (auto closure = dyn_cast<ClosureExpr>(this))
return closure->hasSingleExpressionBody();
Expand Down Expand Up @@ -2485,3 +2494,4 @@ const UnifiedStatsReporter::TraceFormatter*
FrontendStatsTracer::getTraceFormatter<const Expr *>() {
return &TF;
}

20 changes: 17 additions & 3 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
/// parseExprSequenceElement
///
/// expr-sequence-element(Mode):
/// 'try' expr-unary(Mode)
/// 'try' '?' expr-unary(Mode)
/// 'try' '!' expr-unary(Mode)
/// '__await' expr-sequence-element(Mode)
/// 'try' expr-sequence-element(Mode)
/// 'try' '?' expr-sequence-element(Mode)
/// 'try' '!' expr-sequence-element(Mode)
/// expr-unary(Mode)
///
/// 'try' is not actually allowed at an arbitrary position of a
Expand All @@ -390,6 +391,19 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
bool isExprBasic) {
SyntaxParsingContext ElementContext(SyntaxContext,
SyntaxContextKind::Expr);

if (Context.LangOpts.EnableExperimentalConcurrency &&
Tok.is(tok::kw___await)) {
SourceLoc awaitLoc = consumeToken(tok::kw___await);
ParserResult<Expr> sub = parseExprUnary(message, isExprBasic);
if (!sub.hasCodeCompletion() && !sub.isNull()) {
ElementContext.setCreateSyntax(SyntaxKind::TryExpr);
sub = makeParserResult(new (Context) AwaitExpr(awaitLoc, sub.get()));
}

return sub;
}

SourceLoc tryLoc;
bool hadTry = consumeIf(tok::kw_try, tryLoc);
Optional<Token> trySuffix;
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool Parser::isStartOfStmt() {

case tok::kw_try: {
// "try" cannot actually start any statements, but we parse it there for
// better recovery.
// better recovery in cases like 'try return'.
Parser::BacktrackingScope backtrack(*this);
consumeToken(tok::kw_try);
return isStartOfStmt();
Expand Down
Loading