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

Account for missing ' in break tags #81192

Closed
estebank opened this issue Jan 19, 2021 · 0 comments · Fixed by #81236
Closed

Account for missing ' in break tags #81192

estebank opened this issue Jan 19, 2021 · 0 comments · Fixed by #81236
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given

fn main() {
    'a: for _ in 0..1 {
        break 'a;
    }
    'b: for _ in 0..1 {
        break b;
    }
    c: for _ in 0..1 {
        break 'c;
    }
    d: for _ in 0..1 {
        break d;
    }
}

the current output is

error: expected identifier, found keyword `for`
 --> src/main.rs:8:8
  |
8 |     c: for _ in 0..1 {
  |        ^^^ expected identifier, found keyword

error: expected `<`, found reserved identifier `_`
 --> src/main.rs:8:12
  |
8 |     c: for _ in 0..1 {
  |      -     ^ expected `<`
  |      |
  |      tried to parse a type due to this type ascription
  |
  = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
  = note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error[E0425]: cannot find value `b` in this scope
 --> src/main.rs:6:15
  |
6 |         break b;
  |               ^
  |               |
  |               not found in this scope
  |               help: a label with a similar name exists: `'b`

warning: unused label
 --> src/main.rs:5:5
  |
5 |     'b: for _ in 0..1 {
  |     ^^
  |
  = note: `#[warn(unused_labels)]` on by default

error[E0571]: `break` with value from a `for` loop
 --> src/main.rs:6:9
  |
6 |         break b;
  |         ^^^^^^^ can only break with a value inside `loop` or breakable block
  |
help: instead, use `break` on its own without a value inside this `for` loop
  |
6 |         break;
  |         ^^^^^

error: aborting due to 4 previous errors; 1 warning emitted

It should instead be 5 errors with appropriate suggestions to add the missing ' to the tags.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Jan 19, 2021
@bors bors closed this as completed in 3682a06 Jan 22, 2021
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 30, 2021
Gracefully handle loop labels missing leading `'` in different positions

Fix rust-lang#81192.

* Account for labels when suggesting `loop` instead of `while true`
* Suggest `'a` when given `a` only when appropriate
* Add loop head span to hir
* Tweak error for invalid `break expr`
* Add more misspelled label tests
* Avoid emitting redundant "unused label" lint
* Parse loop labels missing a leading `'`

Each commit can be reviewed in isolation.
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-parser Area: The parsing of Rust source code to an AST D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

1 participant