Skip to content

Commit deb504b

Browse files
authored
Rollup merge of #119712 - madsravn:parsing-errors, r=estebank
Adding alignment to the cases to test for specific error messages. Adding alignment to the list of cases to test for specific error message. Covers `>`, `^` and `<`. Pinging people who chimed in last time ( #106805 ): ``@estebank`` , ``@compiler-errors`` and ``@Nilstrieb``
2 parents 50982bd + 506c066 commit deb504b

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

compiler/rustc_parse_format/src/lib.rs

+22-6
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: None,
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

+6
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

+19-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,23 @@ 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+
error: invalid format string: expected format parameter to occur after `:`
60+
--> $DIR/format-string-wrong-order.rs:17:21
61+
|
62+
LL | format!("Hello {^5:}!", "x");
63+
| ^ expected `^` to occur after `:` in format string
64+
65+
error: invalid format string: expected format parameter to occur after `:`
66+
--> $DIR/format-string-wrong-order.rs:19:21
67+
|
68+
LL | format!("Hello {>5:}!", "x");
69+
| ^ expected `>` to occur after `:` in format string
70+
71+
error: aborting due to 9 previous errors
5472

0 commit comments

Comments
 (0)