Bad diagnostic in breaking labeled block? · Issue #123261 · rust-lang/rust (original) (raw)

Code

fn main() { let args: Vec = std::env::args().collect(); let n = args.len(); let x = { print!(""); if n == 0 { break 1; } break 0; }; }

Current output

error[E0268]: break outside of a loop or labeled block --> hello.rs:7:7 | 7 | break 1; | ^^^^^^^ cannot break outside of a loop or labeled block | help: consider labeling this block to be able to break within it | 6 ~ if n == 0 'block: { 7 ~ break 'block 1; |

error[E0268]: break outside of a loop or labeled block --> hello.rs:9:5 | 9 | break 0; | ^^^^^^^ cannot break outside of a loop or labeled block | help: consider labeling this block to be able to break within it | 4 ~ let x = 'block: { 5 | print!(""); ... 8 | } 9 ~ break 'block 0; |

error: aborting due to 2 previous errors

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

Desired output

error[E0268]: break outside of a loop or labeled block --> hello.rs:9:5 | 9 | break 0; | ^^^^^^^ cannot break outside of a loop or labeled block | help: consider labeling this block to be able to break within it | 4 ~ let x = 'block: { 5 | print!(""); ... 8 | } 9 ~ break 'block 0; |

error: aborting due to 2 previous errors

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

Rationale and extra context

The problem in the diagnostic is to mention the hint:

help: consider labeling this block to be able to break within it
  |
6 ~     if n == 0 'block: {
7 ~       break 'block 1;

Which is wrong because a label cannot be set here after an if clause

Other cases

No response

Rust Version

rustc --version --verbose rustc 1.76.0 (07dca489a 2024-02-04) binary: rustc commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce commit-date: 2024-02-04 host: x86_64-apple-darwin release: 1.76.0 LLVM version: 17.0.6

Anything else?

https://users.rust-lang.org/t/bad-diagnostic-in-breaking-labeled-block/109124