Skip to content

Commit 873ca5f

Browse files
committed
Just suggest positional arg and adjust issue0139104 ui test
Signed-off-by: xizheyin <[email protected]>
1 parent 64867c6 commit 873ca5f

File tree

3 files changed

+134
-95
lines changed

3 files changed

+134
-95
lines changed

compiler/rustc_parse_format/src/lib.rs

+50-66
Original file line numberDiff line numberDiff line change
@@ -918,73 +918,57 @@ impl<'a> Parser<'a> {
918918
}
919919

920920
fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
921-
// If the argument is an identifier, it may be a field access.
922-
if arg.is_identifier() {
923-
if let Some(end) = self.consume_pos('.') {
924-
let byte_pos = self.to_span_index(end);
925-
let start = InnerOffset(byte_pos.0 + 1);
926-
let field = self.argument(start);
927-
// We can only parse simple `foo.bar` field access or `foo.0` tuple index access, any
928-
// deeper nesting, or another type of expression, like method calls, are not supported
929-
if !self.consume('}') {
930-
return;
931-
}
932-
if let ArgumentNamed(_) = arg.position {
933-
match field.position {
934-
ArgumentNamed(_) => {
935-
self.errors.insert(
936-
0,
937-
ParseError {
938-
description: "field access isn't supported".to_string(),
939-
note: None,
940-
label: "not supported".to_string(),
941-
span: InnerSpan::new(
942-
arg.position_span.start,
943-
field.position_span.end,
944-
),
945-
secondary_label: None,
946-
suggestion: Suggestion::UsePositional,
947-
},
948-
);
949-
}
950-
ArgumentIs(_) => {
951-
self.errors.insert(
952-
0,
953-
ParseError {
954-
description: "tuple index access isn't supported".to_string(),
955-
note: None,
956-
label: "not supported".to_string(),
957-
span: InnerSpan::new(
958-
arg.position_span.start,
959-
field.position_span.end,
960-
),
961-
secondary_label: None,
962-
suggestion: Suggestion::UsePositional,
963-
},
964-
);
965-
}
966-
_ => {}
967-
};
968-
}
969-
}
970-
} else if matches!(arg.position, ArgumentNamed(_) | ArgumentIs(_)) {
971-
let arg_name = match arg.position {
972-
ArgumentNamed(arg_name) => &format!("`{arg_name}`"),
973-
ArgumentIs(arg_index) => &format!("at index `{arg_index}`"),
974-
_ => unreachable!(),
975-
};
921+
// If the argument is not an identifier, it is not a field access.
922+
if !arg.is_identifier() {
923+
return;
924+
}
976925

977-
self.errors.insert(
978-
0,
979-
ParseError {
980-
description: format!("invalid format string for argument {}", arg_name),
981-
note: None,
982-
label: format!("invalid format specifier for this argument"),
983-
span: InnerSpan::new(arg.position_span.start, arg.position_span.end),
984-
secondary_label: None,
985-
suggestion: Suggestion::None,
986-
},
987-
);
926+
if let Some(end) = self.consume_pos('.') {
927+
let byte_pos = self.to_span_index(end);
928+
let start = InnerOffset(byte_pos.0 + 1);
929+
let field = self.argument(start);
930+
// We can only parse simple `foo.bar` field access or `foo.0` tuple index access, any
931+
// deeper nesting, or another type of expression, like method calls, are not supported
932+
if !self.consume('}') {
933+
return;
934+
}
935+
if let ArgumentNamed(_) = arg.position {
936+
match field.position {
937+
ArgumentNamed(_) => {
938+
self.errors.insert(
939+
0,
940+
ParseError {
941+
description: "field access isn't supported".to_string(),
942+
note: None,
943+
label: "not supported".to_string(),
944+
span: InnerSpan::new(
945+
arg.position_span.start,
946+
field.position_span.end,
947+
),
948+
secondary_label: None,
949+
suggestion: Suggestion::UsePositional,
950+
},
951+
);
952+
}
953+
ArgumentIs(_) => {
954+
self.errors.insert(
955+
0,
956+
ParseError {
957+
description: "tuple index access isn't supported".to_string(),
958+
note: None,
959+
label: "not supported".to_string(),
960+
span: InnerSpan::new(
961+
arg.position_span.start,
962+
field.position_span.end,
963+
),
964+
secondary_label: None,
965+
suggestion: Suggestion::UsePositional,
966+
},
967+
);
968+
}
969+
_ => {}
970+
};
971+
}
988972
}
989973
}
990974

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
fn main() {
2-
println!("{foo:_1.4}", foo = 3.14); //~ ERROR invalid format string: invalid format string for argument `foo`
3-
println!("{foo:1.4_1.4}", foo = 3.14); //~ ERROR invalid format string: invalid format string for argument `foo`
4-
println!("xxx{0:_1.4}", 1.11); //~ ERROR invalid format string: invalid format string for argument at index `0`
5-
println!("{foo:_1.4", foo = 3.14); //~ ERROR invalid format string: invalid format string for argument `foo`
6-
println!("xxx{0:_1.4", 1.11); //~ ERROR invalid format string: invalid format string for argument at index `0`
7-
println!("xxx{ 0", 1.11); //~ ERROR invalid format string: expected `}`, found `0`
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 `.`
813
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,92 @@
1-
error: invalid format string: invalid format string for argument `foo`
2-
--> $DIR/invalid-parse-format-issue-139104.rs:2:16
1+
error: invalid format string: expected `}`, found `.`
2+
--> $DIR/invalid-parse-format-issue-139104.rs:2:22
33
|
44
LL | println!("{foo:_1.4}", foo = 3.14);
5-
| ^^^ invalid format specifier for this argument in format string
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 `{{`
610

7-
error: invalid format string: invalid format string for argument `foo`
8-
--> $DIR/invalid-parse-format-issue-139104.rs:3:16
11+
error: invalid format string: expected `}`, found `.`
12+
--> $DIR/invalid-parse-format-issue-139104.rs:3:20
913
|
10-
LL | println!("{foo:1.4_1.4}", foo = 3.14);
11-
| ^^^ invalid format specifier for this argument in format string
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 `{{`
1220

13-
error: invalid format string: invalid format string for argument at index `0`
21+
error: invalid format string: expected `}`, found `.`
1422
--> $DIR/invalid-parse-format-issue-139104.rs:4:19
1523
|
16-
LL | println!("xxx{0:_1.4}", 1.11);
17-
| ^ invalid format specifier for this argument in format string
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 `{{`
1830

19-
error: invalid format string: invalid format string for argument `foo`
20-
--> $DIR/invalid-parse-format-issue-139104.rs:5:16
31+
error: invalid format string: expected `}`, found `.`
32+
--> $DIR/invalid-parse-format-issue-139104.rs:6:22
2133
|
2234
LL | println!("{foo:_1.4", foo = 3.14);
23-
| ^^^ invalid format specifier for this argument in format string
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 `{{`
2450

25-
error: invalid format string: invalid format string for argument at index `0`
26-
--> $DIR/invalid-parse-format-issue-139104.rs:6:19
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
2758
|
28-
LL | println!("xxx{0:_1.4", 1.11);
29-
| ^ invalid format specifier for this argument in format string
59+
= note: if you intended to print `{`, you can escape it using `{{`
3060

3161
error: invalid format string: expected `}`, found `0`
32-
--> $DIR/invalid-parse-format-issue-139104.rs:7:21
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
3383
|
34-
LL | println!("xxx{ 0", 1.11);
35-
| - ^ expected `}` in format string
36-
| |
37-
| because of this opening brace
84+
LL | println!("{0:1.4_1.4}", 3.14);
85+
| - ^ expected `}` in format string
86+
| |
87+
| because of this opening brace
3888
|
3989
= note: if you intended to print `{`, you can escape it using `{{`
4090

41-
error: aborting due to 6 previous errors
91+
error: aborting due to 9 previous errors
4292

0 commit comments

Comments
 (0)