Skip to content

Commit a88ca4b

Browse files
authored
Rollup merge of rust-lang#37124 - nikomatsakis:incr-comp-benchmark, r=michaelwoerister
add test case for changing private methods The goal of this test case is to ensure we are getting the reuse we expect. This targets a particular change where we modify the body of a private inherent method defined on a struct, and looks at different ways we can use that struct. It checks for when type-checking would be needed as well as the actual reuse achieved. cc rust-lang#37121 r? @michaelwoerister
2 parents 373fcd1 + c560ca4 commit a88ca4b

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
#![allow(dead_code)]
20+
21+
#![rustc_partition_translated(module="struct_point-point", cfg="rpass2")]
22+
23+
// FIXME(#37121) -- the following two modules *should* be reused but are not
24+
#![rustc_partition_translated(module="struct_point-fn_calls_methods_in_same_impl", cfg="rpass2")]
25+
#![rustc_partition_translated(module="struct_point-fn_calls_methods_in_another_impl", cfg="rpass2")]
26+
#![rustc_partition_reused(module="struct_point-fn_make_struct", cfg="rpass2")]
27+
#![rustc_partition_reused(module="struct_point-fn_read_field", cfg="rpass2")]
28+
#![rustc_partition_reused(module="struct_point-fn_write_field", cfg="rpass2")]
29+
30+
mod point {
31+
pub struct Point {
32+
pub x: f32,
33+
pub y: f32,
34+
}
35+
36+
impl Point {
37+
fn distance_squared(&self) -> f32 {
38+
#[cfg(rpass1)]
39+
return self.x + self.y;
40+
41+
#[cfg(rpass2)]
42+
return self.x * self.x + self.y * self.y;
43+
}
44+
45+
pub fn distance_from_origin(&self) -> f32 {
46+
self.distance_squared().sqrt()
47+
}
48+
}
49+
50+
impl Point {
51+
pub fn translate(&mut self, x: f32, y: f32) {
52+
self.x += x;
53+
self.y += y;
54+
}
55+
}
56+
57+
}
58+
59+
/// A fn item that calls (public) methods on `Point` from the same impl which changed
60+
mod fn_calls_methods_in_same_impl {
61+
use point::Point;
62+
63+
// FIXME(#37121) -- we should not need to typeck this again
64+
#[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
65+
pub fn check() {
66+
let x = Point { x: 2.0, y: 2.0 };
67+
x.distance_from_origin();
68+
}
69+
}
70+
71+
/// A fn item that calls (public) methods on `Point` from another impl
72+
mod fn_calls_methods_in_another_impl {
73+
use point::Point;
74+
75+
// FIXME(#37121) -- we should not need to typeck this again
76+
#[rustc_dirty(label="TypeckItemBody", cfg="rpass2")]
77+
pub fn check() {
78+
let mut x = Point { x: 2.0, y: 2.0 };
79+
x.translate(3.0, 3.0);
80+
}
81+
}
82+
83+
/// A fn item that makes an instance of `Point` but does not invoke methods
84+
mod fn_make_struct {
85+
use point::Point;
86+
87+
#[rustc_clean(label="TypeckItemBody", cfg="rpass2")]
88+
pub fn make_origin() -> Point {
89+
Point { x: 2.0, y: 2.0 }
90+
}
91+
}
92+
93+
/// A fn item that reads fields from `Point` but does not invoke methods
94+
mod fn_read_field {
95+
use point::Point;
96+
97+
#[rustc_clean(label="TypeckItemBody", cfg="rpass2")]
98+
pub fn get_x(p: Point) -> f32 {
99+
p.x
100+
}
101+
}
102+
103+
/// A fn item that writes to a field of `Point` but does not invoke methods
104+
mod fn_write_field {
105+
use point::Point;
106+
107+
#[rustc_clean(label="TypeckItemBody", cfg="rpass2")]
108+
pub fn inc_x(p: &mut Point) {
109+
p.x += 1.0;
110+
}
111+
}
112+
113+
fn main() {
114+
}

0 commit comments

Comments
 (0)