Skip to content

Spurious "note: downstream crates may implement trait Foo for type &_" #48869

Open
@glandium

Description

@glandium

The following code:

trait Foo {}

impl<T: Foo> std::fmt::Display for T {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        Ok(())
    }
}

produces the following compiler output:

error[E0119]: conflicting implementations of trait `std::fmt::Display` for type `&_`:
 --> src/main.rs:3:1
  |
3 | impl<T: Foo> std::fmt::Display for T {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<'a, T> std::fmt::Display for &'a T
            where T: std::fmt::Display, T: ?Sized;
  = note: downstream crates may implement trait `Foo` for type `&_`

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
 --> src/main.rs:3:1
  |
3 | / impl<T: Foo> std::fmt::Display for T {
4 | |     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
5 | |         Ok(())
6 | |     }
7 | | }
  | |_^

error: aborting due to 2 previous errors

While there might be legitimate reasons why this is not allowed, the note is wrong: the trait Foo is not public, a downstream can't possibly implement it for some type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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