|
| 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 the body of a private method in an impl. |
| 12 | +// We then test what sort of functions must be rebuilt as a result. |
| 13 | + |
| 14 | +// revisions:rpass1 rpass2 |
| 15 | +// compile-flags: -Z query-dep-graph |
| 16 | + |
| 17 | +#![feature(rustc_attrs)] |
| 18 | +#![feature(stmt_expr_attributes)] |
| 19 | +#![feature(static_in_const)] |
| 20 | +#![allow(dead_code)] |
| 21 | + |
| 22 | +// These are expected to require translation. |
| 23 | +#![rustc_partition_translated(module="struct_point-point", cfg="rpass2")] |
| 24 | +#![rustc_partition_translated(module="struct_point-fn_calls_changed_method", cfg="rpass2")] |
| 25 | + |
| 26 | +// FIXME(#36349) -- this gets recompiled because we don't separate items from impl |
| 27 | +#![rustc_partition_translated(module="struct_point-fn_calls_another_method", cfg="rpass2")] |
| 28 | + |
| 29 | +#![rustc_partition_reused(module="struct_point-fn_make_struct", cfg="rpass2")] |
| 30 | +#![rustc_partition_reused(module="struct_point-fn_read_field", cfg="rpass2")] |
| 31 | +#![rustc_partition_reused(module="struct_point-fn_write_field", cfg="rpass2")] |
| 32 | + |
| 33 | +mod point { |
| 34 | + pub struct Point { |
| 35 | + pub x: f32, |
| 36 | + pub y: f32, |
| 37 | + } |
| 38 | + |
| 39 | + impl Point { |
| 40 | + #[cfg(rpass1)] |
| 41 | + pub fn distance_from_point(&self, p: Option<Point>) -> f32 { |
| 42 | + let p = p.unwrap_or(Point { x: 0.0, y: 0.0 }); |
| 43 | + let x_diff = self.x - p.x; |
| 44 | + let y_diff = self.y - p.y; |
| 45 | + return x_diff * x_diff + y_diff * y_diff; |
| 46 | + } |
| 47 | + |
| 48 | + #[cfg(rpass2)] |
| 49 | + pub fn distance_from_point(&self, p: Option<&Point>) -> f32 { |
| 50 | + const ORIGIN: &Point = &Point { x: 0.0, y: 0.0 }; |
| 51 | + let p = p.unwrap_or(ORIGIN); |
| 52 | + let x_diff = self.x - p.x; |
| 53 | + let y_diff = self.y - p.y; |
| 54 | + return x_diff * x_diff + y_diff * y_diff; |
| 55 | + } |
| 56 | + |
| 57 | + pub fn x(&self) -> f32 { |
| 58 | + self.x |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// A fn item that calls (public) methods on `Point` from the same impl which changed |
| 64 | +mod fn_calls_changed_method { |
| 65 | + use point::Point; |
| 66 | + |
| 67 | + #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")] |
| 68 | + pub fn check() { |
| 69 | + let p = Point { x: 2.0, y: 2.0 }; |
| 70 | + p.distance_from_point(None); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// A fn item that calls (public) methods on `Point` from the same impl which changed |
| 75 | +mod fn_calls_another_method { |
| 76 | + use point::Point; |
| 77 | + |
| 78 | + // FIXME(#36349) -- this gets recompiled because we don't separate items from impl |
| 79 | + #[rustc_dirty(label="TypeckItemBody", cfg="rpass2")] |
| 80 | + pub fn check() { |
| 81 | + let p = Point { x: 2.0, y: 2.0 }; |
| 82 | + p.x(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// A fn item that makes an instance of `Point` but does not invoke methods |
| 87 | +mod fn_make_struct { |
| 88 | + use point::Point; |
| 89 | + |
| 90 | + #[rustc_clean(label="TypeckItemBody", cfg="rpass2")] |
| 91 | + pub fn make_origin() -> Point { |
| 92 | + Point { x: 2.0, y: 2.0 } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// A fn item that reads fields from `Point` but does not invoke methods |
| 97 | +mod fn_read_field { |
| 98 | + use point::Point; |
| 99 | + |
| 100 | + #[rustc_clean(label="TypeckItemBody", cfg="rpass2")] |
| 101 | + pub fn get_x(p: Point) -> f32 { |
| 102 | + p.x |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// A fn item that writes to a field of `Point` but does not invoke methods |
| 107 | +mod fn_write_field { |
| 108 | + use point::Point; |
| 109 | + |
| 110 | + #[rustc_clean(label="TypeckItemBody", cfg="rpass2")] |
| 111 | + pub fn inc_x(p: &mut Point) { |
| 112 | + p.x += 1.0; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +fn main() { |
| 117 | +} |
0 commit comments