Skip to content

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

@Nemo157

Description

@Nemo157

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.D-papercutDiagnostics: An error or lint that needs small tweaks.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