|
| 1 | +//! Test that deeply nested generic traits with complex bounds |
| 2 | +//! don't cause excessive memory usage during type checking. |
| 3 | +//! |
| 4 | +//! Regression test for <https://github.com/rust-lang/rust/issues/31849>. |
| 5 | +
|
1 | 6 | //@ run-pass |
2 | | -// Regression test for #31849: the problem here was actually a performance |
3 | | -// cliff, but I'm adding the test for reference. |
4 | 7 |
|
5 | 8 | pub trait Upcast<T> { |
6 | 9 | fn upcast(self) -> T; |
7 | 10 | } |
8 | 11 |
|
9 | | -impl<S1, S2, T1, T2> Upcast<(T1, T2)> for (S1,S2) |
10 | | - where S1: Upcast<T1>, |
11 | | - S2: Upcast<T2>, |
| 12 | +impl<S1, S2, T1, T2> Upcast<(T1, T2)> for (S1, S2) |
| 13 | +where |
| 14 | + S1: Upcast<T1>, |
| 15 | + S2: Upcast<T2>, |
12 | 16 | { |
13 | | - fn upcast(self) -> (T1, T2) { (self.0.upcast(), self.1.upcast()) } |
| 17 | + fn upcast(self) -> (T1, T2) { |
| 18 | + (self.0.upcast(), self.1.upcast()) |
| 19 | + } |
14 | 20 | } |
15 | 21 |
|
16 | | -impl Upcast<()> for () |
17 | | -{ |
18 | | - fn upcast(self) -> () { () } |
| 22 | +impl Upcast<()> for () { |
| 23 | + fn upcast(self) -> () { |
| 24 | + () |
| 25 | + } |
19 | 26 | } |
20 | 27 |
|
21 | 28 | pub trait ToStatic { |
22 | 29 | type Static: 'static; |
23 | | - fn to_static(self) -> Self::Static where Self: Sized; |
| 30 | + fn to_static(self) -> Self::Static |
| 31 | + where |
| 32 | + Self: Sized; |
24 | 33 | } |
25 | 34 |
|
26 | 35 | impl<T, U> ToStatic for (T, U) |
27 | | - where T: ToStatic, |
28 | | - U: ToStatic |
| 36 | +where |
| 37 | + T: ToStatic, |
| 38 | + U: ToStatic, |
29 | 39 | { |
30 | 40 | type Static = (T::Static, U::Static); |
31 | | - fn to_static(self) -> Self::Static { (self.0.to_static(), self.1.to_static()) } |
| 41 | + fn to_static(self) -> Self::Static { |
| 42 | + (self.0.to_static(), self.1.to_static()) |
| 43 | + } |
32 | 44 | } |
33 | 45 |
|
34 | | -impl ToStatic for () |
35 | | -{ |
| 46 | +impl ToStatic for () { |
36 | 47 | type Static = (); |
37 | | - fn to_static(self) -> () { () } |
| 48 | + fn to_static(self) -> () { |
| 49 | + () |
| 50 | + } |
38 | 51 | } |
39 | 52 |
|
40 | | - |
41 | 53 | trait Factory { |
42 | 54 | type Output; |
43 | 55 | fn build(&self) -> Self::Output; |
44 | 56 | } |
45 | 57 |
|
46 | | -impl<S,T> Factory for (S, T) |
47 | | - where S: Factory, |
48 | | - T: Factory, |
49 | | - S::Output: ToStatic, |
50 | | - <S::Output as ToStatic>::Static: Upcast<S::Output>, |
| 58 | +impl<S, T> Factory for (S, T) |
| 59 | +where |
| 60 | + S: Factory, |
| 61 | + T: Factory, |
| 62 | + S::Output: ToStatic, |
| 63 | + <S::Output as ToStatic>::Static: Upcast<S::Output>, |
51 | 64 | { |
52 | 65 | type Output = (S::Output, T::Output); |
53 | | - fn build(&self) -> Self::Output { (self.0.build().to_static().upcast(), self.1.build()) } |
| 66 | + fn build(&self) -> Self::Output { |
| 67 | + (self.0.build().to_static().upcast(), self.1.build()) |
| 68 | + } |
54 | 69 | } |
55 | 70 |
|
56 | 71 | impl Factory for () { |
57 | 72 | type Output = (); |
58 | | - fn build(&self) -> Self::Output { () } |
| 73 | + fn build(&self) -> Self::Output { |
| 74 | + () |
| 75 | + } |
59 | 76 | } |
60 | 77 |
|
61 | 78 | fn main() { |
62 | | - // More parens, more time. |
63 | | - let it = ((((((((((),()),()),()),()),()),()),()),()),()); |
| 79 | + // Deeply nested tuple to trigger the original performance issue |
| 80 | + let it = ((((((((((), ()), ()), ()), ()), ()), ()), ()), ()), ()); |
64 | 81 | it.build(); |
65 | 82 | } |
0 commit comments