Skip to content

Incorrect "conflicting implementations" error between two non-overlapping blanket impls #149650

@SludgePhD

Description

@SludgePhD

I tried this code:

pub trait Pod: Copy + 'static {}

pub trait Marshal {}

impl<T> Marshal for T where T: Pod {}
impl<T> Marshal for &mut T where T: Pod {}

I expected to see this happen: Compiles.

Instead, this happened:

error[E0119]: conflicting implementations of trait `Marshal` for type `&mut _`
 --> src/lib.rs:6:1
  |
5 | impl<T> Marshal for T where T: Pod {}
  | ---------------------------------- first implementation here
6 | impl<T> Marshal for &mut T where T: Pod {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&mut _`
  |
  = note: downstream crates may implement trait `std::clone::Clone` for type `&mut _`
  = note: downstream crates may implement trait `std::marker::Copy` for type `&mut _`
  = note: downstream crates may implement trait `Pod` for type `&mut _`

Notably, the notes are simply wrong: while it is possible to implement some upstream traits for &mut T as long as T is local (since &mut T is fundamental), a Clone or Copy impl cannot be written for any mutable reference type, since libcore has a negative impl !Clone for &mut T.
Attempting to do so anyways will yield "error[E0751]: found both positive and negative implementation of trait Clone for type &mut S".

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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