]> git.lizzy.rs Git - rust.git/blob - src/test/incremental/change_add_field/struct_point.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / test / incremental / change_add_field / struct_point.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test where we change a type definition by adding a field.  Fns with
12 // this type in their signature are recompiled, as are their callers.
13 // Fns with that type used only in their body are also recompiled, but
14 // their callers are not.
15
16 // revisions:rpass1 rpass2
17 // compile-flags: -Z query-dep-graph
18
19 #![feature(rustc_attrs)]
20 #![feature(stmt_expr_attributes)]
21 #![allow(dead_code)]
22
23 // These are expected to require translation.
24 #![rustc_partition_translated(module="struct_point-point", cfg="rpass2")]
25 #![rustc_partition_translated(module="struct_point-fn_with_type_in_sig", cfg="rpass2")]
26 #![rustc_partition_translated(module="struct_point-call_fn_with_type_in_sig", cfg="rpass2")]
27 #![rustc_partition_translated(module="struct_point-fn_with_type_in_body", cfg="rpass2")]
28 #![rustc_partition_translated(module="struct_point-fn_make_struct", cfg="rpass2")]
29 #![rustc_partition_translated(module="struct_point-fn_read_field", cfg="rpass2")]
30 #![rustc_partition_translated(module="struct_point-fn_write_field", cfg="rpass2")]
31
32 #![rustc_partition_reused(module="struct_point-call_fn_with_type_in_body", cfg="rpass2")]
33
34 mod point {
35     #[cfg(rpass1)]
36     pub struct Point {
37         pub x: f32,
38         pub y: f32,
39     }
40
41     #[cfg(rpass2)]
42     pub struct Point {
43         pub x: f32,
44         pub y: f32,
45         pub z: f32,
46     }
47
48     impl Point {
49         pub fn origin() -> Point {
50             #[cfg(rpass1)]
51             return Point { x: 0.0, y: 0.0 };
52
53             #[cfg(rpass2)]
54             return Point { x: 0.0, y: 0.0, z: 0.0 };
55         }
56
57         pub fn total(&self) -> f32 {
58             #[cfg(rpass1)]
59             return self.x + self.y;
60
61             #[cfg(rpass2)]
62             return self.x + self.y + self.z;
63         }
64
65         pub fn x(&self) -> f32 {
66             self.x
67         }
68     }
69 }
70
71 /// A fn that has the changed type in its signature; must currently be
72 /// rebuilt.
73 ///
74 /// You could imagine that, in the future, if the change were
75 /// sufficiently "private", we might not need to type-check again.
76 /// Rebuilding is probably always necessary since the layout may be
77 /// affected.
78 mod fn_with_type_in_sig {
79     use point::Point;
80
81     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
82     pub fn boop(p: Option<&Point>) -> f32 {
83         p.map(|p| p.total()).unwrap_or(0.0)
84     }
85 }
86
87 /// Call a fn that has the changed type in its signature; this
88 /// currently must also be rebuilt.
89 ///
90 /// You could imagine that, in the future, if the change were
91 /// sufficiently "private", we might not need to type-check again.
92 /// Rebuilding is probably always necessary since the layout may be
93 /// affected.
94 mod call_fn_with_type_in_sig {
95     use fn_with_type_in_sig;
96
97     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
98     pub fn bip() -> f32 {
99         fn_with_type_in_sig::boop(None)
100     }
101 }
102
103 /// A fn that uses the changed type, but only in its body, not its
104 /// signature.
105 ///
106 /// You could imagine that, in the future, if the change were
107 /// sufficiently "private", we might not need to type-check again.
108 /// Rebuilding is probably always necessary since the layout may be
109 /// affected.
110 mod fn_with_type_in_body {
111     use point::Point;
112
113     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
114     pub fn boop() -> f32 {
115         Point::origin().total()
116     }
117 }
118
119 /// A fn X that calls a fn Y, where Y uses the changed type in its
120 /// body. In this case, the effects of the change should be contained
121 /// to Y; X should not have to be rebuilt, nor should it need to be
122 /// typechecked again.
123 mod call_fn_with_type_in_body {
124     use fn_with_type_in_body;
125
126     #[rustc_clean(label="TypeckTables", cfg="rpass2")]
127     pub fn bip() -> f32 {
128         fn_with_type_in_body::boop()
129     }
130 }
131
132 /// A fn item that makes an instance of `Point` but does not invoke methods
133 mod fn_make_struct {
134     use point::Point;
135
136     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
137     pub fn make_origin(p: Point) -> Point {
138         Point { ..p }
139     }
140 }
141
142 /// A fn item that reads fields from `Point` but does not invoke methods
143 mod fn_read_field {
144     use point::Point;
145
146     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
147     pub fn get_x(p: Point) -> f32 {
148         p.x
149     }
150 }
151
152 /// A fn item that writes to a field of `Point` but does not invoke methods
153 mod fn_write_field {
154     use point::Point;
155
156     #[rustc_dirty(label="TypeckTables", cfg="rpass2")]
157     pub fn inc_x(p: &mut Point) {
158         p.x += 1.0;
159     }
160 }
161
162 fn main() {
163 }