]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-object-lifetime.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-object-lifetime.rs
1 // Test that borrows that occur due to calls to object methods
2 // properly "claim" the object path.
3
4
5
6 trait Foo {
7     fn borrowed(&self) -> &();
8     fn mut_borrowed(&mut self) -> &();
9 }
10
11 fn borrowed_receiver(x: &dyn Foo) {
12     let y = x.borrowed();
13     let z = x.borrowed();
14     z.use_ref();
15     y.use_ref();
16 }
17
18 fn mut_borrowed_receiver(x: &mut dyn Foo) {
19     let y = x.borrowed();
20     let z = x.mut_borrowed(); //~ ERROR cannot borrow
21     y.use_ref();
22 }
23
24 fn mut_owned_receiver(mut x: Box<dyn Foo>) {
25     let y = x.borrowed();
26     let z = &mut x; //~ ERROR cannot borrow
27     y.use_ref();
28 }
29
30 fn imm_owned_receiver(mut x: Box<dyn Foo>) {
31     let y = x.borrowed();
32     let z = &x;
33     z.use_ref();
34     y.use_ref();
35 }
36
37 fn main() {}
38
39 trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { }  }
40 impl<T> Fake for T { }