Skip to content
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

Incorrect Deref implementation causes incorrect error about importing Clone #131003

Closed
acotis opened this issue Sep 29, 2024 · 0 comments · Fixed by #131024
Closed

Incorrect Deref implementation causes incorrect error about importing Clone #131003

acotis opened this issue Sep 29, 2024 · 0 comments · Fixed by #131024
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@acotis
Copy link

acotis commented Sep 29, 2024

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

@acotis acotis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 29, 2024
@compiler-errors compiler-errors self-assigned this Sep 29, 2024
@bors bors closed this as completed in 0d65f12 Oct 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 4, 2024
Rollup merge of rust-lang#131024 - compiler-errors:deref-sugg, r=estebank

Don't give method suggestions when method probe fails due to bad implementation of `Deref`

If we have a bad `Deref` impl, we used to bail with `MethodError::NoMatch`, which makes the error reporting code think that there was no applicable method (and thus try to suggest importing something, even if it's in scope).

Suppress this error, which fixes rust-lang#131003.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants