Skip to content

Misleading diagnostics with missing bounds when deriving a trait  #84003

Closed
@a1phyr

Description

@a1phyr

Given the following code: https://play.rust-lang.org/?edition=2018&gist=5f8c9b8b72174c48746e236c827ab881

use std::fmt;

trait Trait {}

struct X<T>(T);

impl<T: fmt::Debug + Trait> fmt::Debug for X<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("X").field(&self.0).finish()
    }
}

#[derive(Debug)]
struct Y<T>(X<T>);

The current output is:

error[E0277]: the trait bound `T: Trait` is not satisfied
  --> src/lib.rs:14:13
   |
14 | struct Y<T>(X<T>);
   |             ^^^^ the trait `Trait` is not implemented for `T`
   |
   = note: required because of the requirements on the impl of `Debug` for `X<T>`
   = note: 1 redundant requirements hidden
   = note: required because of the requirements on the impl of `Debug` for `&X<T>`
   = note: required for the cast to the object type `dyn Debug`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
   |
13 | #[derive(Debug + Trait)]
   |                ^^^^^^^

The proposed solution is obviously wrong, the compiler should suggest a manual impl instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions