Open
Description
It looks like type inference of arguments to a lambda function somehow interferes with borrow checking.
I tried this code: (playground link)
struct Bar {}
struct Foo {
a: Bar,
}
impl Foo {
fn one(&self) -> Option<&Bar> {
return Some(&self.a);
}
fn two(&mut self) -> Option<Bar> {
return Some(Bar {});
}
}
fn main() {
let mut f = Foo { a: Bar {} };
let test = |_t| {};
match f.one() {
Some(a) => test(a),
None => {}
}
match f.two() {
Some(ref b) => test(b),
None => {}
}
}
I expected to see this happen:
A successful build
Instead, this happened:
Compilation failed with this error:
error[E0502]: cannot borrow `f` as mutable because it is also borrowed as immutable
--> src/lib.rs:27:11
|
22 | match f.one() {
| - immutable borrow occurs here
...
27 | match f.two() {
| ^^^^^^^ mutable borrow occurs here
28 | Some(ref b) => test(b),
| ---- immutable borrow later used here
Note that changing line 20 to
let test = |_t: &Bar| {};
results in a successful compilation (playground link)
Meta
rustc --version --verbose
:
rustc 1.36.0 (a53f9df32 2019-07-03)
binary: rustc
commit-hash: a53f9df32fbb0b5f4382caaad8f1a46f36ea887c
commit-date: 2019-07-03
host: x86_64-pc-windows-msvc
release: 1.36.0
LLVM version: 8.0