Skip to content

Parse error on labeled loop after break #86948

Closed
@LordHavelockVetinari

Description

@LordHavelockVetinari

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.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