Skip to content

Commit 70b092e

Browse files
Adjust error yield/await lowering
1 parent fc3800f commit 70b092e

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+39-9
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
764764
Some(hir::CoroutineKind::Coroutine(_))
765765
| Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _))
766766
| None => {
767-
return hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
768-
await_kw_span,
769-
item_span: self.current_item,
770-
}));
767+
let stmt_id = self.next_id();
768+
let expr_err = self.expr(
769+
expr.span,
770+
hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
771+
await_kw_span,
772+
item_span: self.current_item,
773+
})),
774+
);
775+
return hir::ExprKind::Block(
776+
self.block_all(
777+
expr.span,
778+
arena_vec![self; hir::Stmt {
779+
hir_id: stmt_id,
780+
kind: hir::StmtKind::Semi(expr),
781+
span: expr.span,
782+
}],
783+
Some(self.arena.alloc(expr_err)),
784+
),
785+
None,
786+
);
771787
}
772788
};
773789

@@ -1500,12 +1516,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
15001516
}
15011517

15021518
fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1519+
let yielded =
1520+
opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1521+
15031522
let is_async_gen = match self.coroutine_kind {
15041523
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => false,
15051524
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
15061525
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
1507-
return hir::ExprKind::Err(
1508-
self.dcx().emit_err(AsyncCoroutinesNotSupported { span }),
1526+
let stmt_id = self.next_id();
1527+
let expr_err = self.expr(
1528+
yielded.span,
1529+
hir::ExprKind::Err(self.dcx().emit_err(AsyncCoroutinesNotSupported { span })),
1530+
);
1531+
return hir::ExprKind::Block(
1532+
self.block_all(
1533+
yielded.span,
1534+
arena_vec![self; hir::Stmt {
1535+
hir_id: stmt_id,
1536+
kind: hir::StmtKind::Semi(yielded),
1537+
span: yielded.span,
1538+
}],
1539+
Some(self.arena.alloc(expr_err)),
1540+
),
1541+
None,
15091542
);
15101543
}
15111544
Some(hir::CoroutineKind::Coroutine(_)) => {
@@ -1535,9 +1568,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
15351568
}
15361569
};
15371570

1538-
let yielded =
1539-
opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1540-
15411571
if is_async_gen {
15421572
// `yield $expr` is transformed into `task_context = yield async_gen_ready($expr)`.
15431573
// This ensures that we store our resumed `ResumeContext` correctly, and also that
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition:2021
2+
3+
fn main() {
4+
async {
5+
use std::ops::Add;
6+
let _ = 1.add(3);
7+
}.await
8+
//~^ ERROR `await` is only allowed inside `async` functions and blocks
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0728]: `await` is only allowed inside `async` functions and blocks
2+
--> $DIR/async-outside-of-await-issue-121096.rs:7:7
3+
|
4+
LL | fn main() {
5+
| ---- this is not `async`
6+
...
7+
LL | }.await
8+
| ^^^^^ only allowed inside `async` functions and blocks
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0728`.

0 commit comments

Comments
 (0)