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

Confusing error message on deref-coercion after partial move out #73268

Closed
Aaron1011 opened this issue Jun 12, 2020 · 0 comments · Fixed by #75304
Closed

Confusing error message on deref-coercion after partial move out #73268

Aaron1011 opened this issue Jun 12, 2020 · 0 comments · Fixed by #75304
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code:

use std::ops::Deref;

struct NotCopy {
    inner: bool
}

struct Foo {
    first: NotCopy,
    second: NotCopy
}

impl Deref for Foo {
    type Target = NotCopy;
    fn deref(&self) -> &NotCopy {
        &self.second
    }
}

fn use_it(val: Foo) {
    let inner = val.first;
    //val.second;
    val.inner;
}

gives the following error message:

error[E0382]: borrow of moved value: `val`
  --> src/lib.rs:22:5
   |
20 |     let inner = val.first;
   |                 --------- value moved here
21 |     //val.second;
22 |     val.inner;
   |     ^^^ value borrowed here after partial move
   |
   = note: move occurs because `val.first` has type `NotCopy`, which does not implement the `Copy` trait

This error occurs because val.inner performs a deref-coercion to access the field inner on NotCopy. However, this is not mentioned in the error message - we instead refer to val being 'borrowed' by what appears to be a normal field access.

If val.second is uncommented, and val.inner commented out, the code compiles successfully. This may be very confusing to users who don't realize that only one field involves a deref-coercion, as it looks like either access should be allowed.

When a move error occurs, we should explicitly mention any deref-coercions that occur, and explain why they prevent the code from compiling.

@Aaron1011 Aaron1011 added C-enhancement Category: An issue proposing an enhancement or a PR with one. 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. A-coercions Area: implicit and explicit `expr as Type` coercions D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jun 12, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Sep 15, 2020
…ut, r=estebank

Note when a a move/borrow error is caused by a deref coercion

Fixes rust-lang#73268

When a deref coercion occurs, we may end up with a move error if the
base value has been partially moved out of. However, we do not indicate
anywhere that a deref coercion is occuring, resulting in an error
message with a confusing span.

This PR adds an explicit note to move errors when a deref coercion is
involved. We mention the name of the type that the deref-coercion
resolved to, as well as the `Deref::Target` associated type being used.
@bors bors closed this as completed in d18b4bb Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

1 participant