Skip to content

Commit 1e4a2ee

Browse files
authored
Rollup merge of rust-lang#65746 - estebank:newcomer-format, r=Centril
Tweak format string error to point at arguments always Add secondary span labels with no text to make it clear when there's a mismatch bewteen the positional arguments in a format string and the arguments to the macro. This shouldn't affect experienced users, but it should make it easier for newcomers to more clearly understand how `format!()` and `println!()` are supposed to be used. ``` error: 2 positional arguments in format string, but there is 1 argument --> file8.rs:2:14 | 2 | format!("{} {}", 1); | ^^ ^^ - ``` instead of ``` error: 2 positional arguments in format string, but there is 1 argument --> file8.rs:2:14 | 2 | format!("{} {}", 1); | ^^ ^^ ``` r? @Centril
2 parents efa5037 + 8467cef commit 1e4a2ee

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/libsyntax_ext/format.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'a, 'b> Context<'a, 'b> {
278278
/// format string.
279279
fn report_invalid_references(&self, numbered_position_args: bool) {
280280
let mut e;
281-
let sp = if self.is_literal {
281+
let sp = if self.is_literal { // Point at the formatting arguments.
282282
MultiSpan::from_spans(self.arg_spans.clone())
283283
} else {
284284
MultiSpan::from_span(self.fmtsp)
@@ -304,6 +304,9 @@ impl<'a, 'b> Context<'a, 'b> {
304304
self.describe_num_args(),
305305
),
306306
);
307+
for arg in &self.args { // Point at the arguments that will be formatted.
308+
e.span_label(arg.span, "");
309+
}
307310
} else {
308311
let (mut refs, spans): (Vec<_>, Vec<_>) = refs.unzip();
309312
// Avoid `invalid reference to positional arguments 7 and 7 (there is 1 argument)`

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

+2
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ fn main() {
4848
4949
"###);
5050
//~^^^ ERROR invalid format string: unmatched `}` found
51+
println!("{} {} {}", 1, 2);
52+
//~^ ERROR 3 positional arguments in format string, but there are 2 arguments
5153
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,11 @@ LL | }
107107
|
108108
= note: if you intended to print `}`, you can escape it using `}}`
109109

110-
error: aborting due to 12 previous errors
110+
error: 3 positional arguments in format string, but there are 2 arguments
111+
--> $DIR/format-string-error.rs:51:15
112+
|
113+
LL | println!("{} {} {}", 1, 2);
114+
| ^^ ^^ ^^ - -
115+
116+
error: aborting due to 13 previous errors
111117

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ error: 4 positional arguments in format string, but there are 3 arguments
224224
--> $DIR/ifmt-bad-arg.rs:78:15
225225
|
226226
LL | println!("{} {:.*} {}", 1, 3.2, 4);
227-
| ^^ ^^--^ ^^ --- this parameter corresponds to the precision flag
228-
| |
227+
| ^^ ^^--^ ^^ - --- -
228+
| | |
229+
| | this parameter corresponds to the precision flag
229230
| this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
230231
|
231232
= note: positional arguments are zero-based
@@ -235,8 +236,9 @@ error: 4 positional arguments in format string, but there are 3 arguments
235236
--> $DIR/ifmt-bad-arg.rs:81:15
236237
|
237238
LL | println!("{} {:07$.*} {}", 1, 3.2, 4);
238-
| ^^ ^^^----^ ^^ --- this parameter corresponds to the precision flag
239-
| | |
239+
| ^^ ^^^----^ ^^ - --- -
240+
| | | |
241+
| | | this parameter corresponds to the precision flag
240242
| | this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
241243
| this width flag expects an `usize` argument at position 7, but there are 3 arguments
242244
|

0 commit comments

Comments
 (0)