]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-loan-rcvr-overloaded-op.rs
Rollup merge of #106570 - Xaeroxe:div-duration-tests, r=JohnTitor
[rust.git] / tests / ui / borrowck / borrowck-loan-rcvr-overloaded-op.rs
1 use std::ops::Add;
2
3 #[derive(Copy, Clone)]
4 struct Point {
5     x: isize,
6     y: isize,
7 }
8
9 impl Add<isize> for Point {
10     type Output = isize;
11
12     fn add(self, z: isize) -> isize {
13         self.x + self.y + z
14     }
15 }
16
17 impl Point {
18     pub fn times(&self, z: isize) -> isize {
19         self.x * self.y * z
20     }
21 }
22
23 fn a() {
24     let mut p = Point {x: 3, y: 4};
25
26     // ok (we can loan out rcvr)
27     p + 3;
28     p.times(3);
29 }
30
31 fn b() {
32     let mut p = Point {x: 3, y: 4};
33
34     // Here I create an outstanding loan and check that we get conflicts:
35
36     let q = &mut p;
37
38     p + 3;  //~ ERROR cannot use `p`
39     p.times(3); //~ ERROR cannot borrow `p`
40
41     *q + 3; // OK to use the new alias `q`
42     q.x += 1; // and OK to mutate it
43 }
44
45 fn main() {
46 }