Skip to content

Correct plural of arguments in format_args! #15785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,21 @@ impl<'a, 'b> Context<'a, 'b> {
}
}

fn describe_num_args(&self) -> String {
match self.args.len() {
0 => "no arguments given".to_string(),
1 => "there is 1 argument".to_string(),
x => format!("there are {} arguments", x),
}
}

fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
match arg {
Exact(arg) => {
if self.args.len() <= arg {
let msg = format!("invalid reference to argument `{}` (there \
are {} arguments)", arg, self.args.len());
let msg = format!("invalid reference to argument `{}` ({:s})",
arg, self.describe_num_args());

self.ecx.span_err(self.fmtsp, msg.as_slice());
return;
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/ifmt-bad-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ fn main() {
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow

// bad number of arguments, see #15780

format!("{0}");
//~^ ERROR invalid reference to argument `0` (no arguments given)

format!("{0} {1}", 1);
//~^ ERROR invalid reference to argument `1` (there is 1 argument)

format!("{0} {1} {2}", 1, 2);
//~^ ERROR invalid reference to argument `2` (there are 2 arguments)

format!("{0} {1}");
//~^ ERROR invalid reference to argument `0` (no arguments given)
//~^^ ERROR invalid reference to argument `1` (no arguments given)

// bad syntax of the format string

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