Skip to content

Commit 05b7b46

Browse files
committed
fix: break inside async closure has incorrect span for enclosing closure
1 parent a330e49 commit 05b7b46

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

compiler/rustc_ast_lowering/src/item.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
13111311
CoroutineKind::AsyncGen { .. } => hir::CoroutineDesugaring::AsyncGen,
13121312
};
13131313
let closure_id = coroutine_kind.closure_id();
1314+
1315+
let span = if let FnRetTy::Default(span) = decl.output
1316+
&& matches!(coroutine_source, rustc_hir::CoroutineSource::Closure)
1317+
{
1318+
body_span.with_lo(span.lo())
1319+
} else {
1320+
body_span
1321+
};
13141322
let coroutine_expr = self.make_desugared_coroutine_expr(
13151323
// The default capture mode here is by-ref. Later on during upvar analysis,
13161324
// we will force the captured arguments to by-move, but for async closures,
@@ -1319,7 +1327,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13191327
CaptureBy::Ref,
13201328
closure_id,
13211329
None,
1322-
body_span,
1330+
span,
13231331
desugaring_kind,
13241332
coroutine_source,
13251333
mkbody,

tests/ui/async-await/async-closures/wrong-fn-kind.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ LL | fn needs_async_fn(_: impl async Fn()) {}
2020
| ^^^^^^^^^^ required by this bound in `needs_async_fn`
2121

2222
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
23-
--> $DIR/wrong-fn-kind.rs:9:29
23+
--> $DIR/wrong-fn-kind.rs:9:20
2424
|
2525
LL | fn needs_async_fn(_: impl async Fn()) {}
2626
| --------------- change this to accept `FnMut` instead of `Fn`
2727
...
2828
LL | needs_async_fn(async || {
29-
| _____--------------_--------_^
30-
| | | |
31-
| | | in this closure
29+
| -------------- ^-------
30+
| | |
31+
| _____|______________in this closure
32+
| | |
3233
| | expects `Fn` instead of `FnMut`
3334
LL | |
3435
LL | | x += 1;

tests/ui/coroutine/break-inside-coroutine-issue-124495.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async gen fn async_gen_fn() {
1818

1919
fn main() {
2020
let _ = async { break; }; //~ ERROR `break` inside `async` block
21+
2122
let _ = async || { break; }; //~ ERROR `break` inside `async` closure
2223

2324
let _ = gen { break; }; //~ ERROR `break` inside `gen` block

tests/ui/coroutine/break-inside-coroutine-issue-124495.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ LL | let _ = async { break; };
3838
| enclosing `async` block
3939

4040
error[E0267]: `break` inside `async` closure
41-
--> $DIR/break-inside-coroutine-issue-124495.rs:21:24
41+
--> $DIR/break-inside-coroutine-issue-124495.rs:22:24
4242
|
4343
LL | let _ = async || { break; };
44-
| --^^^^^---
45-
| | |
46-
| | cannot `break` inside `async` closure
47-
| enclosing `async` closure
44+
| -----------^^^^^---
45+
| | |
46+
| | cannot `break` inside `async` closure
47+
| enclosing `async` closure
4848

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

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

0 commit comments

Comments
 (0)