Skip to content

Cannot assign to captured outer variable in an FnMut closure diagnostic #41790

Closed
@Mark-Simulacrum

Description

@Mark-Simulacrum
fn foo(mut f: Box<FnMut()>) {
    f();
}

fn main() {
    let y = true;
    foo(Box::new(move || y = false) as Box<_>);
}

produces

error: cannot assign to captured outer variable in an `FnMut` closure
 --> test.rs:7:26
  |
7 |     foo(Box::new(move || y = false) as Box<_>);
  |                          ^^^^^^^^^

error: aborting due to previous error

but should produce

error: cannot assign to captured outer variable in an `FnMut` closure
 --> test.rs:7:26
  |
6 |     let y = true;
  |         - use `mut y` here to make mutable
7 |     foo(Box::new(move || y = false) as Box<_>);
  |                          ^^^^^^^^^

error: aborting due to previous error

cc @estebank, you implemented a similar change for moves: #41523.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions