Skip to content

impl Trait missed bound should hint if underlying type could satisfy the bound #146607

@zesterer

Description

@zesterer

Code

use core::fmt::Debug;

fn accepts_copy(_x: impl Copy + Debug) {}

fn create_copyable() -> impl Debug { 42i32 }

fn main() {
    accepts_copy(create_copyable());
}

Current output

error[E0277]: the trait bound `impl Debug: std::marker::Copy` is not satisfied
 --> src/main.rs:8:19
  |
8 |     accepts_copy(create_copyable());
  |     ------------ ^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `impl Debug`
  |     |
  |     required by a bound introduced by this call
  |
note: required by a bound in `accepts_clone`
 --> src/main.rs:3:26
  |
3 | fn accepts_copy(_x: impl Copy + Debug) {}
  |                          ^^^^ required by this bound in `accepts_clone`

For more information about this error, try `rustc --explain E0277`.

Desired output

error[E0277]: the trait bound `impl Debug: std::marker::Copy` is not satisfied
 --> src/main.rs:8:19
  |
8 |     accepts_copy(create_copyable());
  |     ------------ ^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `impl Debug`
  |     |
  |     required by a bound introduced by this call
  |
note: required by a bound in `accepts_clone`
 --> src/main.rs:3:26
  |
3 | fn accepts_copy(x: impl Copy + Debug) {}
  |                         ^^^^ required by this bound in `accepts_clone`
  |
note: underlying type `i32` implements `Copy`, consider changing the bound
  |
5 | fn create_copyable() -> impl Debug + Copy { 42i32 }
                                      +++++++

Rationale and extra context

I was inspired to report this by this issue.

In cases where the originator of the impl Trait type is 'owned' by the consumer (i.e: is in the same crate/workspace) and the underlying type is found to implement the required trait, a note that explains how to express this syntactically would be very useful.

This might even be a useful diagnostic even when the underlying type does not implement the required trait, although this is much more debatable.

Other cases

Rust Version

1.92.0-nightly (2025-09-14 52618eb338609df44978)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-trait-systemArea: Trait systemD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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