/src/test/incremental/change_pub_inherent_method_sig/struct_point.rs

https://gitlab.com/jianglu/rust · Rust · 111 lines · 73 code · 19 blank · 19 comment · 0 complexity · 05b585dc12b8c6fb3f57faf27a9d662e 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 *signature* of a public, inherent method.
  11. // revisions:cfail1 cfail2
  12. // compile-flags: -Z query-dep-graph
  13. // compile-pass
  14. #![crate_type = "rlib"]
  15. #![feature(rustc_attrs)]
  16. #![feature(stmt_expr_attributes)]
  17. #![allow(dead_code)]
  18. // These are expected to require codegen.
  19. #![rustc_partition_codegened(module="struct_point-point", cfg="cfail2")]
  20. #![rustc_partition_codegened(module="struct_point-fn_calls_changed_method", cfg="cfail2")]
  21. #![rustc_partition_reused(module="struct_point-fn_calls_another_method", 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. impl Point {
  31. #[cfg(cfail1)]
  32. pub fn distance_from_point(&self, p: Option<Point>) -> f32 {
  33. let p = p.unwrap_or(Point { x: 0.0, y: 0.0 });
  34. let x_diff = self.x - p.x;
  35. let y_diff = self.y - p.y;
  36. return x_diff * x_diff + y_diff * y_diff;
  37. }
  38. #[cfg(cfail2)]
  39. pub fn distance_from_point(&self, p: Option<&Point>) -> f32 {
  40. const ORIGIN: &Point = &Point { x: 0.0, y: 0.0 };
  41. let p = p.unwrap_or(ORIGIN);
  42. let x_diff = self.x - p.x;
  43. let y_diff = self.y - p.y;
  44. return x_diff * x_diff + y_diff * y_diff;
  45. }
  46. pub fn x(&self) -> f32 {
  47. self.x
  48. }
  49. }
  50. }
  51. /// A fn item that calls the method that was changed
  52. pub mod fn_calls_changed_method {
  53. use point::Point;
  54. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  55. pub fn check() {
  56. let p = Point { x: 2.0, y: 2.0 };
  57. p.distance_from_point(None);
  58. }
  59. }
  60. /// A fn item that calls a method that was not changed
  61. pub mod fn_calls_another_method {
  62. use point::Point;
  63. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  64. pub fn check() {
  65. let p = Point { x: 2.0, y: 2.0 };
  66. p.x();
  67. }
  68. }
  69. /// A fn item that makes an instance of `Point` but does not invoke methods
  70. pub mod fn_make_struct {
  71. use point::Point;
  72. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  73. pub fn make_origin() -> Point {
  74. Point { x: 2.0, y: 2.0 }
  75. }
  76. }
  77. /// A fn item that reads fields from `Point` but does not invoke methods
  78. pub mod fn_read_field {
  79. use point::Point;
  80. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  81. pub fn get_x(p: Point) -> f32 {
  82. p.x
  83. }
  84. }
  85. /// A fn item that writes to a field of `Point` but does not invoke methods
  86. pub mod fn_write_field {
  87. use point::Point;
  88. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  89. pub fn inc_x(p: &mut Point) {
  90. p.x += 1.0;
  91. }
  92. }