Skip to content

Positive and Negative Trait Implementations Clash Unexpectedly #134632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ez-FlawLess opened this issue Dec 21, 2024 · 2 comments · Fixed by #134639
Closed

Positive and Negative Trait Implementations Clash Unexpectedly #134632

Ez-FlawLess opened this issue Dec 21, 2024 · 2 comments · Fixed by #134639
Labels
A-coherence Area: Coherence A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-negative_impls #![feature(negative_impls)] T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Ez-FlawLess
Copy link

Code

#![allow(unused)]
#![feature(negative_impls)]

trait MyTrait {}

#[derive(Clone)]
struct MyString {
    string: String,
}

impl<T: Copy> !MyTrait for T { }

// Works
impl MyTrait for MyString { }
// Throws error but it should work
impl MyTrait for String { }

fn main() {
    
}

rustc --version --verbose:

rustc 1.85.0-nightly (5f23ef7d3 2024-12-20)
binary: rustc
commit-hash: 5f23ef7d3f7a8c3e0ca5c4e1978829c0448a3686
commit-date: 2024-12-20
host: aarch64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Error output

error[E0751]: found both positive and negative implementation of trait `MyTrait` for type `String`:
  --> src/main.rs:16:1
   |
11 | impl<T: Copy> !MyTrait for T { }
   | ---------------------------- negative implementation here
...
16 | impl MyTrait for String { }
   | ^^^^^^^^^^^^^^^^^^^^^^^ positive implementation here
@Ez-FlawLess Ez-FlawLess added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 21, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 21, 2024
@Ez-FlawLess Ez-FlawLess changed the title Rust Compiler Error: Positive and Negative Trait Implementations Clash Unexpectedly Positive and Negative Trait Implementations Clash Unexpectedly Dec 21, 2024
@compiler-errors
Copy link
Member

So we aren't as good at explaining coherence errors for negative impls, but if you change this to a positive impl you'll get the justification you need:

error[E0119]: conflicting implementations of trait `MyTrait` for type `std::string::String`
 --> <source>:8:1
  |
5 | impl<T: Copy> MyTrait for T { }
  | --------------------------- first implementation here
...
8 | impl MyTrait for String { }
  | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `std::string::String`
  |
  = note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::string::String` in future versions

That is to say, according to the orphan rules, the standard library is is technically free to add a new impl Copy for String implementation in the future. This would break your downstream crate, so the compiler proactively rejects this impl overlap.

The reason it works for MyString is because that's a type that is local to your crate.

@Ez-FlawLess
Copy link
Author

Ez-FlawLess commented Dec 21, 2024

Thanks. So the only thing we need is changing the error message?
@compiler-errors

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints F-negative_impls #![feature(negative_impls)] D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-coherence Area: Coherence and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 22, 2024
@bors bors closed this as completed in 4d166cc Dec 22, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 22, 2024
Rollup merge of rust-lang#134639 - compiler-errors:negative-ambiguity-causes, r=oli-obk

Make sure we note ambiguity causes on positive/negative impl conflicts

Fixes rust-lang#134632 by explaining why the error must be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coherence Area: Coherence A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-negative_impls #![feature(negative_impls)] T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants