Skip to content

Commit bca9973

Browse files
committed
project-cache-issue-31849
1 parent 556939a commit bca9973

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed
Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,82 @@
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+
16
//@ run-pass
2-
// Regression test for #31849: the problem here was actually a performance
3-
// cliff, but I'm adding the test for reference.
47

58
pub trait Upcast<T> {
69
fn upcast(self) -> T;
710
}
811

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>,
1216
{
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+
}
1420
}
1521

16-
impl Upcast<()> for ()
17-
{
18-
fn upcast(self) -> () { () }
22+
impl Upcast<()> for () {
23+
fn upcast(self) -> () {
24+
()
25+
}
1926
}
2027

2128
pub trait ToStatic {
2229
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;
2433
}
2534

2635
impl<T, U> ToStatic for (T, U)
27-
where T: ToStatic,
28-
U: ToStatic
36+
where
37+
T: ToStatic,
38+
U: ToStatic,
2939
{
3040
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+
}
3244
}
3345

34-
impl ToStatic for ()
35-
{
46+
impl ToStatic for () {
3647
type Static = ();
37-
fn to_static(self) -> () { () }
48+
fn to_static(self) -> () {
49+
()
50+
}
3851
}
3952

40-
4153
trait Factory {
4254
type Output;
4355
fn build(&self) -> Self::Output;
4456
}
4557

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>,
5164
{
5265
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+
}
5469
}
5570

5671
impl Factory for () {
5772
type Output = ();
58-
fn build(&self) -> Self::Output { () }
73+
fn build(&self) -> Self::Output {
74+
()
75+
}
5976
}
6077

6178
fn main() {
62-
// More parens, more time.
63-
let it = ((((((((((),()),()),()),()),()),()),()),()),());
79+
// Deeply nested tuple to trigger the original performance issue
80+
let it = ((((((((((), ()), ()), ()), ()), ()), ()), ()), ()), ());
6481
it.build();
6582
}

0 commit comments

Comments
 (0)