Skip to content

Incorrect Deref implementation causes incorrect error about importing Clone #131003

Closed
@acotis

Description

@acotis

Code

use std::clone::Clone;
use std::ops::Deref;

#[derive(Clone)]
pub struct Foo {}

impl Deref for Foo {}

pub fn main() {
    let f = Foo {};
    let _ = f.clone();
}

Current output

   Compiling sandbox v0.1.0 (/home/evan/Projects/AAAA Sandbox/sandbox)
error[E0046]: not all trait items implemented, missing: `Target`, `deref`
 --> src/main.rs:8:1
  |
8 | impl Deref for Foo {}
  | ^^^^^^^^^^^^^^^^^^ missing `Target`, `deref` in implementation
  |
  = help: implement the missing item: `type Target = /* Type */;`
  = help: implement the missing item: `fn deref(&self) -> &<Self as Deref>::Target { todo!() }`

error[E0599]: no method named `clone` found for struct `Foo` in the current scope
  --> src/main.rs:12:15
   |
6  | pub struct Foo {}
   | -------------- method `clone` not found for this struct
...
12 |     let _ = f.clone();
   |               ^^^^^ method not found in `Foo`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `Clone`
help: trait `Clone` which provides `clone` is implemented but not in scope; perhaps you want to import it
   |
2  + use std::clone::Clone;
   |

Some errors have detailed explanations: E0046, E0599.
For more information about an error, try `rustc --explain E0046`.
error: could not compile `sandbox` (bin "sandbox") due to 2 previous errors

Desired output

   Compiling sandbox v0.1.0 (/home/evan/Projects/AAAA Sandbox/sandbox)
error[E0046]: not all trait items implemented, missing: `Target`, `deref`
 --> src/main.rs:8:1
  |
8 | impl Deref for Foo {}
  | ^^^^^^^^^^^^^^^^^^ missing `Target`, `deref` in implementation
  |
  = help: implement the missing item: `type Target = /* Type */;`
  = help: implement the missing item: `fn deref(&self) -> &<Self as Deref>::Target { todo!() }`

Some errors have detailed explanations: E0046.
For more information about an error, try `rustc --explain E0046`.
error: could not compile `sandbox` (bin "sandbox") due to 1 previous error

Rationale and extra context

The code above contains an incorrect implementation of Deref. The compiler output should contain one error message explaining that the Deref implementation is wrong. Instead, it contains that error message plus another one saying that Clone isn't in scope and needs to be imported before I can use .clone(), which is wrong for two reasons: first, it doesn't need to be imported, and second, it already is. If the incorrect Deref implementation is removed, the code compiles without complaining about my use of .clone().

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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