Skip to content

Incorrect 'use occurs' note when value is moved into a closure inside a loop #75904

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

Closed
Aaron1011 opened this issue Aug 25, 2020 · 0 comments · Fixed by #75933
Closed

Incorrect 'use occurs' note when value is moved into a closure inside a loop #75904

Aaron1011 opened this issue Aug 25, 2020 · 0 comments · Fixed by #75933
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information.

Comments

@Aaron1011
Copy link
Member

Aaron1011 commented Aug 25, 2020

The following code:

struct NotCopy;

fn main() {
    let mut a = NotCopy;
    loop {
        || {
            &mut a;
            a;
        };
    }
}

produces the following error:

error[E0382]: use of moved value: `a`
 --> src/lib.rs:6:9
  |
4 |     let mut a = NotCopy;
  |         ----- move occurs because `a` has type `NotCopy`, which does not implement the `Copy` trait
5 |     loop {
6 |         || {
  |         ^^ value moved into closure here, in previous iteration of loop
7 |             &mut a;
  |                  - use occurs due to use in closure

However, the &mut a; statement is not responsible for the move - it's the a; statement below it which causes the move.

Meta

rustc --version --verbose:

rustc 1.47.0-nightly (de521cbb3 2020-08-21)
binary: rustc
commit-hash: de521cbb303c08febd9fa3755caccd4f3e491ea3
commit-date: 2020-08-21
host: x86_64-unknown-linux-gnu
release: 1.47.0-nightly
LLVM version: 10.0

@Aaron1011 Aaron1011 added the C-bug Category: This is a bug. label Aug 25, 2020
@Aaron1011 Aaron1011 changed the title Incorrect Incorrect 'use occurs' note when value is moved into a closure inside a loop Aug 25, 2020
@Aaron1011 Aaron1011 added A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Aug 25, 2020
@jonas-schievink jonas-schievink added the A-closures Area: Closures (`|…| { … }`) label Aug 25, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 27, 2020
…=oli-obk

Point to a move-related span when pointing to closure upvars

Fixes rust-lang#75904

When emitting move/borrow errors, we may point into a closure to
indicate why an upvar is used in the closure. However, we use the
'upvar span', which is just an arbitrary usage of the upvar. If the
upvar is used in multiple places (e.g. a borrow and a move), we may end
up pointing to the borrow. If the overall error is a move error, this
can be confusing.

This PR tracks the span that caused an upvar to become captured by-value
instead of by-ref (assuming that it's not a `move` closure). We use this
span instead of the 'upvar' span when we need to point to an upvar usage
during borrow checking.
@bors bors closed this as completed in b5b8b93 Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants