Skip to content

Commit 35a64f8

Browse files
committed
Auto merge of #57140 - estebank:str-err, r=varkor
Tweaks to format string diagnostics Add label spans and fix incorrect spans. Fix #55155, fix #55350.
2 parents 43d26b1 + 5e952e3 commit 35a64f8

File tree

7 files changed

+61
-37
lines changed

7 files changed

+61
-37
lines changed

src/libfmt_macros/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub struct Parser<'a> {
144144
/// `Some(raw count)` when the string is "raw", used to position spans correctly
145145
style: Option<usize>,
146146
/// Start and end byte offset of every successfully parsed argument
147-
pub arg_places: Vec<(usize, usize)>,
147+
pub arg_places: Vec<(SpanIndex, SpanIndex)>,
148148
/// Characters that need to be shifted
149149
skips: Vec<usize>,
150150
/// Span offset of the last opening brace seen, used for error reporting
@@ -154,7 +154,7 @@ pub struct Parser<'a> {
154154
}
155155

156156
#[derive(Clone, Copy, Debug)]
157-
pub struct SpanIndex(usize);
157+
pub struct SpanIndex(pub usize);
158158

159159
impl SpanIndex {
160160
pub fn unwrap(self) -> usize {
@@ -166,7 +166,6 @@ impl<'a> Iterator for Parser<'a> {
166166
type Item = Piece<'a>;
167167

168168
fn next(&mut self) -> Option<Piece<'a>> {
169-
let raw = self.raw();
170169
if let Some(&(pos, c)) = self.cur.peek() {
171170
match c {
172171
'{' => {
@@ -180,7 +179,7 @@ impl<'a> Iterator for Parser<'a> {
180179
} else {
181180
let arg = self.argument();
182181
if let Some(arg_pos) = self.must_consume('}').map(|end| {
183-
(pos + raw + 1, end + raw + 2)
182+
(self.to_span_index(pos), self.to_span_index(end + 1))
184183
}) {
185184
self.arg_places.push(arg_pos);
186185
}

src/libsyntax_ext/format.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,9 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
860860
}
861861

862862
let arg_spans = parser.arg_places.iter()
863-
.map(|&(start, end)| fmt.span.from_inner_byte_pos(start, end))
863+
.map(|&(parse::SpanIndex(start), parse::SpanIndex(end))| {
864+
fmt.span.from_inner_byte_pos(start, end)
865+
})
864866
.collect();
865867

866868
let mut cx = Context {
@@ -954,13 +956,18 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
954956
let mut diag = {
955957
if errs_len == 1 {
956958
let (sp, msg) = errs.into_iter().next().unwrap();
957-
cx.ecx.struct_span_err(sp, msg)
959+
let mut diag = cx.ecx.struct_span_err(sp, msg);
960+
diag.span_label(sp, msg);
961+
diag
958962
} else {
959963
let mut diag = cx.ecx.struct_span_err(
960964
errs.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
961965
"multiple unused formatting arguments",
962966
);
963967
diag.span_label(cx.fmtsp, "multiple missing formatting specifiers");
968+
for (sp, msg) in errs {
969+
diag.span_label(sp, msg);
970+
}
964971
diag
965972
}
966973
};

src/test/ui/fmt/format-string-error-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ raw { \n
6767
asdf}
6868
", asdf=1);
6969
//~^^ ERROR invalid format string
70+
println!("\t{}");
71+
//~^ ERROR 1 positional argument in format string
7072
}

src/test/ui/fmt/format-string-error-2.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,11 @@ LL | asdf}
133133
|
134134
= note: if you intended to print `{`, you can escape it using `{{`
135135

136-
error: aborting due to 13 previous errors
136+
error: 1 positional argument in format string, but no arguments were given
137+
--> $DIR/format-string-error-2.rs:70:17
138+
|
139+
LL | println!("/t{}");
140+
| ^^
141+
142+
error: aborting due to 14 previous errors
137143

src/test/ui/if/ifmt-bad-arg.stderr

+14-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error: argument never used
1616
--> $DIR/ifmt-bad-arg.rs:9:20
1717
|
1818
LL | format!("{1}", 1);
19-
| ----- ^
19+
| ----- ^ argument never used
2020
| |
2121
| formatting specifier missing
2222

@@ -80,56 +80,58 @@ error: multiple unused formatting arguments
8080
--> $DIR/ifmt-bad-arg.rs:32:17
8181
|
8282
LL | format!("", 1, 2); //~ ERROR: multiple unused formatting arguments
83-
| -- ^ ^
84-
| |
83+
| -- ^ ^ argument never used
84+
| | |
85+
| | argument never used
8586
| multiple missing formatting specifiers
8687

8788
error: argument never used
8889
--> $DIR/ifmt-bad-arg.rs:33:22
8990
|
9091
LL | format!("{}", 1, 2); //~ ERROR: argument never used
91-
| ---- ^
92+
| ---- ^ argument never used
9293
| |
9394
| formatting specifier missing
9495

9596
error: argument never used
9697
--> $DIR/ifmt-bad-arg.rs:34:20
9798
|
9899
LL | format!("{1}", 1, 2); //~ ERROR: argument never used
99-
| ----- ^
100+
| ----- ^ argument never used
100101
| |
101102
| formatting specifier missing
102103

103104
error: named argument never used
104105
--> $DIR/ifmt-bad-arg.rs:35:26
105106
|
106107
LL | format!("{}", 1, foo=2); //~ ERROR: named argument never used
107-
| ---- ^
108+
| ---- ^ named argument never used
108109
| |
109110
| formatting specifier missing
110111

111112
error: argument never used
112113
--> $DIR/ifmt-bad-arg.rs:36:22
113114
|
114115
LL | format!("{foo}", 1, foo=2); //~ ERROR: argument never used
115-
| ------- ^
116+
| ------- ^ argument never used
116117
| |
117118
| formatting specifier missing
118119

119120
error: named argument never used
120121
--> $DIR/ifmt-bad-arg.rs:37:21
121122
|
122123
LL | format!("", foo=2); //~ ERROR: named argument never used
123-
| -- ^
124+
| -- ^ named argument never used
124125
| |
125126
| formatting specifier missing
126127

127128
error: multiple unused formatting arguments
128129
--> $DIR/ifmt-bad-arg.rs:38:32
129130
|
130131
LL | format!("{} {}", 1, 2, foo=1, bar=2); //~ ERROR: multiple unused formatting arguments
131-
| ------- ^ ^
132-
| |
132+
| ------- ^ ^ named argument never used
133+
| | |
134+
| | named argument never used
133135
| multiple missing formatting specifiers
134136

135137
error: duplicate argument named `foo`
@@ -160,7 +162,7 @@ error: named argument never used
160162
--> $DIR/ifmt-bad-arg.rs:45:51
161163
|
162164
LL | format!("{valuea} {valueb}", valuea=5, valuec=7);
163-
| ------------------- ^
165+
| ------------------- ^ named argument never used
164166
| |
165167
| formatting specifier missing
166168

@@ -194,7 +196,7 @@ error: argument never used
194196
--> $DIR/ifmt-bad-arg.rs:56:27
195197
|
196198
LL | format!("foo %s baz", "bar"); //~ ERROR: argument never used
197-
| -- ^^^^^
199+
| -- ^^^^^ argument never used
198200
| |
199201
| help: format specifiers use curly braces: `{}`
200202
|

src/test/ui/macros/format-foreign.stderr

+15-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error: multiple unused formatting arguments
22
--> $DIR/format-foreign.rs:2:30
33
|
44
LL | println!("%.*3$s %s!/n", "Hello,", "World", 4); //~ ERROR multiple unused formatting arguments
5-
| -------------- ^^^^^^^^ ^^^^^^^ ^
6-
| |
5+
| -------------- ^^^^^^^^ ^^^^^^^ ^ argument never used
6+
| | | |
7+
| | | argument never used
8+
| | argument never used
79
| multiple missing formatting specifiers
810
|
911
= note: printf formatting not supported; see the documentation for `std::fmt`
@@ -16,7 +18,7 @@ error: argument never used
1618
--> $DIR/format-foreign.rs:3:29
1719
|
1820
LL | println!("%1$*2$.*3$f", 123.456); //~ ERROR never used
19-
| ----------- ^^^^^^^
21+
| ----------- ^^^^^^^ argument never used
2022
| |
2123
| help: format specifiers use curly braces: `{0:1$.2$}`
2224
|
@@ -29,8 +31,10 @@ LL | println!(r###"%.*3$s
2931
| ______________-
3032
LL | | %s!/n
3133
LL | | "###, "Hello,", "World", 4);
32-
| | - ^^^^^^^^ ^^^^^^^ ^
33-
| |____|
34+
| | - ^^^^^^^^ ^^^^^^^ ^ argument never used
35+
| | | | |
36+
| | | | argument never used
37+
| |____| argument never used
3438
| multiple missing formatting specifiers
3539
|
3640
= note: printf formatting not supported; see the documentation for `std::fmt`
@@ -44,15 +48,15 @@ error: argument never used
4448
--> $DIR/format-foreign.rs:12:30
4549
|
4650
LL | println!("{} %f", "one", 2.0); //~ ERROR never used
47-
| ------- ^^^
51+
| ------- ^^^ argument never used
4852
| |
4953
| formatting specifier missing
5054

5155
error: named argument never used
5256
--> $DIR/format-foreign.rs:14:39
5357
|
5458
LL | println!("Hi there, $NAME.", NAME="Tim"); //~ ERROR never used
55-
| ----- ^^^^^
59+
| ----- ^^^^^ named argument never used
5660
| |
5761
| help: format specifiers use curly braces: `{NAME}`
5862
|
@@ -62,8 +66,10 @@ error: multiple unused formatting arguments
6266
--> $DIR/format-foreign.rs:15:32
6367
|
6468
LL | println!("$1 $0 $$ $NAME", 1, 2, NAME=3);
65-
| ---------------- ^ ^ ^
66-
| |
69+
| ---------------- ^ ^ ^ named argument never used
70+
| | | |
71+
| | | argument never used
72+
| | argument never used
6773
| multiple missing formatting specifiers
6874
|
6975
= note: shell formatting not supported; see the documentation for `std::fmt`

src/test/ui/macros/format-unused-lables.stderr

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ error: multiple unused formatting arguments
22
--> $DIR/format-unused-lables.rs:2:22
33
|
44
LL | println!("Test", 123, 456, 789);
5-
| ------ ^^^ ^^^ ^^^
6-
| |
5+
| ------ ^^^ ^^^ ^^^ argument never used
6+
| | | |
7+
| | | argument never used
8+
| | argument never used
79
| multiple missing formatting specifiers
810

911
error: multiple unused formatting arguments
@@ -12,17 +14,17 @@ error: multiple unused formatting arguments
1214
LL | println!("Test2",
1315
| ------- multiple missing formatting specifiers
1416
LL | 123, //~ ERROR multiple unused formatting arguments
15-
| ^^^
17+
| ^^^ argument never used
1618
LL | 456,
17-
| ^^^
19+
| ^^^ argument never used
1820
LL | 789
19-
| ^^^
21+
| ^^^ argument never used
2022

2123
error: named argument never used
2224
--> $DIR/format-unused-lables.rs:11:35
2325
|
2426
LL | println!("Some stuff", UNUSED="args"); //~ ERROR named argument never used
25-
| ------------ ^^^^^^
27+
| ------------ ^^^^^^ named argument never used
2628
| |
2729
| formatting specifier missing
2830

@@ -35,12 +37,12 @@ LL | println!("Some more $STUFF",
3537
| | help: format specifiers use curly braces: `{STUFF}`
3638
| multiple missing formatting specifiers
3739
LL | "woo!", //~ ERROR multiple unused formatting arguments
38-
| ^^^^^^
40+
| ^^^^^^ argument never used
3941
LL | STUFF=
4042
LL | "things"
41-
| ^^^^^^^^
43+
| ^^^^^^^^ named argument never used
4244
LL | , UNUSED="args");
43-
| ^^^^^^
45+
| ^^^^^^ named argument never used
4446
|
4547
= note: shell formatting not supported; see the documentation for `std::fmt`
4648

0 commit comments

Comments
 (0)