Skip to content

closure compilation fails depending on type inference #64373

@chlobes

Description

@chlobes
fn foo(_: &mut String) {}

fn bug1() {
    let _ = |a: &mut String| for _ in 0..0 {
        foo(a)
    }; //works fine
    let _ = |a| for _ in 0..0 {
        foo(a)
    }; //fails
}

fn bug2() {
    let mut a = String::new();
    let mut b = String::new();
    let f = |a: &mut String| foo(a);
    let g = |b| foo(b);
    f(&mut a); //works
    f(&mut a); //works
    g(&mut b); //works
    g(&mut b); //fails
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `a`
 --> src/lib.rs:8:13
  |
7 |     let _ = |a| for _ in 0..0 {
  |              - move occurs because `a` has type `&mut std::string::String`, which does not implement the `Copy` trait
8 |         foo(a)
  |             ^ value moved here, in previous iteration of loop

error[E0499]: cannot borrow `b` as mutable more than once at a time
  --> src/lib.rs:20:7
   |
19 |     g(&mut b); //works
   |       ------ first mutable borrow occurs here
20 |     g(&mut b); //fails
   |     - ^^^^^^ second mutable borrow occurs here
   |     |
   |     first borrow later used by call

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0382, E0499.
For more information about an error, try `rustc --explain E0382`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-inferenceArea: Type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions