Skip to content

Bad diagnostic in breaking labeled block? #123261

Closed

Description

Code

fn main() {
  let args: Vec<String> = 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions