Skip to content

Positive and Negative Trait Implementations Clash Unexpectedly #134632

Closed
@Ez-FlawLess

Description

@Ez-FlawLess

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coherenceArea: CoherenceA-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-negative_impls#![feature(negative_impls)]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