Fix off by one error and add ui test.

This commit is contained in:
miam-miam100 2022-07-20 13:40:45 +01:00
parent 62187b12c2
commit f8dfc4bf35
No known key found for this signature in database
GPG Key ID: 0C3730101953F7DD
3 changed files with 17 additions and 3 deletions

View File

@ -485,7 +485,7 @@ impl<'a, 'b> Context<'a, 'b> {
if let Some(span) = fmt.width_span { if let Some(span) = fmt.width_span {
let span = self.fmtsp.from_inner(InnerSpan::new(span.start, span.end)); let span = self.fmtsp.from_inner(InnerSpan::new(span.start, span.end));
match fmt.width { match fmt.width {
parse::CountIsParam(pos) if pos > self.num_args() => { parse::CountIsParam(pos) if pos >= self.num_args() => {
e.span_label( e.span_label(
span, span,
&format!( &format!(

View File

@ -86,6 +86,9 @@ tenth number: {}",
println!("{:foo}", 1); //~ ERROR unknown format trait `foo` println!("{:foo}", 1); //~ ERROR unknown format trait `foo`
println!("{5} {:4$} {6:7$}", 1); println!("{5} {:4$} {6:7$}", 1);
//~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument) //~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument)
let foo = 1;
println!("{foo:0$}");
//~^ ERROR invalid reference to positional argument 0 (no arguments were given)
// We used to ICE here because we tried to unconditionally access the first argument, which // We used to ICE here because we tried to unconditionally access the first argument, which
// doesn't exist. // doesn't exist.

View File

@ -251,8 +251,19 @@ LL | println!("{5} {:4$} {6:7$}", 1);
= note: positional arguments are zero-based = note: positional arguments are zero-based
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
error: invalid reference to positional argument 0 (no arguments were given)
--> $DIR/ifmt-bad-arg.rs:90:15
|
LL | println!("{foo:0$}");
| ^^^^^--^
| |
| this width flag expects an `usize` argument at position 0, but no arguments were given
|
= note: positional arguments are zero-based
= note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html
error: 2 positional arguments in format string, but no arguments were given error: 2 positional arguments in format string, but no arguments were given
--> $DIR/ifmt-bad-arg.rs:92:15 --> $DIR/ifmt-bad-arg.rs:95:15
| |
LL | println!("{:.*}"); LL | println!("{:.*}");
| ^^--^ | ^^--^
@ -328,7 +339,7 @@ LL | pub fn from_usize(x: &usize) -> ArgumentV1<'_> {
| ^^^^^^^^^^ | ^^^^^^^^^^
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 36 previous errors error: aborting due to 37 previous errors
Some errors have detailed explanations: E0308, E0425. Some errors have detailed explanations: E0308, E0425.
For more information about an error, try `rustc --explain E0308`. For more information about an error, try `rustc --explain E0308`.