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

Incorrectly strips r# prefix from labels #6411

Closed
Raspberry1111 opened this issue Dec 2, 2024 · 5 comments · Fixed by #6425
Closed

Incorrectly strips r# prefix from labels #6411

Raspberry1111 opened this issue Dec 2, 2024 · 5 comments · Fixed by #6425
Assignees
Labels
bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors

Comments

@Raspberry1111
Copy link

Raspberry1111 commented Dec 2, 2024

Rust playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=051766b3584e787d3abab1d2f18424bb

The following code:

'r#if: {
  break 'r#if;
}

when run through either stable or nightly rustfmt will incorrectly be formatted into this:

'r#if: {
  break 'if;
}

which rustc will complain because 'if is an invalid label name:

error: invalid label name `'if`
 --> src/main.rs:5:15
  |
5 |         break 'if;
  |               ^^^

Note that this still does occur if the label name is not a keyword:

'r#a: {
  break 'r#a;
}

is formatted into

'r#a: {
  break 'a;
}

However this does not cause an error.

Versions of rustfmt (from rust-playground)
1.8.0-stable (2024-11-26 90b35a6239)
and
1.8.0-nightly (2024-11-30 7442931d49)

@ytmimi ytmimi added the bug Panic, non-idempotency, invalid code, etc. label Dec 2, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Dec 2, 2024

@Raspberry1111 thanks for the report. Has it always been possible to use r# in labels?

@Raspberry1111
Copy link
Author

Judging from this pull request: rust-lang/rust#126452 they were added in version 1.83 which is coincidentally the current stable release

Testing this with rustc +1.82 --edition 2021 test.rs gives:

error: expected `while`, `for`, `loop` or `{` after a label
 --> test.rs:4:7
  |
4 |     'r#if: {
  |       ^ expected `while`, `for`, `loop` or `{` after a label
  |
help: add `'` to close the char literal
  |
4 |     'r'#if: {
  |       +

error: expected one of `.`, `:`, `;`, `?`, `for`, `loop`, `while`, `}`, or an operator, found `#`
 --> test.rs:4:7
  |
4 |     'r#if: {
  |       ^ expected one of 9 possible tokens

error: expected `while`, `for`, `loop` or `{` after a label
  --> test.rs:10:7
   |
10 |     'r#a: {
   |       ^ expected `while`, `for`, `loop` or `{` after a label
   |
help: add `'` to close the char literal
   |
10 |     'r'#a: {
   |       +

error: expected one of `.`, `:`, `;`, `?`, `for`, `loop`, `while`, `}`, or an operator, found `#`
  --> test.rs:10:7
   |
10 |     'r#a: {
   |       ^ expected one of 9 possible tokens

error: aborting due to 4 previous errors

Running rustc +1.83 --edition 2021 test.rs produces no errors

@ytmimi
Copy link
Contributor

ytmimi commented Dec 2, 2024

Thanks for the extra info. Yeah, I can reproduce this using the latest commit 9f8fcc2, and running rustfmt from source with cargo run --bin rustfmt -- --edition=2021:

input:

fn broken() {
    'r#if: {
        break 'r#if;
    }
}

fn works() {
    'r#a: {
        break 'r#a;
    }
}

output:

fn broken() {
    'r#if: {
        break 'if;
    }
}

fn works() {
    'r#a: {
        break 'a;
    }
}

Looks like break and continue formatting need to be updated to use the span of the Label, and not just the identifier like rust-lang/rust#126452:

rustfmt/src/expr.rs

Lines 201 to 213 in 9f8fcc2

ast::ExprKind::Continue(ref opt_label) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
None => String::new(),
};
Ok(format!("continue{id_str}"))
}
ast::ExprKind::Break(ref opt_label, ref opt_expr) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
None => String::new(),
};

@ytmimi ytmimi added the good first issue Issues up for grabs, also good candidates for new rustfmt contributors label Dec 2, 2024
@sobatha
Copy link
Contributor

sobatha commented Dec 21, 2024

@rustbot claim
I haven't contribute rustfmt yet but I'd like to work on this issue!

@ytmimi
Copy link
Contributor

ytmimi commented Dec 21, 2024

@sobatha that's great! Let me know if you need any help getting started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants