mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Rollup merge of #145592 - nilotpal-n7:fix-format-alignment, r=lcnr
Fix format string grammar in docs and improve alignment error message for #144023 This PR improves error messages and documentation for format strings involving alignment and formatting traits. Highlights: - Clearer error messages for invalid alignment specifiers (e.g., `{0:#X>18}`), showing the expected `<`, `^`, or `>` and a working example: println!("{0:>#18X}", value); - Updated UI test `format-alignment-hash.rs` to reflect the improved error output. - Documentation clarification: ensures examples correctly show how width, alignment, and traits like `x`, `X`, `#` combine. Motivation: Previously, using `#` with alignment and width produced confusing errors. This PR guides users on the correct syntax and provides actionable examples. Testing: - Built the compiler (`./x build`) - Blessed and ran UI tests (`./x. test src/test/ui/fmt/format-alignment-hash.rs --bless`) - Verified full test suite passes (`./x test`) Issue: rust-lang/rust#144023
This commit is contained in:
@@ -858,7 +858,9 @@ fn suggest_format_align(&mut self, alignment: char) {
|
||||
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,
|
||||
|
||||
@@ -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 '$'
|
||||
//! ```
|
||||
|
||||
@@ -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: `{:>?}`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user