Can't take self as mutable because it is also borrowed as immutable
I want the struct function to call itself under special conditions. It worked when I had HashMap
both one of the fields, but it broke when I changed HashMap
to Vec
. It doesn't even need to be used, which seems very strange, and I can't find a reasonable explanation for this.
use std::vec::Vec;
use std::collections::HashMap;
struct Foo<'a> {
bar: Vec<&'a str>
//bar: HashMap<&'a str, &'a str>
}
impl<'a> Foo<'a> {
pub fn new() -> Foo<'a> {
Foo { bar: Vec::new() }
//Foo { bar: HashMap::new() }
}
pub fn baz(&'a self) -> Option<int> {
None
}
pub fn qux(&'a mut self, retry: bool) {
let opt = self.baz();
if retry { self.qux(false); }
}
}
pub fn main() {
let mut foo = Foo::new();
foo.qux(true);
}
playpen: http://is.gd/GgMy79
Mistake:
<anon>:22:24: 22:28 error: cannot borrow `*self` as mutable because it is also borrowed as immutable
<anon>:22 if retry { self.qux(false); }
^~~~
<anon>:21:23: 21:27 note: previous borrow of `*self` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `*self` until the borrow ends
<anon>:21 let opt = self.baz();
^~~~
<anon>:23:10: 23:10 note: previous borrow ends here
<anon>:20 pub fn qux(&'a mut self, retry: bool) {
<anon>:21 let opt = self.baz();
<anon>:22 if retry { self.qux(false); }
<anon>:23 }
How can I fix this? Could this be caused by # 6268 ?
source to share
I think I found the reason. This definition HashMap
:
pub struct HashMap<K, V, H = RandomSipHasher> {
// All hashes are keyed on these values, to prevent hash collision attacks.
hasher: H,
table: RawTable<K, V>,
// We keep this at the end since it might as well have tail padding.
resize_policy: DefaultResizePolicy,
}
This definition Vec
:
pub struct Vec<T> {
ptr: *mut T,
len: uint,
cap: uint,
}
The only difference is how the type parameters are used. Now let's check this code:
struct S1<T> { s: Option<T> }
//struct S1<T> { s: *mut T }
struct Foo<'a> {
bar: S1<&'a str>
}
impl<'a> Foo<'a> {
pub fn new() -> Foo<'a> { // '
Foo { bar: S1 { s: None } }
//Foo { bar: S1 { s: std::ptr::null_mut() } }
}
pub fn baz(&'a self) -> Option<int> {
None
}
pub fn qux(&'a mut self, retry: bool) {
let opt = self.baz();
if retry { self.qux(false); }
}
}
pub fn main() {
let mut foo = Foo::new();
foo.qux(true);
}
This one compiles. If you choose a different definition for S1
, with a pointer *mut T
, then the program will fail.
I think it looks like a bug, somewhere around the variation over a lifetime.
Update posted here
source to share