Skip to content

Commit 1bc7a04

Browse files
committed
fix: break inside async closure has incorrect span for enclosing closure
1 parent ba956ef commit 1bc7a04

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

compiler/rustc_passes/src/loops.rs

+6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
8989
hir::ExprKind::Closure(&hir::Closure {
9090
ref fn_decl, body, fn_decl_span, kind, ..
9191
}) => {
92+
let fn_decl_span = match self.cx {
93+
Coroutine { coroutine_span: span, .. } | Closure(span) => {
94+
fn_decl_span.with_lo(span.lo())
95+
}
96+
_ => fn_decl_span,
97+
};
9298
let cx = match kind {
9399
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
94100
Coroutine { coroutine_span: fn_decl_span, kind, source }

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

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ fn main() {
2020
let _ = async { break; }; //~ ERROR `break` inside `async` block
2121
let _ = async || { break; }; //~ ERROR `break` inside `async` closure
2222

23+
let _ = || || || break; //~ ERROR `break` inside of a closure
24+
let _ = async || async || async || break; //~ ERROR `break` inside `async` closure
25+
2326
let _ = gen { break; }; //~ ERROR `break` inside `gen` block
2427

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

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,30 @@ error[E0267]: `break` inside `async` closure
4141
--> $DIR/break-inside-coroutine-issue-124495.rs:21: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
48+
49+
error[E0267]: `break` inside of a closure
50+
--> $DIR/break-inside-coroutine-issue-124495.rs:23:22
51+
|
52+
LL | let _ = || || || break;
53+
| -------- ^^^^^ cannot `break` inside of a closure
54+
| |
55+
| enclosing closure
56+
57+
error[E0267]: `break` inside `async` closure
58+
--> $DIR/break-inside-coroutine-issue-124495.rs:24:40
59+
|
60+
LL | let _ = async || async || async || break;
61+
| ---------------------------^^^^^
62+
| | |
63+
| | cannot `break` inside `async` closure
64+
| enclosing `async` closure
4865

4966
error[E0267]: `break` inside `gen` block
50-
--> $DIR/break-inside-coroutine-issue-124495.rs:23:19
67+
--> $DIR/break-inside-coroutine-issue-124495.rs:26:19
5168
|
5269
LL | let _ = gen { break; };
5370
| ------^^^^^---
@@ -56,14 +73,14 @@ LL | let _ = gen { break; };
5673
| enclosing `gen` block
5774

5875
error[E0267]: `break` inside `async gen` block
59-
--> $DIR/break-inside-coroutine-issue-124495.rs:25:25
76+
--> $DIR/break-inside-coroutine-issue-124495.rs:28:25
6077
|
6178
LL | let _ = async gen { break; };
6279
| ------------^^^^^---
6380
| | |
6481
| | cannot `break` inside `async gen` block
6582
| enclosing `async gen` block
6683

67-
error: aborting due to 7 previous errors
84+
error: aborting due to 9 previous errors
6885

6986
For more information about this error, try `rustc --explain E0267`.

0 commit comments

Comments
 (0)