Skip to content

Commit 5b30586

Browse files
committed
Adding alignment to the list of cases to test for specific error message. Covers >, ^ and <.
1 parent e51e98d commit 5b30586

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

compiler/rustc_parse_format/src/lib.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ impl<'a> Iterator for Parser<'a> {
289289
}
290290
} else {
291291
if let Some(&(_, maybe)) = self.cur.peek() {
292-
if maybe == '?' {
293-
self.suggest_format();
294-
} else {
295-
self.suggest_positional_arg_instead_of_captured_arg(arg);
292+
match maybe {
293+
'?' => self.suggest_format_debug(),
294+
'<' | '^' | '>' => self.suggest_format_align(maybe),
295+
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
296296
}
297297
}
298298
}
@@ -868,10 +868,9 @@ impl<'a> Parser<'a> {
868868
found.then_some(cur)
869869
}
870870

871-
fn suggest_format(&mut self) {
871+
fn suggest_format_debug(&mut self) {
872872
if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
873873
let word = self.word();
874-
let _end = self.current_pos();
875874
let pos = self.to_span_index(pos);
876875
self.errors.insert(
877876
0,
@@ -887,6 +886,23 @@ impl<'a> Parser<'a> {
887886
}
888887
}
889888

889+
fn suggest_format_align(&mut self, alignment: char) {
890+
if let Some(pos) = self.consume_pos(alignment) {
891+
let pos = self.to_span_index(pos);
892+
self.errors.insert(
893+
0,
894+
ParseError {
895+
description: "expected format parameter to occur after `:`".to_owned(),
896+
note: Some(format!("`{}` comes after `:`.", alignment)),
897+
label: format!("expected `{}` to occur after `:`", alignment).to_owned(),
898+
span: pos.to(pos),
899+
secondary_label: None,
900+
suggestion: Suggestion::None,
901+
},
902+
);
903+
}
904+
}
905+
890906
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
891907
if let Some(end) = self.consume_pos('.') {
892908
let byte_pos = self.to_span_index(end);

tests/ui/fmt/format-string-wrong-order.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ fn main() {
1212
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
1313
format!("{?:#?}", bar);
1414
//~^ ERROR invalid format string: expected format parameter to occur after `:`
15+
format!("Hello {<5:}!", "x");
16+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
17+
format!("Hello {^5:}!", "x");
18+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
19+
format!("Hello {>5:}!", "x");
20+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
1521
}

tests/ui/fmt/format-string-wrong-order.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,29 @@ LL | format!("{?:#?}", bar);
5050
|
5151
= note: `?` comes after `:`, try `:?` instead
5252

53-
error: aborting due to 6 previous errors
53+
error: invalid format string: expected format parameter to occur after `:`
54+
--> $DIR/format-string-wrong-order.rs:15:21
55+
|
56+
LL | format!("Hello {<5:}!", "x");
57+
| ^ expected `<` to occur after `:` in format string
58+
|
59+
= note: `<` comes after `:`.
60+
61+
error: invalid format string: expected format parameter to occur after `:`
62+
--> $DIR/format-string-wrong-order.rs:17:21
63+
|
64+
LL | format!("Hello {^5:}!", "x");
65+
| ^ expected `^` to occur after `:` in format string
66+
|
67+
= note: `^` comes after `:`.
68+
69+
error: invalid format string: expected format parameter to occur after `:`
70+
--> $DIR/format-string-wrong-order.rs:19:21
71+
|
72+
LL | format!("Hello {>5:}!", "x");
73+
| ^ expected `>` to occur after `:` in format string
74+
|
75+
= note: `>` comes after `:`.
76+
77+
error: aborting due to 9 previous errors
5478

0 commit comments

Comments
 (0)