Skip to content

Commit 0f177c3

Browse files
estebankMark-Simulacrum
authored andcommitted
Do not ICE whith a precision flag in formatting str and no format arguments
1 parent 71dbc68 commit 0f177c3

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/libsyntax_ext/format.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,12 @@ impl<'a, 'b> Context<'a, 'b> {
374374
format!("are {} arguments", count)
375375
},
376376
));
377-
e.span_label(
378-
self.args[pos].span,
379-
"this parameter corresponds to the precision flag",
380-
);
377+
if let Some(arg) = self.args.get(pos) {
378+
e.span_label(
379+
arg.span,
380+
"this parameter corresponds to the precision flag",
381+
);
382+
}
381383
zero_based_note = true;
382384
}
383385
_ => {}

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

+5
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ tenth number: {}",
8686
println!("{:foo}", 1); //~ ERROR unknown format trait `foo`
8787
println!("{5} {:4$} {6:7$}", 1);
8888
//~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument)
89+
90+
// We used to ICE here because we tried to unconditionally access the first argument, which
91+
// doesn't exist.
92+
println!("{:.*}");
93+
//~^ ERROR 2 positional arguments in format string, but no arguments were given
8994
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ LL | println!("{5} {:4$} {6:7$}", 1);
285285
= note: positional arguments are zero-based
286286
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
287287

288+
error: 2 positional arguments in format string, but no arguments were given
289+
--> $DIR/ifmt-bad-arg.rs:92:15
290+
|
291+
LL | println!("{:.*}");
292+
| ^^--^
293+
| |
294+
| this precision flag adds an extra required argument at position 0, which is why there are 2 arguments expected
295+
|
296+
= note: positional arguments are zero-based
297+
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
298+
288299
error[E0308]: mismatched types
289300
--> $DIR/ifmt-bad-arg.rs:78:32
290301
|
@@ -303,6 +314,6 @@ LL | println!("{} {:07$.*} {}", 1, 3.2, 4);
303314
= note: expected type `&usize`
304315
found type `&{float}`
305316

306-
error: aborting due to 35 previous errors
317+
error: aborting due to 36 previous errors
307318

308319
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)