/src/test/incremental/change_private_fn/struct_point.rs

https://gitlab.com/jianglu/rust · Rust · 110 lines · 70 code · 21 blank · 19 comment · 0 complexity · 2d46f370a54fc203daa2b66f6d0b01b8 MD5 · raw file

  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. // Test where we change the body of a private method in an impl.
  11. // We then test what sort of functions must be rebuilt as a result.
  12. // revisions:cfail1 cfail2
  13. // compile-flags: -Z query-dep-graph
  14. // compile-pass
  15. #![feature(rustc_attrs)]
  16. #![feature(stmt_expr_attributes)]
  17. #![allow(dead_code)]
  18. #![crate_type = "rlib"]
  19. #![rustc_partition_codegened(module="struct_point-point", cfg="cfail2")]
  20. #![rustc_partition_reused(module="struct_point-fn_calls_methods_in_same_impl", cfg="cfail2")]
  21. #![rustc_partition_reused(module="struct_point-fn_calls_methods_in_another_impl", cfg="cfail2")]
  22. #![rustc_partition_reused(module="struct_point-fn_make_struct", cfg="cfail2")]
  23. #![rustc_partition_reused(module="struct_point-fn_read_field", cfg="cfail2")]
  24. #![rustc_partition_reused(module="struct_point-fn_write_field", cfg="cfail2")]
  25. pub mod point {
  26. pub struct Point {
  27. pub x: f32,
  28. pub y: f32,
  29. }
  30. fn distance_squared(this: &Point) -> f32 {
  31. #[cfg(cfail1)]
  32. return this.x + this.y;
  33. #[cfg(cfail2)]
  34. return this.x * this.x + this.y * this.y;
  35. }
  36. impl Point {
  37. pub fn distance_from_origin(&self) -> f32 {
  38. distance_squared(self).sqrt()
  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. /// A fn item that calls (public) methods on `Point` from the same impl which changed
  49. pub mod fn_calls_methods_in_same_impl {
  50. use point::Point;
  51. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  52. pub fn check() {
  53. let x = Point { x: 2.0, y: 2.0 };
  54. x.distance_from_origin();
  55. }
  56. }
  57. /// A fn item that calls (public) methods on `Point` from another impl
  58. pub mod fn_calls_methods_in_another_impl {
  59. use point::Point;
  60. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  61. pub fn check() {
  62. let mut x = Point { x: 2.0, y: 2.0 };
  63. x.translate(3.0, 3.0);
  64. }
  65. }
  66. /// A fn item that makes an instance of `Point` but does not invoke methods
  67. pub mod fn_make_struct {
  68. use point::Point;
  69. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  70. pub fn make_origin() -> Point {
  71. Point { x: 2.0, y: 2.0 }
  72. }
  73. }
  74. /// A fn item that reads fields from `Point` but does not invoke methods
  75. pub mod fn_read_field {
  76. use point::Point;
  77. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  78. pub fn get_x(p: Point) -> f32 {
  79. p.x
  80. }
  81. }
  82. /// A fn item that writes to a field of `Point` but does not invoke methods
  83. pub mod fn_write_field {
  84. use point::Point;
  85. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  86. pub fn inc_x(p: &mut Point) {
  87. p.x += 1.0;
  88. }
  89. }