diff options
| author | Nilotpal Gupta <nilotpalgupta0701@gmail.com> | 2025-08-19 11:10:32 +0530 |
|---|---|---|
| committer | Nilotpal Gupta <nilotpalgupta0701@gmail.com> | 2025-08-27 20:45:41 +0530 |
| commit | fdbaaac245673d8dbf7bcc21ebedc833693434f6 (patch) | |
| tree | f6371ea809dd0ce3eac8f1169fb3ec184d7f5d7a | |
| parent | 269d5b56bcfdf2be82213e72ef9a2e4c592a8c6b (diff) | |
| download | rust-fdbaaac245673d8dbf7bcc21ebedc833693434f6.tar.gz rust-fdbaaac245673d8dbf7bcc21ebedc833693434f6.zip | |
Fix format string grammar in docs and improve alignment error message
| -rw-r--r-- | compiler/rustc_parse_format/src/lib.rs | 4 | ||||
| -rw-r--r-- | library/alloc/src/fmt.rs | 2 | ||||
| -rw-r--r-- | tests/ui/fmt/format-string-wrong-order.rs | 8 | ||||
| -rw-r--r-- | tests/ui/fmt/format-string-wrong-order.stderr | 14 |
4 files changed, 19 insertions, 9 deletions
diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 8e4da7923fc..5cda0b813d2 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -858,7 +858,9 @@ impl<'input> Parser<'input> { self.errors.insert( 0, ParseError { - description: "expected format parameter to occur after `:`".to_owned(), + description: + "expected alignment specifier after `:` in format string; example: `{:>?}`" + .to_owned(), note: None, label: format!("expected `{}` to occur after `:`", alignment), span: range, diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index d0ba9c39886..82eaf7d8724 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -354,7 +354,7 @@ //! sign := '+' | '-' //! width := count //! precision := count | '*' -//! type := '?' | 'x?' | 'X?' | identifier +//! type := '?' | 'x?' | 'X?' | 'o' | 'x' | 'X' | 'p' | 'b' | 'e' | 'E' //! count := parameter | integer //! parameter := argument '$' //! ``` diff --git a/tests/ui/fmt/format-string-wrong-order.rs b/tests/ui/fmt/format-string-wrong-order.rs index 891279b97e4..4d4e04ecd04 100644 --- a/tests/ui/fmt/format-string-wrong-order.rs +++ b/tests/ui/fmt/format-string-wrong-order.rs @@ -13,9 +13,11 @@ fn main() { format!("{?:#?}", bar); //~^ ERROR invalid format string: expected format parameter to occur after `:` format!("Hello {<5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` format!("Hello {^5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` format!("Hello {>5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` + println!("{0:#X>18}", 12345); + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` } diff --git a/tests/ui/fmt/format-string-wrong-order.stderr b/tests/ui/fmt/format-string-wrong-order.stderr index 7f017511761..441ae6d2e50 100644 --- a/tests/ui/fmt/format-string-wrong-order.stderr +++ b/tests/ui/fmt/format-string-wrong-order.stderr @@ -50,23 +50,29 @@ LL | format!("{?:#?}", bar); | = note: `?` comes after `:`, try `:?` instead -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:15:21 | LL | format!("Hello {<5:}!", "x"); | ^ expected `<` to occur after `:` in format string -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:17:21 | LL | format!("Hello {^5:}!", "x"); | ^ expected `^` to occur after `:` in format string -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:19:21 | LL | format!("Hello {>5:}!", "x"); | ^ expected `>` to occur after `:` in format string -error: aborting due to 9 previous errors +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` + --> $DIR/format-string-wrong-order.rs:21:20 + | +LL | println!("{0:#X>18}", 12345); + | ^ expected `>` to occur after `:` in format string + +error: aborting due to 10 previous errors |
