Skip to content

Commit 2fdb355

Browse files
authored
Rollup merge of #106805 - madsravn:master, r=compiler-errors
Suggest `{var:?}` when finding `{?:var}` in inline format strings Link to issue: #106572 This is my first PR to this project, so hopefully I can get some good pointers with me from the first PR. Currently my idea was to test out whether or not this is the correct solution to this issue and then hopefully expand upon the idea to not only work for Debug formatting but for all of them. If this is a valid solution, I will create a new issue to give a better error message to a broader range of wrong-order formatting.
2 parents 6b94f4d + f922c83 commit 2fdb355

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

compiler/rustc_parse_format/src/lib.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,13 @@ impl<'a> Iterator for Parser<'a> {
273273
);
274274
}
275275
} else {
276-
self.suggest_positional_arg_instead_of_captured_arg(arg);
276+
if let Some(&(_, maybe)) = self.cur.peek() {
277+
if maybe == '?' {
278+
self.suggest_format();
279+
} else {
280+
self.suggest_positional_arg_instead_of_captured_arg(arg);
281+
}
282+
}
277283
}
278284
Some(NextArgument(Box::new(arg)))
279285
}
@@ -832,6 +838,27 @@ impl<'a> Parser<'a> {
832838
if found { Some(cur) } else { None }
833839
}
834840

841+
fn suggest_format(&mut self) {
842+
if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
843+
let word = self.word();
844+
let _end = self.current_pos();
845+
let pos = self.to_span_index(pos);
846+
self.errors.insert(
847+
0,
848+
ParseError {
849+
description: "expected format parameter to occur after `:`".to_owned(),
850+
note: Some(
851+
format!("`?` comes after `:`, try `{}:{}` instead", word, "?").to_owned(),
852+
),
853+
label: "expected `?` to occur after `:`".to_owned(),
854+
span: pos.to(pos),
855+
secondary_label: None,
856+
should_be_replaced_with_positional_argument: false,
857+
},
858+
);
859+
}
860+
}
861+
835862
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
836863
if let Some(end) = self.consume_pos('.') {
837864
let byte_pos = self.to_span_index(end);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fn main() {
2+
let bar = 3;
3+
format!("{?:}", bar);
4+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
5+
format!("{?:bar}");
6+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
7+
format!("{?:?}", bar);
8+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
9+
format!("{??}", bar);
10+
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
11+
format!("{?;bar}");
12+
//~^ ERROR invalid format string: expected `'}'`, found `'?'`
13+
format!("{?:#?}", bar);
14+
//~^ ERROR invalid format string: expected format parameter to occur after `:`
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: invalid format string: expected format parameter to occur after `:`
2+
--> $DIR/format-string-wrong-order.rs:3:15
3+
|
4+
LL | format!("{?:}", bar);
5+
| ^ expected `?` to occur after `:` in format string
6+
|
7+
= note: `?` comes after `:`, try `:?` instead
8+
9+
error: invalid format string: expected format parameter to occur after `:`
10+
--> $DIR/format-string-wrong-order.rs:5:15
11+
|
12+
LL | format!("{?:bar}");
13+
| ^ expected `?` to occur after `:` in format string
14+
|
15+
= note: `?` comes after `:`, try `bar:?` instead
16+
17+
error: invalid format string: expected format parameter to occur after `:`
18+
--> $DIR/format-string-wrong-order.rs:7:15
19+
|
20+
LL | format!("{?:?}", bar);
21+
| ^ expected `?` to occur after `:` in format string
22+
|
23+
= note: `?` comes after `:`, try `:?` instead
24+
25+
error: invalid format string: expected `'}'`, found `'?'`
26+
--> $DIR/format-string-wrong-order.rs:9:15
27+
|
28+
LL | format!("{??}", bar);
29+
| -^ expected `}` in format string
30+
| |
31+
| because of this opening brace
32+
|
33+
= note: if you intended to print `{`, you can escape it using `{{`
34+
35+
error: invalid format string: expected `'}'`, found `'?'`
36+
--> $DIR/format-string-wrong-order.rs:11:15
37+
|
38+
LL | format!("{?;bar}");
39+
| -^ expected `}` in format string
40+
| |
41+
| because of this opening brace
42+
|
43+
= note: if you intended to print `{`, you can escape it using `{{`
44+
45+
error: invalid format string: expected format parameter to occur after `:`
46+
--> $DIR/format-string-wrong-order.rs:13:15
47+
|
48+
LL | format!("{?:#?}", bar);
49+
| ^ expected `?` to occur after `:` in format string
50+
|
51+
= note: `?` comes after `:`, try `:?` instead
52+
53+
error: aborting due to 6 previous errors
54+

0 commit comments

Comments
 (0)