Skip to content

Commit 4f55b52

Browse files
committed
auto merge of #15785 : treeman/rust/fix-15780, r=alexcrichton
Fix for #15780.
2 parents 50481f5 + 820a558 commit 4f55b52

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/libsyntax/ext/format.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,21 @@ impl<'a, 'b> Context<'a, 'b> {
215215
}
216216
}
217217

218+
fn describe_num_args(&self) -> String {
219+
match self.args.len() {
220+
0 => "no arguments given".to_string(),
221+
1 => "there is 1 argument".to_string(),
222+
x => format!("there are {} arguments", x),
223+
}
224+
}
225+
218226
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
219227
match arg {
220228
Exact(arg) => {
221229
if self.args.len() <= arg {
222-
let msg = format!("invalid reference to argument `{}` (there \
223-
are {} arguments)", arg, self.args.len());
230+
let msg = format!("invalid reference to argument `{}` ({:s})",
231+
arg, self.describe_num_args());
232+
224233
self.ecx.span_err(self.fmtsp, msg.as_slice());
225234
return;
226235
}

src/test/compile-fail/ifmt-bad-arg.rs

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ fn main() {
2929
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
3030
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
3131

32+
// bad number of arguments, see #15780
33+
34+
format!("{0}");
35+
//~^ ERROR invalid reference to argument `0` (no arguments given)
36+
37+
format!("{0} {1}", 1);
38+
//~^ ERROR invalid reference to argument `1` (there is 1 argument)
39+
40+
format!("{0} {1} {2}", 1, 2);
41+
//~^ ERROR invalid reference to argument `2` (there are 2 arguments)
42+
43+
format!("{0} {1}");
44+
//~^ ERROR invalid reference to argument `0` (no arguments given)
45+
//~^^ ERROR invalid reference to argument `1` (no arguments given)
46+
3247
// bad syntax of the format string
3348

3449
format!("{"); //~ ERROR: expected `}` but string was terminated

0 commit comments

Comments
 (0)