-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Description
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