Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

break inside gen blocks misidentifies gen block as a closure #124495

Closed
dtolnay opened this issue Apr 28, 2024 · 1 comment · Fixed by #124777
Closed

break inside gen blocks misidentifies gen block as a closure #124495

dtolnay opened this issue Apr 28, 2024 · 1 comment · Fixed by #124777
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-gen_blocks `gen {}` expressions that produce `Iterator`s requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Apr 28, 2024

Code

#![feature(gen_blocks)]

fn main() {
    let _ = || break;
    let _ = async { break };
    let _ = gen { break };
}

Current output

error[E0267]: `break` inside of a closure
 --> src/main.rs:4:16
  |
4 |     let _ = || break;
  |             -- ^^^^^ cannot `break` inside of a closure
  |             |
  |             enclosing closure

error[E0267]: `break` inside of an `async` block
 --> src/main.rs:5:21
  |
5 |     let _ = async { break };
  |             --------^^^^^--
  |             |       |
  |             |       cannot `break` inside of an `async` block
  |             enclosing `async` block

error[E0267]: `break` inside of a closure
 --> src/main.rs:6:19
  |
6 |     let _ = gen { break };
  |             ------^^^^^--
  |             |     |
  |             |     cannot `break` inside of a closure
  |             enclosing closure

Desired output

...

error[E0267]: `break` inside of a `gen` block
 --> src/main.rs:6:19
  |
6 |     let _ = gen { break };
  |             ------^^^^^--
  |             |     |
  |             |     cannot `break` inside of a `gen` block
  |             enclosing `gen` block

Rationale and extra context

Same issue as #63391 which was for async blocks.

Rust Version

rustc 1.79.0-nightly (aed2187d5 2024-04-27)
binary: rustc
commit-hash: aed2187d53b8789e3a37f50ae36f894a2a679077
commit-date: 2024-04-27
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4
@dtolnay dtolnay added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. F-gen_blocks `gen {}` expressions that produce `Iterator`s labels Apr 28, 2024
@veera-sivarajan
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 9b834d0 May 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 9, 2024
Rollup merge of rust-lang#124777 - veera-sivarajan:bugfix-124495-identify-gen-block, r=compiler-errors

Fix Error Messages for `break` Inside Coroutines

Fixes rust-lang#124495

Previously, `break` inside `gen` blocks and functions
were incorrectly identified to be enclosed by a closure.

This PR fixes it by displaying an appropriate error message
for async blocks, async closures, async functions, gen blocks,
gen closures, gen functions, async gen blocks, async gen closures
and async gen functions.

Note: gen closure and async gen closure are not supported by the
compiler yet but I have added an error message here assuming that
they might be implemented in the future.

~~Also, fixes grammar in a few places by replacing
`inside of a $coroutine` with `inside a $coroutine`.~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-gen_blocks `gen {}` expressions that produce `Iterator`s requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants