Skip to content

Parse formatting strings existing in other languages and explain we don't support them #145718

@estebank

Description

@estebank

Rust has a similar formatting string syntax to other languages', like Python's, but with fewer supported formatters. We already emit a pretty comprehensive output when encountering bad textual formatters:

error: unknown format trait `g`
 --> src/main.rs:3:14
  |
3 | println!("{x:g}");
  |              ^
  |
  = note: the only appropriate formatting traits are:
          - ``, which uses the `Display` trait
          - `?`, which uses the `Debug` trait
          - `e`, which uses the `LowerExp` trait
          - `E`, which uses the `UpperExp` trait
          - `o`, which uses the `Octal` trait
          - `p`, which uses the `Pointer` trait
          - `b`, which uses the `Binary` trait
          - `x`, which uses the `LowerHex` trait
          - `X`, which uses the `UpperHex` trait
error: unknown format trait `_b`
 --> src/main.rs:3:16
  |
3 | println!("{x:09_b}");
  |                ^^
  |
  = note: the only appropriate formatting traits are:
          - ``, which uses the `Display` trait
          - `?`, which uses the `Debug` trait
          - `e`, which uses the `LowerExp` trait
          - `E`, which uses the `UpperExp` trait
          - `o`, which uses the `Octal` trait
          - `p`, which uses the `Pointer` trait
          - `b`, which uses the `Binary` trait
          - `x`, which uses the `LowerHex` trait
          - `X`, which uses the `UpperHex` trait

but when using symbols for the formatter we emit simpler diagnostics without much guidance:

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

The parser should recognize these, tell people that Rust isn't Python :) and link to the documentation. If we can identify what the corresponding intended operation was, mention it. For example println!("{x=") likely meant to be dbg!("{x}"), _b should have been b (but we don't have support to customize its separator). This way we don't have to show the full list of valid formatters every time, and we explain more than "we didn't expect this here".

CC #144023

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.P-lowLow priorityT-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