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

Raw identifiers don't work in formatting strings #115466

Closed
ilyvion opened this issue Sep 2, 2023 · 2 comments · Fixed by #115611
Closed

Raw identifiers don't work in formatting strings #115466

ilyvion opened this issue Sep 2, 2023 · 2 comments · Fixed by #115611
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@ilyvion
Copy link

ilyvion commented Sep 2, 2023

I tried this code:

fn main() {
    let r#type = "foobar";
    println!("It is {r#type}");
}

I expected to see this happen: The program compiles and prints "It is foobar" to stdout.

Instead, this happened: The compiler gives an error while compiling: "error: invalid format string"

Meta

rustc --version --verbose:
(Only tested on playground, but on all three channels, reported as:)

1.74.0-nightly 2023-09-01 35e416303e6591a71ef6
1.73.0-beta.4 2023-09-01 9f37cd4f79d289492cca
1.72.0

It's not a compiler crash, so I'm going to leave out the backtrace and instead just show what the compiler outputs when you try to compile the above code:

error: invalid format string: expected `'}'`, found `'#'`
 --> src/main.rs:3:23
  |
3 |     println!("It is {r#type}");
  |                     - ^ expected `'}'` in format string
  |                     |
  |                     because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

I'm not convinced this is solvable/possible (I can imagine allowing this could potentially interfere with other formatting options (although they all follow a :, right? So maybe not.), but I also couldn't find any existing issue about it, so I figured I'd open one to have it clarified.

@ilyvion ilyvion added the C-bug Category: This is a bug. label Sep 2, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 2, 2023
@Nilstrieb Nilstrieb added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 2, 2023
@lukas-code
Copy link
Contributor

Identifiers in format strings are always "raw" and it will compile if you remove the r#:

fn main() {
    let r#type = "foobar";
    println!("It is {type}");
}

This is documented here:

But the error should probably have a better diagnostic and a suggestion to remove the r#.

@ilyvion
Copy link
Author

ilyvion commented Sep 3, 2023

Aha. That's good. Thanks for letting me know; was not aware of this.

@bors bors closed this as completed in 0900712 Sep 17, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 17, 2023
Rollup merge of rust-lang#115611 - lukas-code:format!("{r#ident}"), r=compiler-errors

add diagnostic for raw identifiers in format string

Format strings don't support raw identifiers (e.g. `format!("{r#type}")`), but they do support keywords in the format string directly (e.g. `format!("{type}")`). This PR improves the error output when attempting to use a raw identifier in a format string and adds a machine-applicable suggestion to remove the `r#`.

fixes rust-lang#115466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants