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

Bad diagnostic in breaking labeled block? #123261

Closed
chacha21 opened this issue Mar 31, 2024 · 1 comment · Fixed by #123623
Closed

Bad diagnostic in breaking labeled block? #123261

chacha21 opened this issue Mar 31, 2024 · 1 comment · Fixed by #123623
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chacha21
Copy link

chacha21 commented Mar 31, 2024

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

@chacha21 chacha21 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. labels Mar 31, 2024
@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 31, 2024
@surechen
Copy link
Contributor

surechen commented Apr 1, 2024

@rustbot claim

surechen added a commit to surechen/rust that referenced this issue Apr 8, 2024
…k, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
surechen added a commit to surechen/rust that referenced this issue Apr 9, 2024
…k, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
surechen added a commit to surechen/rust that referenced this issue Apr 11, 2024
…k, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
surechen added a commit to surechen/rust that referenced this issue Apr 11, 2024
…k, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
surechen added a commit to surechen/rust that referenced this issue May 21, 2024
…r we wiil get another err: block label not supported here.

fixes rust-lang#123261
surechen added a commit to surechen/rust that referenced this issue May 21, 2024
…r we wiil get another err: block label not supported here.

fixes rust-lang#123261
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 23, 2024
Fix OutsideLoop's error suggestion: adding label `'block` for `if` block.

For OutsideLoop we should not suggest add `'block` label in `if` block, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
@bors bors closed this as completed in 8fde7e3 May 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 23, 2024
Rollup merge of rust-lang#123623 - surechen:fix_123261, r=estebank

Fix OutsideLoop's error suggestion: adding label `'block` for `if` block.

For OutsideLoop we should not suggest add `'block` label in `if` block, or we wiil get another err: block label not supported here.

fixes rust-lang#123261
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

3 participants