Skip to content
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

Tweak format string error to point at arguments always #65746

Merged
merged 1 commit into from
Oct 25, 2019
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
5 changes: 4 additions & 1 deletion src/libsyntax_ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, 'b> Context<'a, 'b> {
/// format string.
fn report_invalid_references(&self, numbered_position_args: bool) {
let mut e;
let sp = if self.is_literal {
let sp = if self.is_literal { // Point at the formatting arguments.
MultiSpan::from_spans(self.arg_spans.clone())
} else {
MultiSpan::from_span(self.fmtsp)
Expand All @@ -304,6 +304,9 @@ impl<'a, 'b> Context<'a, 'b> {
self.describe_num_args(),
),
);
for arg in &self.args { // Point at the arguments that will be formatted.
e.span_label(arg.span, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use of "" is fairly weird but it works I suppose. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but DiagnosticBuilder<'_> already handles "" conceptually as an Option::None so we might as well (ab)use it 🤷‍♂

}
} else {
let (mut refs, spans): (Vec<_>, Vec<_>) = refs.unzip();
// Avoid `invalid reference to positional arguments 7 and 7 (there is 1 argument)`
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/fmt/format-string-error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ fn main() {

"###);
//~^^^ ERROR invalid format string: unmatched `}` found
println!("{} {} {}", 1, 2);
//~^ ERROR 3 positional arguments in format string, but there are 2 arguments
}
8 changes: 7 additions & 1 deletion src/test/ui/fmt/format-string-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,11 @@ LL | }
|
= note: if you intended to print `}`, you can escape it using `}}`

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

error: aborting due to 13 previous errors

10 changes: 6 additions & 4 deletions src/test/ui/if/ifmt-bad-arg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ error: 4 positional arguments in format string, but there are 3 arguments
--> $DIR/ifmt-bad-arg.rs:78:15
|
LL | println!("{} {:.*} {}", 1, 3.2, 4);
| ^^ ^^--^ ^^ --- this parameter corresponds to the precision flag
| |
| ^^ ^^--^ ^^ - --- -
| | |
| | this parameter corresponds to the precision flag
| this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
|
= note: positional arguments are zero-based
Expand All @@ -235,8 +236,9 @@ error: 4 positional arguments in format string, but there are 3 arguments
--> $DIR/ifmt-bad-arg.rs:81:15
|
LL | println!("{} {:07$.*} {}", 1, 3.2, 4);
| ^^ ^^^----^ ^^ --- this parameter corresponds to the precision flag
| | |
| ^^ ^^^----^ ^^ - --- -
| | | |
| | | this parameter corresponds to the precision flag
| | this precision flag adds an extra required argument at position 1, which is why there are 4 arguments expected
| this width flag expects an `usize` argument at position 7, but there are 3 arguments
|
Expand Down