-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #109664 - m-ou-se:format-args-placeholder-span, r=oli…
…-obk Use span of placeholders in format_args!() expansion. `format_args!("{}", x)` expands to something that contains `Argument::new_display(&x)`. That entire expression was generated with the span of `x`. After this PR, `&x` uses the span of `x`, but the `new_display` call uses the span of the `{}` placeholder within the format string. If an implicitly captured argument was used like in `format_args!("{x}")`, both use the span of the `{x}` placeholder. This fixes #109576, and also allows for more improvements to similar diagnostics in the future, since the usage of `x` can now be traced to the exact `{}` placeholder that required it to be `Display` (or `Debug` etc.)
- Loading branch information
Showing
8 changed files
with
153 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// check-compile | ||
|
||
struct DisplayOnly; | ||
|
||
impl std::fmt::Display for DisplayOnly { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let x = Some(1); | ||
println!("{x:?} {x} {x:?}"); | ||
//~^ ERROR: `Option<{integer}>` doesn't implement `std::fmt::Display` | ||
println!("{x:?} {x} {x:?}", x = Some(1)); | ||
//~^ ERROR: `Option<{integer}>` doesn't implement `std::fmt::Display` | ||
let x = DisplayOnly; | ||
println!("{x} {x:?} {x}"); | ||
//~^ ERROR: `DisplayOnly` doesn't implement `Debug` | ||
println!("{x} {x:?} {x}", x = DisplayOnly); | ||
//~^ ERROR: `DisplayOnly` doesn't implement `Debug` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
error[E0277]: `Option<{integer}>` doesn't implement `std::fmt::Display` | ||
--> $DIR/format-args-argument-span.rs:13:21 | ||
| | ||
LL | println!("{x:?} {x} {x:?}"); | ||
| ^^^ `Option<{integer}>` cannot be formatted with the default formatter | ||
| | ||
= help: the trait `std::fmt::Display` is not implemented for `Option<{integer}>` | ||
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead | ||
= 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[E0277]: `Option<{integer}>` doesn't implement `std::fmt::Display` | ||
--> $DIR/format-args-argument-span.rs:15:37 | ||
| | ||
LL | println!("{x:?} {x} {x:?}", x = Some(1)); | ||
| ^^^^^^^ `Option<{integer}>` cannot be formatted with the default formatter | ||
| | ||
= help: the trait `std::fmt::Display` is not implemented for `Option<{integer}>` | ||
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead | ||
= 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[E0277]: `DisplayOnly` doesn't implement `Debug` | ||
--> $DIR/format-args-argument-span.rs:18:19 | ||
| | ||
LL | println!("{x} {x:?} {x}"); | ||
| ^^^^^ `DisplayOnly` cannot be formatted using `{:?}` | ||
| | ||
= help: the trait `Debug` is not implemented for `DisplayOnly` | ||
= note: add `#[derive(Debug)]` to `DisplayOnly` or manually `impl Debug for DisplayOnly` | ||
= 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) | ||
help: consider annotating `DisplayOnly` with `#[derive(Debug)]` | ||
| | ||
LL | #[derive(Debug)] | ||
| | ||
|
||
error[E0277]: `DisplayOnly` doesn't implement `Debug` | ||
--> $DIR/format-args-argument-span.rs:20:35 | ||
| | ||
LL | println!("{x} {x:?} {x}", x = DisplayOnly); | ||
| ^^^^^^^^^^^ `DisplayOnly` cannot be formatted using `{:?}` | ||
| | ||
= help: the trait `Debug` is not implemented for `DisplayOnly` | ||
= note: add `#[derive(Debug)]` to `DisplayOnly` or manually `impl Debug for DisplayOnly` | ||
= 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) | ||
help: consider annotating `DisplayOnly` with `#[derive(Debug)]` | ||
| | ||
LL | #[derive(Debug)] | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters