Closed saethlin closed 6 years ago
Seems like a borrow checker limitation! For now you can work around it by writing:
#[no_panic]
fn get_mut(&mut self) -> &mut usize {
&mut {self}.data
}
I would love a PR to fix this.
Can you explain to me how that fix works? I think this is way beyond me.
For move closures there appears to be a difference between reborrowing from the input reference, in which case the reborrow lives only as long as the closure, vs moving ownership of the original borrow into the closure, which lives as long as the input reference. Curly braces result in the value being moved rather than reborrowed.
struct S {
data: usize,
}
fn does_not_compile(s: &mut S) -> &mut usize {
(move || &mut s.data)()
}
fn ok(s: &mut S) -> &mut usize {
(move || &mut {s}.data)()
}
#[no_panic]
internally uses a move closure to insert a check just before the function returns for whether it is returning normally or unwinding from a panic.
with NLLs I see this (the current borrow checker message is very verbose):
And if change the return type to be a
&usize
, a slight variation on itTo be honest, I'm at a loss to explain this. Everything is fine if the method accepts
&self
. Is this a limitation of the existing borrow checkers?