Skip to content

Parse error on labeled loop after break #86948

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

Closed
LordHavelockVetinari opened this issue Jul 7, 2021 · 0 comments · Fixed by #87026
Closed

Parse error on labeled loop after break #86948

LordHavelockVetinari opened this issue Jul 7, 2021 · 0 comments · Fixed by #87026
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@LordHavelockVetinari
Copy link

While playing with labels in Rust, I noticed that labeled loops can be used as expressions, but aren't parsed correctly after break:

// This is OK:
let a = 'first_loop: loop {
    break 'first_loop 1;
};
// This isn't:
let b = loop {
    // Note the colon after 'inner_loop.
    break 'inner_loop: loop {
        break 'inner_loop 1;
    };
};

Output:

error: expected identifier, found keyword `loop`
 --> src\main.rs:9:28
  |
9 |         break 'inner_loop: loop {
  |                            ^^^^ expected identifier, found keyword
  |
help: you can escape reserved keywords to use them as identifiers
  |
9 |         break 'inner_loop: r#loop {
  |                            ^^^^^^

error: expected type, found keyword `loop`
 --> src\main.rs:9:28
  |
9 |         break 'inner_loop: loop {
  |                          - ^^^^ expected type
  |                          |
  |                          help: maybe write a path separator here: `::`

error: aborting due to 2 previous errors

This only happens when the label comes immediately after break, so this does compile:

loop {
    break ('inner: loop {
        break 'inner 1;
    });
};
loop {
    break 1 + 'inner: loop {
        break 'inner 1;
    };
};
'outer: loop {
    break 'outer 'inner: loop {
        break 'inner 1;
    };
};

Meta

rustc --version --verbose:

rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-pc-windows-gnu
release: 1.53.0
LLVM version: 12.0.1

(The same thing happened on all the platforms and versions I have tried.)

@LordHavelockVetinari LordHavelockVetinari added the C-bug Category: This is a bug. label Jul 7, 2021
@jonas-schievink jonas-schievink added the A-parser Area: The lexing & parsing of Rust source code to an AST label Jul 7, 2021
@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 10, 2021
@bors bors closed this as completed in 49ca3d9 Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST C-bug Category: This is a bug. 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