Skip to content

Take advantage of negative_impls in error messages #79683

Closed
@scottmcm

Description

@scottmcm

(Spotted in https://users.rust-lang.org/t/the-trait-clone-is-not-implemented-for-mut-t/51989?u=scottmcm)

I tried this code https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=6c22f56bb7cfec1e8e04a79fc078cdd6:

#![feature(negative_impls)]

trait Foo {
    fn foo(&self) {}
}

impl Foo for u64 {}
impl !Foo for i32 {}

fn main() {
    let x = 4_i32;
    x.foo();    
}

Which of course fails to compile. And it gives the following:

error[E0599]: no method named `foo` found for type `i32` in the current scope
  --> src/main.rs:12:7
   |
12 |     x.foo();    
   |       ^^^ method not found in `i32`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
note: `Foo` defines an item `foo`, perhaps you need to implement it
  --> src/main.rs:3:1
   |
3  | trait Foo {
   | ^^^^^^^^^

It would be nice to take advantage of the negative implementation to not say "perhaps you need to implement it" and in fact to say that it explicitly is not implemented. Other good similar cases would be things like Foo::foo(&x).

(This is a follow-up issue to #79458, as the bug part was fixed, so this is the lower-priority improvement part.)

cc #68318

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