New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lifetime error on methods that accept &mut self and return a borrow of self #1
Comments
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)()
}
|
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?The text was updated successfully, but these errors were encountered: