Skip to content

Uninformative "cannot move out of captured variable" error #37150

Closed
@alexcrichton

Description

@alexcrichton

For this code:

#![feature(conservative_impl_trait)]
use std::rc::Rc;

fn bar<R, F: FnMut() -> R>(_: F) -> R {
    loop {}
}

fn foo() -> impl FnOnce() {
    let a = Rc::new(());
    bar(move || {
        let _b = a.clone();
        move || {
            a.clone();
        }
    })
}

it yields the error:

error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
  --> foo.rs:12:9
   |
12 |         move || {
   |         ^^^^^^^ cannot move out of captured outer variable in an `FnMut` closure

error: aborting due to previous error

I recently found this error when I misspelled a captured variable as connnections (should be connections) and it'd be mega-helpful if the error message could mention which captured variable cannot be moved out of. (a span pointing at the variable would also work).

cc @jonathandturner

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions