Skip to content

Commit 4841a77

Browse files
authored
Unrolled build for rust-lang#140286
Rollup merge of rust-lang#140286 - xizheyin:issue-139104, r=lcnr Check if format argument is identifier to avoid error err-emit Fixes rust-lang#139104 When `argument` is not an identifier, it should not be considered a field access. I checked this and if not emit an invalid format string error. I think we could do with a little finer error handling, I'll open an issue to track this down later. The first commit submits the ui test, the second commits the code and the changes to the test output. r? compiler
2 parents 1bea580 + 873ca5f commit 4841a77

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

compiler/rustc_parse_format/src/lib.rs

+29
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ pub struct Argument<'a> {
100100
pub format: FormatSpec<'a>,
101101
}
102102

103+
impl<'a> Argument<'a> {
104+
pub fn is_identifier(&self) -> bool {
105+
matches!(self.position, Position::ArgumentNamed(_))
106+
&& matches!(
107+
self.format,
108+
FormatSpec {
109+
fill: None,
110+
fill_span: None,
111+
align: AlignUnknown,
112+
sign: None,
113+
alternate: false,
114+
zero_pad: false,
115+
debug_hex: None,
116+
precision: CountImplied,
117+
precision_span: None,
118+
width: CountImplied,
119+
width_span: None,
120+
ty: "",
121+
ty_span: None,
122+
},
123+
)
124+
}
125+
}
126+
103127
/// Specification for the formatting of an argument in the format string.
104128
#[derive(Copy, Clone, Debug, PartialEq)]
105129
pub struct FormatSpec<'a> {
@@ -894,6 +918,11 @@ impl<'a> Parser<'a> {
894918
}
895919

896920
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
921+
// If the argument is not an identifier, it is not a field access.
922+
if !arg.is_identifier() {
923+
return;
924+
}
925+
897926
if let Some(end) = self.consume_pos('.') {
898927
let byte_pos = self.to_span_index(end);
899928
let start = InnerOffset(byte_pos.0 + 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
println!("{foo:_1.4}", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
3+
println!("{0:_1.4}", 1.11); //~ ERROR invalid format string: expected `}`, found `.`
4+
println!("{:_1.4}", 3.14); //~ ERROR invalid format string: expected `}`, found `.`
5+
6+
println!("{foo:_1.4", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
7+
println!("{0:_1.4", 1.11); //~ ERROR invalid format string: expected `}`, found `.`
8+
println!("{:_1.4", 3.14); //~ ERROR invalid format string: expected `}`, found `.`
9+
10+
println!("{ 0", 1.11); //~ ERROR invalid format string: expected `}`, found `0`
11+
println!("{foo:1.4_1.4}", foo = 3.14); //~ ERROR invalid format string: expected `}`, found `.`
12+
println!("{0:1.4_1.4}", 3.14); //~ ERROR invalid format string: expected `}`, found `.`
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
error: invalid format string: expected `}`, found `.`
2+
--> $DIR/invalid-parse-format-issue-139104.rs:2:22
3+
|
4+
LL | println!("{foo:_1.4}", foo = 3.14);
5+
| - ^ expected `}` in format string
6+
| |
7+
| because of this opening brace
8+
|
9+
= note: if you intended to print `{`, you can escape it using `{{`
10+
11+
error: invalid format string: expected `}`, found `.`
12+
--> $DIR/invalid-parse-format-issue-139104.rs:3:20
13+
|
14+
LL | println!("{0:_1.4}", 1.11);
15+
| - ^ expected `}` in format string
16+
| |
17+
| because of this opening brace
18+
|
19+
= note: if you intended to print `{`, you can escape it using `{{`
20+
21+
error: invalid format string: expected `}`, found `.`
22+
--> $DIR/invalid-parse-format-issue-139104.rs:4:19
23+
|
24+
LL | println!("{:_1.4}", 3.14);
25+
| - ^ expected `}` in format string
26+
| |
27+
| because of this opening brace
28+
|
29+
= note: if you intended to print `{`, you can escape it using `{{`
30+
31+
error: invalid format string: expected `}`, found `.`
32+
--> $DIR/invalid-parse-format-issue-139104.rs:6:22
33+
|
34+
LL | println!("{foo:_1.4", foo = 3.14);
35+
| - ^ expected `}` in format string
36+
| |
37+
| because of this opening brace
38+
|
39+
= note: if you intended to print `{`, you can escape it using `{{`
40+
41+
error: invalid format string: expected `}`, found `.`
42+
--> $DIR/invalid-parse-format-issue-139104.rs:7:20
43+
|
44+
LL | println!("{0:_1.4", 1.11);
45+
| - ^ expected `}` in format string
46+
| |
47+
| because of this opening brace
48+
|
49+
= note: if you intended to print `{`, you can escape it using `{{`
50+
51+
error: invalid format string: expected `}`, found `.`
52+
--> $DIR/invalid-parse-format-issue-139104.rs:8:19
53+
|
54+
LL | println!("{:_1.4", 3.14);
55+
| - ^ expected `}` in format string
56+
| |
57+
| because of this opening brace
58+
|
59+
= note: if you intended to print `{`, you can escape it using `{{`
60+
61+
error: invalid format string: expected `}`, found `0`
62+
--> $DIR/invalid-parse-format-issue-139104.rs:10:18
63+
|
64+
LL | println!("{ 0", 1.11);
65+
| - ^ expected `}` in format string
66+
| |
67+
| because of this opening brace
68+
|
69+
= note: if you intended to print `{`, you can escape it using `{{`
70+
71+
error: invalid format string: expected `}`, found `.`
72+
--> $DIR/invalid-parse-format-issue-139104.rs:11:25
73+
|
74+
LL | println!("{foo:1.4_1.4}", foo = 3.14);
75+
| - ^ expected `}` in format string
76+
| |
77+
| because of this opening brace
78+
|
79+
= note: if you intended to print `{`, you can escape it using `{{`
80+
81+
error: invalid format string: expected `}`, found `.`
82+
--> $DIR/invalid-parse-format-issue-139104.rs:12:23
83+
|
84+
LL | println!("{0:1.4_1.4}", 3.14);
85+
| - ^ expected `}` in format string
86+
| |
87+
| because of this opening brace
88+
|
89+
= note: if you intended to print `{`, you can escape it using `{{`
90+
91+
error: aborting due to 9 previous errors
92+

0 commit comments

Comments
 (0)