3 Given the following code:
9 fn mutate_and_share(&mut self) -> &Self { &*self }
15 let loan = foo.mutate_and_share();
20 One might expect it to compile. We call `mutate_and_share`, which mutably borrows
21 `foo` temporarily, but then returns only a shared reference. Therefore we
22 would expect `foo.share()` to succeed as `foo` shouldn't be mutably borrowed.
24 However when we try to compile it:
27 <anon>:11:5: 11:8 error: cannot borrow `foo` as immutable because it is also borrowed as mutable
28 <anon>:11 foo.share();
30 <anon>:10:16: 10:19 note: previous borrow of `foo` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `foo` until the borrow ends
31 <anon>:10 let loan = foo.mutate_and_share();
33 <anon>:12:2: 12:2 note: previous borrow ends here
35 <anon>:9 let mut foo = Foo;
36 <anon>:10 let loan = foo.mutate_and_share();
37 <anon>:11 foo.share();
42 What happened? Well, we got the exact same reasoning as we did for
43 [Example 2 in the previous section][ex2]. We desugar the program and we get
50 fn mutate_and_share<'a>(&'a mut self) -> &'a Self { &'a *self }
51 fn share<'a>(&'a self) {}
56 let mut foo: Foo = Foo;
58 let loan: &'c Foo = Foo::mutate_and_share::<'c>(&'c mut foo);
60 Foo::share::<'d>(&'d foo);
67 The lifetime system is forced to extend the `&mut foo` to have lifetime `'c`,
68 due to the lifetime of `loan` and mutate_and_share's signature. Then when we
69 try to call `share`, and it sees we're trying to alias that `&'c mut foo` and
72 This program is clearly correct according to the reference semantics we actually
73 care about, but the lifetime system is too coarse-grained to handle that.
76 TODO: other common problems? SEME regions stuff, mostly?
81 [ex2]: lifetimes.html#example-aliasing-a-mutable-reference