Code
I tried this code:
use std::fmt::{Debug, Display};
struct Identity<T>(T);
impl<T> Display for Identity<T>
where
T: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
struct Thing {}
#[automatically_derived]
impl Debug for Thing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", Identity("Thing"))
}
}
fn main() {
let hello = Thing {};
println!("{hello:?}");
}
I expected to see this happen: The code compiles without any warnings since, the Identity struct is clearly used.
Instead, this happened: The compiler emits a dead_code lint, telling me that the struct is never constructed
Example where this can occur: when using the derive_more crate to derive a specialized Debug implementation, and the usage of derive_more::Debug utilizes a formatter struct to avoid allocating into an intermediate heap allocated buffer
Real-world example
Version it worked on
It most recently worked on: Rust 1.77.2
Version with regression
rustc --version --verbose:
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged
Code
I tried this code:
I expected to see this happen: The code compiles without any warnings since, the
Identitystruct is clearly used.Instead, this happened: The compiler emits a
dead_codelint, telling me that the struct is never constructedExample where this can occur: when using the
derive_morecrate to derive a specializedDebugimplementation, and the usage ofderive_more::Debugutilizes a formatter struct to avoid allocating into an intermediate heap allocated bufferReal-world example
Version it worked on
It most recently worked on: Rust 1.77.2
Version with regression
rustc --version --verbose:@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged