Skip to content

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

Closed
@Aaron1011

Description

@Aaron1011

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions