Skip to content

Commit

Permalink
fix: break inside async closure has incorrect span for enclosing closure
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed May 27, 2024
1 parent ba956ef commit 1ecf1a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
32 changes: 20 additions & 12 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn_decl_span,
fn_arg_span,
}) => match coroutine_kind {
Some(coroutine_kind) => self.lower_expr_coroutine_closure(
binder,
*capture_clause,
e.id,
hir_id,
*coroutine_kind,
fn_decl,
body,
*fn_decl_span,
*fn_arg_span,
),
Some(coroutine_kind) => self.with_new_scopes(*fn_decl_span, |this| {
this.lower_expr_coroutine_closure(
binder,
*capture_clause,
e.id,
hir_id,
*coroutine_kind,
fn_decl,
body,
*fn_decl_span,
*fn_arg_span,
)
}),
None => self.lower_expr_closure(
binder,
*capture_clause,
Expand Down Expand Up @@ -684,6 +686,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
(params, res)
});

let fn_decl_span = if matches!(coroutine_source, hir::CoroutineSource::Closure) {
self.current_item.map_or(span, |fn_decl_span| span.with_lo(fn_decl_span.lo()))
} else {
span
};

// `static |<_task_context?>| -> <return_ty> { <body> }`:
hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
def_id: self.local_def_id(closure_node_id),
Expand All @@ -692,7 +700,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
bound_generic_params: &[],
fn_decl,
body,
fn_decl_span: self.lower_span(span),
fn_decl_span: self.lower_span(fn_decl_span),
fn_arg_span: None,
kind: hir::ClosureKind::Coroutine(coroutine_kind),
constness: hir::Constness::NotConst,
Expand Down
9 changes: 5 additions & 4 deletions tests/ui/async-await/async-closures/wrong-fn-kind.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ LL | fn needs_async_fn(_: impl async Fn()) {}
| ^^^^^^^^^^ required by this bound in `needs_async_fn`

error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/wrong-fn-kind.rs:9:29
--> $DIR/wrong-fn-kind.rs:9:20
|
LL | fn needs_async_fn(_: impl async Fn()) {}
| --------------- change this to accept `FnMut` instead of `Fn`
...
LL | needs_async_fn(async || {
| _____--------------_--------_^
| | | |
| | | in this closure
| -------------- ^-------
| | |
| _____|______________in this closure
| | |
| | expects `Fn` instead of `FnMut`
LL | |
LL | | x += 1;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/coroutine/break-inside-coroutine-issue-124495.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async gen fn async_gen_fn() {

fn main() {
let _ = async { break; }; //~ ERROR `break` inside `async` block

let _ = async || { break; }; //~ ERROR `break` inside `async` closure

let _ = gen { break; }; //~ ERROR `break` inside `gen` block
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/coroutine/break-inside-coroutine-issue-124495.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ LL | let _ = async { break; };
| enclosing `async` block

error[E0267]: `break` inside `async` closure
--> $DIR/break-inside-coroutine-issue-124495.rs:21:24
--> $DIR/break-inside-coroutine-issue-124495.rs:22:24
|
LL | let _ = async || { break; };
| --^^^^^---
| | |
| | cannot `break` inside `async` closure
| enclosing `async` closure
| -----------^^^^^---
| | |
| | cannot `break` inside `async` closure
| enclosing `async` closure

error[E0267]: `break` inside `gen` block
--> $DIR/break-inside-coroutine-issue-124495.rs:23:19
--> $DIR/break-inside-coroutine-issue-124495.rs:24:19
|
LL | let _ = gen { break; };
| ------^^^^^---
Expand All @@ -56,7 +56,7 @@ LL | let _ = gen { break; };
| enclosing `gen` block

error[E0267]: `break` inside `async gen` block
--> $DIR/break-inside-coroutine-issue-124495.rs:25:25
--> $DIR/break-inside-coroutine-issue-124495.rs:26:25
|
LL | let _ = async gen { break; };
| ------------^^^^^---
Expand Down

0 comments on commit 1ecf1a2

Please sign in to comment.