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

format! error messages point to wrong characters when using macro-generated literals #84651

Closed
Nemo157 opened this issue Apr 28, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Apr 28, 2021

I tried this code:

fn main() {
    format!(concat!("{", "  f"));
}

I expected to see this happen:

error: invalid format string: expected `'}'`, found `'f'`
 --> src/main.rs:2:17
  |
2 |     format!(concat!("{", "  f"));
  |                      -      ^ expected `}` in format string
  |                      |
  |                      because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

Instead, this happened:

error: invalid format string: expected `'}'`, found `'f'`
 --> src/main.rs:2:17
  |
2 |     format!(concat!("{", "  f"));
  |              -  ^ expected `}` in format string
  |              |
  |              because of this opening brace
  |
  = note: if you intended to print `{`, you can escape it using `{{`

Another more complicated example, is using include_str! (with a self-include to make playground testing easier):

fn main() {
    format!(include_str!("main.rs"));
}

Similar to #81070 it'd be very nice if this was able to point to the location in the included file, but if it can't it should probably have a simpler error message like "in external string included here".

Meta

Tested on stable 1.51.0 and 1.53.0-nightly (2021-04-27 727d101561f9b1e81c62)

@Nemo157 Nemo157 added C-bug Category: This is a bug. A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Apr 28, 2021
@JohnTitor JohnTitor added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 12, 2021
@Nemo157
Copy link
Member Author

Nemo157 commented Feb 13, 2023

Current output:

error: invalid format string: expected `'}'`, found `'f'`
 --> src/main.rs:2:13
  |
2 |     format!(concat!("{", "  f"));
  |             ^^^^^^^^^^^^^^^^^^^ expected `}` in format string
  |
  = note: if you intended to print `{`, you can escape it using `{{`
  = note: this error originates in the macro `concat` (in Nightly builds, run with -Z macro-backtrace for more info)
2 |     format!(include_str!("main.rs"));
  |             ^^^^^^^^^^^^^^^^^^^^^^^ expected `}` in format string

Not perfect as it could be possible to track the spans back to the actual source instead of just highlighting the whole macro, but seems good enough to consider this bug fixed.

@Nemo157 Nemo157 closed this as completed Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants