]> git.lizzy.rs Git - rust.git/blob - src/test/incremental/change_add_field/struct_point.rs
Rollup merge of #56476 - GuillaumeGomez:invalid-line-number-match, r=QuietMisdreavus
[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:cfail1 cfail2
17 // compile-flags: -Z query-dep-graph
18 // compile-pass
19
20 #![feature(rustc_attrs)]
21 #![feature(stmt_expr_attributes)]
22 #![allow(dead_code)]
23 #![crate_type = "rlib"]
24
25 // These are expected to require codegen.
26 #![rustc_partition_codegened(module="struct_point-point", cfg="cfail2")]
27 #![rustc_partition_codegened(module="struct_point-fn_with_type_in_sig", cfg="cfail2")]
28 #![rustc_partition_codegened(module="struct_point-call_fn_with_type_in_sig", cfg="cfail2")]
29 #![rustc_partition_codegened(module="struct_point-fn_with_type_in_body", cfg="cfail2")]
30 #![rustc_partition_codegened(module="struct_point-fn_make_struct", cfg="cfail2")]
31 #![rustc_partition_codegened(module="struct_point-fn_read_field", cfg="cfail2")]
32 #![rustc_partition_codegened(module="struct_point-fn_write_field", cfg="cfail2")]
33
34 #![rustc_partition_reused(module="struct_point-call_fn_with_type_in_body", cfg="cfail2")]
35
36 pub mod point {
37     #[cfg(cfail1)]
38     pub struct Point {
39         pub x: f32,
40         pub y: f32,
41     }
42
43     #[cfg(cfail2)]
44     pub struct Point {
45         pub x: f32,
46         pub y: f32,
47         pub z: f32,
48     }
49
50     impl Point {
51         pub fn origin() -> Point {
52             #[cfg(cfail1)]
53             return Point { x: 0.0, y: 0.0 };
54
55             #[cfg(cfail2)]
56             return Point { x: 0.0, y: 0.0, z: 0.0 };
57         }
58
59         pub fn total(&self) -> f32 {
60             #[cfg(cfail1)]
61             return self.x + self.y;
62
63             #[cfg(cfail2)]
64             return self.x + self.y + self.z;
65         }
66
67         pub fn x(&self) -> f32 {
68             self.x
69         }
70     }
71 }
72
73 /// A fn that has the changed type in its signature; must currently be
74 /// rebuilt.
75 ///
76 /// You could imagine that, in the future, if the change were
77 /// sufficiently "private", we might not need to type-check again.
78 /// Rebuilding is probably always necessary since the layout may be
79 /// affected.
80 pub mod fn_with_type_in_sig {
81     use point::Point;
82
83     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
84     pub fn boop(p: Option<&Point>) -> f32 {
85         p.map(|p| p.total()).unwrap_or(0.0)
86     }
87 }
88
89 /// Call a fn that has the changed type in its signature; this
90 /// currently must also be rebuilt.
91 ///
92 /// You could imagine that, in the future, if the change were
93 /// sufficiently "private", we might not need to type-check again.
94 /// Rebuilding is probably always necessary since the layout may be
95 /// affected.
96 pub mod call_fn_with_type_in_sig {
97     use fn_with_type_in_sig;
98
99     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
100     pub fn bip() -> f32 {
101         fn_with_type_in_sig::boop(None)
102     }
103 }
104
105 /// A fn that uses the changed type, but only in its body, not its
106 /// signature.
107 ///
108 /// You could imagine that, in the future, if the change were
109 /// sufficiently "private", we might not need to type-check again.
110 /// Rebuilding is probably always necessary since the layout may be
111 /// affected.
112 pub mod fn_with_type_in_body {
113     use point::Point;
114
115     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
116     pub fn boop() -> f32 {
117         Point::origin().total()
118     }
119 }
120
121 /// A fn X that calls a fn Y, where Y uses the changed type in its
122 /// body. In this case, the effects of the change should be contained
123 /// to Y; X should not have to be rebuilt, nor should it need to be
124 /// typechecked again.
125 pub mod call_fn_with_type_in_body {
126     use fn_with_type_in_body;
127
128     #[rustc_clean(label="TypeckTables", cfg="cfail2")]
129     pub fn bip() -> f32 {
130         fn_with_type_in_body::boop()
131     }
132 }
133
134 /// A fn item that makes an instance of `Point` but does not invoke methods
135 pub mod fn_make_struct {
136     use point::Point;
137
138     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
139     pub fn make_origin(p: Point) -> Point {
140         Point { ..p }
141     }
142 }
143
144 /// A fn item that reads fields from `Point` but does not invoke methods
145 pub mod fn_read_field {
146     use point::Point;
147
148     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
149     pub fn get_x(p: Point) -> f32 {
150         p.x
151     }
152 }
153
154 /// A fn item that writes to a field of `Point` but does not invoke methods
155 pub mod fn_write_field {
156     use point::Point;
157
158     #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
159     pub fn inc_x(p: &mut Point) {
160         p.x += 1.0;
161     }
162 }