]> git.lizzy.rs Git - rust.git/blob - tests/incremental/change_private_impl_method/struct_point.rs
Move /src/test to /tests
[rust.git] / tests / incremental / change_private_impl_method / struct_point.rs
1 // Test where we change the body of a private method in an impl.
2 // We then test what sort of functions must be rebuilt as a result.
3
4 // revisions:cfail1 cfail2
5 // compile-flags: -Z query-dep-graph
6 // build-pass
7
8 #![feature(rustc_attrs)]
9 #![feature(stmt_expr_attributes)]
10 #![allow(dead_code)]
11 #![crate_type = "rlib"]
12
13 #![rustc_partition_codegened(module="struct_point-point", cfg="cfail2")]
14
15 #![rustc_partition_reused(module="struct_point-fn_calls_methods_in_same_impl", cfg="cfail2")]
16 #![rustc_partition_reused(module="struct_point-fn_calls_methods_in_another_impl", cfg="cfail2")]
17 #![rustc_partition_reused(module="struct_point-fn_make_struct", cfg="cfail2")]
18 #![rustc_partition_reused(module="struct_point-fn_read_field", cfg="cfail2")]
19 #![rustc_partition_reused(module="struct_point-fn_write_field", cfg="cfail2")]
20
21 pub mod point {
22     pub struct Point {
23         pub x: f32,
24         pub y: f32,
25     }
26
27     impl Point {
28         pub fn distance_squared(&self) -> f32 {
29             #[cfg(cfail1)]
30             return self.x + self.y;
31
32             #[cfg(cfail2)]
33             return self.x * self.x + self.y * self.y;
34         }
35
36         pub fn distance_from_origin(&self) -> f32 {
37             self.distance_squared().sqrt()
38         }
39     }
40
41     impl Point {
42         pub fn translate(&mut self, x: f32, y: f32) {
43             self.x += x;
44             self.y += y;
45         }
46     }
47
48 }
49
50 /// A fn item that calls (public) methods on `Point` from the same impl which changed
51 pub mod fn_calls_methods_in_same_impl {
52     use point::Point;
53
54     #[rustc_clean(cfg="cfail2")]
55     pub fn check() {
56         let x = Point { x: 2.0, y: 2.0 };
57         x.distance_from_origin();
58     }
59 }
60
61 /// A fn item that calls (public) methods on `Point` from another impl
62 pub mod fn_calls_methods_in_another_impl {
63     use point::Point;
64
65     #[rustc_clean(cfg="cfail2")]
66     pub fn check() {
67         let mut x = Point { x: 2.0, y: 2.0 };
68         x.translate(3.0, 3.0);
69     }
70 }
71
72 /// A fn item that makes an instance of `Point` but does not invoke methods
73 pub mod fn_make_struct {
74     use point::Point;
75
76     #[rustc_clean(cfg="cfail2")]
77     pub fn make_origin() -> Point {
78         Point { x: 2.0, y: 2.0 }
79     }
80 }
81
82 /// A fn item that reads fields from `Point` but does not invoke methods
83 pub mod fn_read_field {
84     use point::Point;
85
86     #[rustc_clean(cfg="cfail2")]
87     pub fn get_x(p: Point) -> f32 {
88         p.x
89     }
90 }
91
92 /// A fn item that writes to a field of `Point` but does not invoke methods
93 pub mod fn_write_field {
94     use point::Point;
95
96     #[rustc_clean(cfg="cfail2")]
97     pub fn inc_x(p: &mut Point) {
98         p.x += 1.0;
99     }
100 }