Skip to content

Incorrect suggestion with labelled breaks and blocks on nightly #87812

Closed
@0x182d4454fb211940

Description

@0x182d4454fb211940

When using blocks with labelled break statements, a warning can appear to avoid confusion. The following code:

let x = 'l: loop {
    break 'l { 3 };
};

produces:

warning: this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression
  --> src/main.rs:71:9
   |
71 |         break 'l { 3 };
   |         ^^^^^^^^^^^^^^
   |
   = note: `#[warn(break_with_label_and_loop)]` on by default
help: wrap this expression in parentheses
   |
71 |         break 'l ({ 3 });
   |                  ^     ^

However when introducing a block via a macro:

macro_rules! foo {
    ( $f:block ) => {
        '_l: loop {
            break '_l $f;
        }
    };
}

fn main() {
    let x = foo!({ 3 });
}

The compiler warns:

warning: this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression
  --> x.rs:4:13
   |
4  |             break '_l $f;
   |             ^^^^^^^^^^^^^
...
10 |     let x = foo!({ 3 });
   |             ----------- in this macro invocation
   |
   = note: `#[warn(break_with_label_and_loop)]` on by default
   = note: this warning originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap this expression in parentheses
   |
4  |             break '_l $f(;)
   |                         ^ ^

Which suggests the incorrect code $f(;), whereas it should suggest ($f);. This is only an issue on nightly, as the warning does not exist on stable.

rustc --version --verbose:

rustc 1.56.0-nightly (2f07ae408 2021-08-05)
binary: rustc
commit-hash: 2f07ae408fce782bf1058e3de808f1b6f9ab60a4
commit-date: 2021-08-05
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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