Skip to content

rustc complains about unused code if item is used in #[automatically_derived] context #126031

Open
@aumetra

Description

@aumetra

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

Activity

added
C-bugCategory: This is a bug.
regression-untriagedUntriaged performance or correctness regression.
on Jun 5, 2024
added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
and removed
regression-untriagedUntriaged performance or correctness regression.
on Jun 5, 2024
aumetra

aumetra commented on Jun 5, 2024

@aumetra
Author

Okay, I submitted it under the wrong category. Apologies for that. Clearly a diagnostic issue.

aumetra

aumetra commented on Jun 5, 2024

@aumetra
Author

@rustbot modify labels: -regression-from-stable-to-stable +A-diagnostics

added
A-diagnosticsArea: Messages for errors, warnings, and lints
and removed
regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
on Jun 5, 2024
aumetra

aumetra commented on Jun 5, 2024

@aumetra
Author

@rustbot modify labels: -I-prioritize +T-compiler

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
and removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Jun 5, 2024
apiraino

apiraino commented on Jun 5, 2024

@apiraino
Contributor

Actually I think you were right, seems to be a diagnostic regression introduced in c69fda7 (so my bisection reports):

cc @mu001999 since author of #121752 and @pnkfelix (reviewer of said patch)

@rustbot label +regression-from-stable-to-stable +P-medium -needs-triage

9 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @apiraino@mu001999@rustbot@aumetra

      Issue actions

        rustc complains about unused code if item is used in `#[automatically_derived]` context · Issue #126031 · rust-lang/rust