Skip to content

Commit 9b0547d

Browse files
committed
Add support for .await
This uses the new `await` keyword.
1 parent ae7befe commit 9b0547d

File tree

7 files changed

+20
-39
lines changed

7 files changed

+20
-39
lines changed

src/Language/Rust/Parser/Internal.y

+2-38
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ left_gen_expression(lhs,rhs,rhs2) :: { Expr Span }
10291029
--
10301030
postfix_blockexpr(lhs) :: { Expr Span }
10311031
: lhs '?' { Try [] $1 ($1 # $>) }
1032+
| lhs '.' await { Await [] $1 ($1 # $>) }
10321033
| lhs '.' ident %prec FIELD { FieldAccess [] $1 (unspan $3) ($1 # $>) }
10331034
| lhs '.' ident '(' sep_byT(expr,',') ')'
10341035
{ MethodCall [] $1 (unspan $3) Nothing $5 ($1 # $>) }
@@ -1040,44 +1041,6 @@ postfix_blockexpr(lhs) :: { Expr Span }
10401041
_ -> parseError $3
10411042
}
10421043

1043-
-- Postfix expressions that can come after an expression block, in a 'stmt'
1044-
--
1045-
-- * `{ 1 }[0]` isn't here because it is treated as `{ 1 }; [0]`
1046-
-- * `{ 1 }(0)` isn't here because it is treated as `{ 1 }; (0)`
1047-
--
1048-
postfix_blockexpr(lhs) :: { Expr Span }
1049-
: lhs '?' { Try [] $1 ($1 # $>) }
1050-
| lhs '.' ident %prec FIELD { FieldAccess [] $1 (unspan $3) ($1 # $>) }
1051-
| lhs '.' ident '(' sep_byT(expr,',') ')'
1052-
{ MethodCall [] $1 (unspan $3) Nothing $5 ($1 # $>) }
1053-
| lhs '.' ident '::' '<' sep_byT(ty,',') '>' '(' sep_byT(expr,',') ')'
1054-
{ MethodCall [] $1 (unspan $3) (Just $6) $9 ($1 # $>) }
1055-
| lhs '.' int {%
1056-
case lit $3 of
1057-
Int Dec i Unsuffixed _ -> pure (TupField [] $1 (fromIntegral i) ($1 # $3))
1058-
_ -> parseError $3
1059-
}
1060-
1061-
-- Postfix expressions that can come after an expression block, in a 'stmt'
1062-
--
1063-
-- * `{ 1 }[0]` isn't here because it is treated as `{ 1 }; [0]`
1064-
-- * `{ 1 }(0)` isn't here because it is treated as `{ 1 }; (0)`
1065-
--
1066-
postfix_blockexpr(lhs) :: { Expr Span }
1067-
: lhs '?' { Try [] $1 ($1 # $>) }
1068-
| lhs '.' ident %prec FIELD { FieldAccess [] $1 (unspan $3) ($1 # $>) }
1069-
| lhs '.' ident '(' sep_byT(expr,',') ')'
1070-
{ MethodCall [] $1 (unspan $3) Nothing $5 ($1 # $>) }
1071-
| lhs '.' ident '::' '<' sep_byT(ty,',') '>' '(' sep_byT(expr,',') ')'
1072-
{ MethodCall [] $1 (unspan $3) (Just $6) $9 ($1 # $>) }
1073-
| lhs '.' int {%
1074-
case lit $3 of
1075-
Int Dec i Unsuffixed _ -> pure (TupField [] $1 (fromIntegral i) ($1 # $3))
1076-
_ -> parseError $3
1077-
}
1078-
1079-
1080-
10811044
-- Then, we instantiate this general production into the following families of rules:
10821045
--
10831046
-- ['expr'] Most general class of expressions, no restrictions
@@ -1899,6 +1862,7 @@ addAttrs as (Closure as' c a m f e s) = Closure (as ++ as') c a m f e s
18991862
addAttrs as (BlockExpr as' b s) = BlockExpr (as ++ as') b s
19001863
addAttrs as (TryBlock as' b s) = TryBlock (as ++ as') b s
19011864
addAttrs as (Async as' c b s) = Async (as ++ as') c b s
1865+
addAttrs as (Await as' e s) = Await (as ++ as') e s
19021866
addAttrs as (Assign as' e1 e2 s) = Assign (as ++ as') e1 e2 s
19031867
addAttrs as (AssignOp as' b e1 e2 s) = AssignOp (as ++ as') b e1 e2 s
19041868
addAttrs as (FieldAccess as' e i s) = FieldAccess (as ++ as') e i s

src/Language/Rust/Pretty/Internal.hs

+4
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
390390
Async attrs cap blk x -> annotate x (hsep [ "async"
391391
, when (cap == Value) "move"
392392
, printBlockWithAttrs True blk attrs])
393+
Await{} -> chainedMethodCalls expr False id
393394
Assign _ lhs rhs x -> annotate x (hsep [ printExpr lhs, "=", printExpr rhs ])
394395
AssignOp _ op lhs rhs x -> annotate x (hsep [ printExpr lhs, printBinOp op <> "=", printExpr rhs ])
395396
FieldAccess{} -> chainedMethodCalls expr False id
@@ -442,6 +443,8 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
442443
= chainedMethodCalls s False (annotate x . (<##> fdoc (indent n (hcat [ ".", printIdent i ]))))
443444
chainedMethodCalls (Try _ s x) _ fdoc
444445
= chainedMethodCalls s False (annotate x . (<> fdoc "?"))
446+
chainedMethodCalls (Await _ s x) _ fdoc
447+
= chainedMethodCalls s False (annotate x . (<##> fdoc (indent n (hcat [ ".", "await" ]))))
445448
chainedMethodCalls (Index _ s i x) _ fdoc
446449
= chainedMethodCalls s False (annotate x . (<> fdoc ("[" <> block NoDelim True mempty mempty [printExpr i] <> "]")))
447450
chainedMethodCalls (TupField _ s i x) t fdoc
@@ -475,6 +478,7 @@ expressionAttrs (Closure as _ _ _ _ _ _) = as
475478
expressionAttrs (BlockExpr as _ _) = as
476479
expressionAttrs (TryBlock as _ _) = as
477480
expressionAttrs (Async as _ _ _) = as
481+
expressionAttrs (Await as _ _) = as
478482
expressionAttrs (Assign as _ _ _) = as
479483
expressionAttrs (AssignOp as _ _ _ _) = as
480484
expressionAttrs (FieldAccess as _ _ _) = as

src/Language/Rust/Pretty/Resolve.hs

+8-1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ resolveLhsExprP p SemiExpr l@Try{} = resolveExprP p AnyExpr l
667667
resolveLhsExprP p SemiExpr l@FieldAccess{} = resolveExprP p AnyExpr l
668668
resolveLhsExprP p SemiExpr l@MethodCall{} = resolveExprP p AnyExpr l
669669
resolveLhsExprP p SemiExpr l@TupField{} = resolveExprP p AnyExpr l
670+
resolveLhsExprP p SemiExpr l@Await{} = resolveExprP p AnyExpr l
670671
resolveLhsExprP _ SemiExpr l | isBlockLike l = parenthesize l
671672
resolveLhsExprP p t l = resolveExprP p (lhs t) l
672673
where
@@ -914,11 +915,17 @@ resolveExprP p c f@(FieldAccess as e i x) = scope f $ parenE (p > 17) $ do
914915
e' <- resolveLhsExprP 17 c e
915916
i' <- resolveIdent i
916917
pure (FieldAccess as' e' i' x)
918+
resolveExprP p SemiExpr a@Await{} = resolveExprP p AnyExpr a
919+
resolveExprP p c a@(Await as e x) = scope a $ parenE (p > 17) $ do
920+
as' <- traverse (resolveAttr OuterAttr) as
921+
--e' <- resolveExprP 17 (lhs c) e
922+
e' <- resolveLhsExprP 17 c e
923+
pure (Await as' e' x)
917924
-- Immediate expressions
918925
resolveExprP _ _ v@(Vec as es x) = scope v $ do
919926
as' <- traverse (resolveAttr EitherAttr) as
920927
es' <- traverse (resolveExprP 0 AnyExpr) es
921-
pure (Vec as' es' x)
928+
pure (Vec as' es' x)
922929
resolveExprP _ _ p@(PathExpr as Nothing p' x) = scope p $ do
923930
as' <- traverse (resolveAttr OuterAttr) as
924931
p'' <- resolvePath ExprPath p'

src/Language/Rust/Syntax/AST.hs

+3
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ data Expr a
335335
| TryBlock [Attribute a] (Block a) a
336336
-- | an async block (example: @async move { 1 }@)
337337
| Async [Attribute a] CaptureBy (Block a) a
338+
-- | an await expression (example: @foo(1,2,3).await@)
339+
| Await [Attribute a] (Expr a) a
338340
-- | assignment (example: @a = foo()@)
339341
| Assign [Attribute a] (Expr a) (Expr a) a
340342
-- | assignment with an operator (example: @a += 1@)
@@ -394,6 +396,7 @@ instance Located a => Located (Expr a) where
394396
spanOf (BlockExpr _ _ s) = spanOf s
395397
spanOf (TryBlock _ _ s) = spanOf s
396398
spanOf (Async _ _ _ s) = spanOf s
399+
spanOf (Await _ _ s) = spanOf s
397400
spanOf (Assign _ _ _ s) = spanOf s
398401
spanOf (AssignOp _ _ _ _ s) = spanOf s
399402
spanOf (FieldAccess _ _ _ s) = spanOf s

test/rustc-tests/Diff.hs

+1
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ instance Show a => Diffable (Expr a) where
916916
NullList as === (val ! "attrs" ! "_field0")
917917
e === (n ! "fields" ! 0)
918918
("Async", Async as c e _) -> error "TODO"
919+
("Await", Await as e _) -> error "TODO"
919920
_ -> diff "differing expressions:" ex val
920921
where
921922
n = val ! "node"

test/unit-tests/ParserTest.hs

+1
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ parserExpressions = testGroup "parsing expressions"
603603
, testP ".." (Range [] Nothing Nothing HalfOpen ())
604604
, testP "x?" (Try [] (PathExpr [] Nothing x ()) ())
605605
, testP "x.0" (TupField [] (PathExpr [] Nothing x ()) 0 ())
606+
, testP "x.await" (Await [] (PathExpr [] Nothing x ()) ())
606607
, testP "x.foo" (FieldAccess [] (PathExpr [] Nothing x ()) "foo" ())
607608
, testP "x.foo()" (MethodCall [] (PathExpr [] Nothing x ()) "foo" Nothing [] ())
608609
, testP "x.foo(1)" (MethodCall [] (PathExpr [] Nothing x ()) "foo" Nothing [Lit [] (Int Dec 1 Unsuffixed ()) ()] ())

test/unit-tests/PrettyTest.hs

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ prettyExpressions = testGroup "printing expressions"
333333
, testFlatten "foo = 1" (printExpr (Assign [] foo _1 ()))
334334
, testFlatten "foo += 1" (printExpr (AssignOp [] AddOp foo _1 ()))
335335
, testFlatten "foo <<= 1" (printExpr (AssignOp [] ShlOp foo _1 ()))
336+
, testFlatten "foo.await" (printExpr (Await [] foo ()))
336337
, testFlatten "foo.bar" (printExpr (FieldAccess [] foo (mkIdent "bar") ()))
337338
, testFlatten "foo.1" (printExpr (TupField [] foo 1 ()))
338339
, testFlatten "foo[1]" (printExpr (Index [] foo _1 ()))

0 commit comments

Comments
 (0)