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 14, 2024
1 parent ba956ef commit 1bc7a04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_passes/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprKind::Closure(&hir::Closure {
ref fn_decl, body, fn_decl_span, kind, ..
}) => {
let fn_decl_span = match self.cx {
Coroutine { coroutine_span: span, .. } | Closure(span) => {
fn_decl_span.with_lo(span.lo())
}
_ => fn_decl_span,
};
let cx = match kind {
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
Coroutine { coroutine_span: fn_decl_span, kind, source }
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/coroutine/break-inside-coroutine-issue-124495.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ fn main() {
let _ = async { break; }; //~ ERROR `break` inside `async` block
let _ = async || { break; }; //~ ERROR `break` inside `async` closure

let _ = || || || break; //~ ERROR `break` inside of a closure
let _ = async || async || async || break; //~ ERROR `break` inside `async` closure

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

let _ = async gen { break; }; //~ ERROR `break` inside `async gen` block
Expand Down
31 changes: 24 additions & 7 deletions tests/ui/coroutine/break-inside-coroutine-issue-124495.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,30 @@ error[E0267]: `break` inside `async` closure
--> $DIR/break-inside-coroutine-issue-124495.rs:21: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 of a closure
--> $DIR/break-inside-coroutine-issue-124495.rs:23:22
|
LL | let _ = || || || break;
| -------- ^^^^^ cannot `break` inside of a closure
| |
| enclosing closure

error[E0267]: `break` inside `async` closure
--> $DIR/break-inside-coroutine-issue-124495.rs:24:40
|
LL | let _ = async || async || async || break;
| ---------------------------^^^^^
| | |
| | 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:26:19
|
LL | let _ = gen { break; };
| ------^^^^^---
Expand All @@ -56,14 +73,14 @@ 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:28:25
|
LL | let _ = async gen { break; };
| ------------^^^^^---
| | |
| | cannot `break` inside `async gen` block
| enclosing `async gen` block

error: aborting due to 7 previous errors
error: aborting due to 9 previous errors

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

0 comments on commit 1bc7a04

Please sign in to comment.