rust-lang / rust-lang/rust

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

Open
#149,650 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-incorrect T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproducer in src/lib.rs and examining the E0119 diagnostic for the two blanket implementations. Trace how the compiler reasons about downstream Clone, Copy, and Pod implementations for &mut T. Done means the non-overlapping implementations compile or the diagnostic no longer makes the incorrect claims described here.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.