/src/test/incremental/change_add_field/struct_point.rs

https://gitlab.com/jianglu/rust · Rust · 162 lines · 91 code · 26 blank · 45 comment · 0 complexity · 5ccc64a6a8c4501db4c88fd563f44904 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 a type definition by adding a field. Fns with
  11. // this type in their signature are recompiled, as are their callers.
  12. // Fns with that type used only in their body are also recompiled, but
  13. // their callers are not.
  14. // revisions:cfail1 cfail2
  15. // compile-flags: -Z query-dep-graph
  16. // compile-pass
  17. #![feature(rustc_attrs)]
  18. #![feature(stmt_expr_attributes)]
  19. #![allow(dead_code)]
  20. #![crate_type = "rlib"]
  21. // These are expected to require codegen.
  22. #![rustc_partition_codegened(module="struct_point-point", cfg="cfail2")]
  23. #![rustc_partition_codegened(module="struct_point-fn_with_type_in_sig", cfg="cfail2")]
  24. #![rustc_partition_codegened(module="struct_point-call_fn_with_type_in_sig", cfg="cfail2")]
  25. #![rustc_partition_codegened(module="struct_point-fn_with_type_in_body", cfg="cfail2")]
  26. #![rustc_partition_codegened(module="struct_point-fn_make_struct", cfg="cfail2")]
  27. #![rustc_partition_codegened(module="struct_point-fn_read_field", cfg="cfail2")]
  28. #![rustc_partition_codegened(module="struct_point-fn_write_field", cfg="cfail2")]
  29. #![rustc_partition_reused(module="struct_point-call_fn_with_type_in_body", cfg="cfail2")]
  30. pub mod point {
  31. #[cfg(cfail1)]
  32. pub struct Point {
  33. pub x: f32,
  34. pub y: f32,
  35. }
  36. #[cfg(cfail2)]
  37. pub struct Point {
  38. pub x: f32,
  39. pub y: f32,
  40. pub z: f32,
  41. }
  42. impl Point {
  43. pub fn origin() -> Point {
  44. #[cfg(cfail1)]
  45. return Point { x: 0.0, y: 0.0 };
  46. #[cfg(cfail2)]
  47. return Point { x: 0.0, y: 0.0, z: 0.0 };
  48. }
  49. pub fn total(&self) -> f32 {
  50. #[cfg(cfail1)]
  51. return self.x + self.y;
  52. #[cfg(cfail2)]
  53. return self.x + self.y + self.z;
  54. }
  55. pub fn x(&self) -> f32 {
  56. self.x
  57. }
  58. }
  59. }
  60. /// A fn that has the changed type in its signature; must currently be
  61. /// rebuilt.
  62. ///
  63. /// You could imagine that, in the future, if the change were
  64. /// sufficiently "private", we might not need to type-check again.
  65. /// Rebuilding is probably always necessary since the layout may be
  66. /// affected.
  67. pub mod fn_with_type_in_sig {
  68. use point::Point;
  69. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  70. pub fn boop(p: Option<&Point>) -> f32 {
  71. p.map(|p| p.total()).unwrap_or(0.0)
  72. }
  73. }
  74. /// Call a fn that has the changed type in its signature; this
  75. /// currently must also be rebuilt.
  76. ///
  77. /// You could imagine that, in the future, if the change were
  78. /// sufficiently "private", we might not need to type-check again.
  79. /// Rebuilding is probably always necessary since the layout may be
  80. /// affected.
  81. pub mod call_fn_with_type_in_sig {
  82. use fn_with_type_in_sig;
  83. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  84. pub fn bip() -> f32 {
  85. fn_with_type_in_sig::boop(None)
  86. }
  87. }
  88. /// A fn that uses the changed type, but only in its body, not its
  89. /// signature.
  90. ///
  91. /// You could imagine that, in the future, if the change were
  92. /// sufficiently "private", we might not need to type-check again.
  93. /// Rebuilding is probably always necessary since the layout may be
  94. /// affected.
  95. pub mod fn_with_type_in_body {
  96. use point::Point;
  97. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  98. pub fn boop() -> f32 {
  99. Point::origin().total()
  100. }
  101. }
  102. /// A fn X that calls a fn Y, where Y uses the changed type in its
  103. /// body. In this case, the effects of the change should be contained
  104. /// to Y; X should not have to be rebuilt, nor should it need to be
  105. /// typechecked again.
  106. pub mod call_fn_with_type_in_body {
  107. use fn_with_type_in_body;
  108. #[rustc_clean(label="TypeckTables", cfg="cfail2")]
  109. pub fn bip() -> f32 {
  110. fn_with_type_in_body::boop()
  111. }
  112. }
  113. /// A fn item that makes an instance of `Point` but does not invoke methods
  114. pub mod fn_make_struct {
  115. use point::Point;
  116. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  117. pub fn make_origin(p: Point) -> Point {
  118. Point { ..p }
  119. }
  120. }
  121. /// A fn item that reads fields from `Point` but does not invoke methods
  122. pub mod fn_read_field {
  123. use point::Point;
  124. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  125. pub fn get_x(p: Point) -> f32 {
  126. p.x
  127. }
  128. }
  129. /// A fn item that writes to a field of `Point` but does not invoke methods
  130. pub mod fn_write_field {
  131. use point::Point;
  132. #[rustc_dirty(label="TypeckTables", cfg="cfail2")]
  133. pub fn inc_x(p: &mut Point) {
  134. p.x += 1.0;
  135. }
  136. }