Skip to content

Commit 35f0aa9

Browse files
authored
Rollup merge of #158392 - aerooneqq:delegation-defaults-in-generics-2, r=petrochenkov
delegation: add tests for defaults and infers in generics This PR adds tests for interaction of default generic params and infers in delegation. Moreover, it groups delegation's pretty tests into a single folder and adds one test for generics. Comments in tests explain the behavior. Part of #118212. r? @petrochenkov
2 parents 21c60a8 + dad52d6 commit 35f0aa9

14 files changed

Lines changed: 399 additions & 8 deletions
File renamed without changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//@ pretty-compare-only
2+
//@ pretty-mode:hir
3+
//@ pp-exact:generics.pp
4+
5+
#![allow(incomplete_features)]
6+
#![attr = Feature([fn_delegation#0])]
7+
extern crate std;
8+
#[attr = PreludeImport]
9+
use ::std::prelude::rust_2015::*;
10+
11+
mod free_to_trait {
12+
trait Trait<'a, XX, Y, T = (), const N: usize = 2> {
13+
fn method<A, B>(&self, t: (T, A, B), slice: &'_ [usize; N]) { }
14+
fn r#static<A, B>(t: (T, A, B), slice: &'_ [usize; N]) { }
15+
}
16+
17+
struct X;
18+
impl <XX, Y, T, const N: usize> Trait<'_, XX, Y, T, N> for X { }
19+
20+
// When infer is specified for default parameter the generic param is generated.
21+
#[attr = Inline(Hint)]
22+
fn foo<'a, Self, XX, Y, T, const N: _, A, B>(self: _, arg1: _, arg2: _)
23+
-> _ where
24+
'a:'a {
25+
<Self as Trait::<'a, XX, Y, T, N>>::method::<A, B>(self, arg1, arg2)
26+
}
27+
#[attr = Inline(Hint)]
28+
fn static_foo<'a, Self, XX, Y, T, const N: _, A, B>(arg0: _, arg1: _) -> _
29+
where
30+
'a:'a {
31+
<Self as Trait::<'a, XX, Y, T, N>>::r#static::<A, B>(arg0, arg1)
32+
}
33+
34+
// When default params are omitted they are not generated but used in signature inheritance.
35+
#[attr = Inline(Hint)]
36+
fn bar<'a, Self, XX, Y, A, B>(self: _, arg1: _, arg2: _) -> _ where
37+
'a:'a {
38+
<Self as Trait::<'a, XX, Y>>::method::<A, B>(self, arg1, arg2)
39+
}
40+
#[attr = Inline(Hint)]
41+
fn static_bar<'a, Self, XX, Y, A, B>(arg0: _, arg1: _) -> _ where
42+
'a:'a { <Self as Trait::<'a, XX, Y>>::r#static::<A, B>(arg0, arg1) }
43+
44+
// Check with user specified args in child:
45+
// When infer is specified for default parameter the generic param is generated.
46+
#[attr = Inline(Hint)]
47+
fn foo1<'a, Self, XX, Y, T, const N: _, B>(self: _, arg1: _, arg2: _) -> _
48+
where
49+
'a:'a {
50+
<Self as Trait::<'a, XX, Y, T, N>>::method::<(), B>(self, arg1, arg2)
51+
}
52+
#[attr = Inline(Hint)]
53+
fn static_foo1<'a, Self, XX, Y, T, const N: _, B>(arg0: _, arg1: _) -> _
54+
where
55+
'a:'a {
56+
<Self as Trait::<'a, XX, Y, T, N>>::r#static::<(), B>(arg0, arg1)
57+
}
58+
59+
// When default params are omitted they are not generated but used in signature inheritance.
60+
#[attr = Inline(Hint)]
61+
fn bar1<'a, Self, XX, Y, A>(self: _, arg1: _, arg2: _) -> _ where
62+
'a:'a {
63+
<Self as Trait::<'a, XX, Y>>::method::<A, ()>(self, arg1, arg2)
64+
}
65+
#[attr = Inline(Hint)]
66+
fn static_bar1<'a, Self, XX, Y, A>(arg0: _, arg1: _) -> _ where
67+
'a:'a { <Self as Trait::<'a, XX, Y>>::r#static::<A, ()>(arg0, arg1) }
68+
69+
// Check with explicit self type.
70+
#[attr = Inline(Hint)]
71+
fn foo2<'a, XX, Y, T, const N: _, A, B>(self: _, arg1: _, arg2: _) -> _
72+
where
73+
'a:'a {
74+
<X as Trait::<'a, XX, Y, T, N>>::method::<A, B>(self, arg1, arg2)
75+
}
76+
#[attr = Inline(Hint)]
77+
fn static_foo2<'a, XX, Y, T, const N: _, A, B>(arg0: _, arg1: _) -> _
78+
where
79+
'a:'a {
80+
<X as Trait::<'a, XX, Y, T, N>>::r#static::<A, B>(arg0, arg1)
81+
}
82+
83+
#[attr = Inline(Hint)]
84+
fn bar2<'a, XX, Y, A, B>(self: _, arg1: _, arg2: _) -> _ where
85+
'a:'a { <X as Trait::<'a, XX, Y>>::method::<A, B>(self, arg1, arg2) }
86+
#[attr = Inline(Hint)]
87+
fn static_bar2<'a, XX, Y, A, B>(arg0: _, arg1: _) -> _ where
88+
'a:'a { <X as Trait::<'a, XX, Y>>::r#static::<A, B>(arg0, arg1) }
89+
90+
#[attr = Inline(Hint)]
91+
fn foo3<'a, XX, Y, T, const N: _, B>(self: _, arg1: _, arg2: _) -> _ where
92+
'a:'a {
93+
<X as Trait::<'a, XX, Y, T, N>>::method::<(), B>(self, arg1, arg2)
94+
}
95+
#[attr = Inline(Hint)]
96+
fn static_foo3<'a, XX, Y, T, const N: _, B>(arg0: _, arg1: _) -> _ where
97+
'a:'a {
98+
<X as Trait::<'a, XX, Y, T, N>>::r#static::<(), B>(arg0, arg1)
99+
}
100+
101+
#[attr = Inline(Hint)]
102+
fn bar3<'a, XX, Y, A>(self: _, arg1: _, arg2: _) -> _ where
103+
'a:'a { <X as Trait::<'a, XX, Y>>::method::<A, ()>(self, arg1, arg2) }
104+
#[attr = Inline(Hint)]
105+
fn static_bar3<'a, XX, Y, A>(arg0: _, arg1: _) -> _ where
106+
'a:'a { <X as Trait::<'a, XX, Y>>::r#static::<A, ()>(arg0, arg1) }
107+
}
108+
109+
mod trait_impl_to_trait {
110+
trait Trait<'a, X, Y, T = (), const N: usize = 2> {
111+
fn foo(&self, t: (T, T, T), slice: &'_ [usize; N]) { }
112+
fn bar(&self, t: (T, T, T), slice: &'_ [usize; N]) { }
113+
}
114+
115+
struct S;
116+
impl <X, Y> Trait<'_, X, Y> for S { }
117+
118+
struct W(S);
119+
impl <X, Y> Trait<'_, X, Y> for W {
120+
// Generics of both methods match generics of their signature
121+
// functions in `Trait` declaration, no matter specified infers.
122+
#[attr = Inline(Hint)]
123+
fn foo(self: _, arg1: _, arg2: _)
124+
-> _ { Trait::<'static, X, Y>::foo(self.0, arg1, arg2) }
125+
#[attr = Inline(Hint)]
126+
fn bar(self: _, arg1: _, arg2: _)
127+
-> _ { Trait::<'static, X, Y, _, _>::foo(self.0, arg1, arg2) }
128+
}
129+
}
130+
131+
fn main() { }
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//@ pretty-compare-only
2+
//@ pretty-mode:hir
3+
//@ pp-exact:generics.pp
4+
5+
#![allow(incomplete_features)]
6+
#![feature(fn_delegation)]
7+
8+
mod free_to_trait {
9+
trait Trait<'a, XX, Y, T = (), const N: usize = 2> {
10+
fn method<A, B>(&self, t: (T, A, B), slice: &[usize; N]) {}
11+
fn r#static<A, B>(t: (T, A, B), slice: &[usize; N]) {}
12+
}
13+
14+
struct X;
15+
impl<XX, Y, T, const N: usize> Trait<'_, XX, Y, T, N> for X {}
16+
17+
// When infer is specified for default parameter the generic param is generated.
18+
reuse Trait::<'_, _, _, _, _>::method as foo;
19+
reuse Trait::<'_, _, _, _, _>::r#static as static_foo;
20+
21+
// When default params are omitted they are not generated but used in signature inheritance.
22+
reuse Trait::<'_, _, _>::method as bar;
23+
reuse Trait::<'_, _, _>::r#static as static_bar;
24+
25+
// Check with user specified args in child:
26+
// When infer is specified for default parameter the generic param is generated.
27+
reuse Trait::<'_, _, _, _, _>::method::<(), _> as foo1;
28+
reuse Trait::<'_, _, _, _, _>::r#static::<(), _> as static_foo1;
29+
30+
// When default params are omitted they are not generated but used in signature inheritance.
31+
reuse Trait::<'_, _, _>::method::<_, ()> as bar1;
32+
reuse Trait::<'_, _, _>::r#static::<_, ()> as static_bar1;
33+
34+
// Check with explicit self type.
35+
reuse <X as Trait::<'_, _, _, _, _>>::method as foo2;
36+
reuse <X as Trait::<'_, _, _, _, _>>::r#static as static_foo2;
37+
38+
reuse <X as Trait::<'_, _, _>>::method as bar2;
39+
reuse <X as Trait::<'_, _, _>>::r#static as static_bar2;
40+
41+
reuse <X as Trait::<'_, _, _, _, _>>::method::<(), _> as foo3;
42+
reuse <X as Trait::<'_, _, _, _, _>>::r#static::<(), _> as static_foo3;
43+
44+
reuse <X as Trait::<'_, _, _>>::method::<_, ()> as bar3;
45+
reuse <X as Trait::<'_, _, _>>::r#static::<_, ()> as static_bar3;
46+
}
47+
48+
mod trait_impl_to_trait {
49+
trait Trait<'a, X, Y, T = (), const N: usize = 2> {
50+
fn foo(&self, t: (T, T, T), slice: &[usize; N]) {}
51+
fn bar(&self, t: (T, T, T), slice: &[usize; N]) {}
52+
}
53+
54+
struct S;
55+
impl<X, Y> Trait<'_, X, Y> for S {}
56+
57+
struct W(S);
58+
impl<X, Y> Trait<'_, X, Y> for W {
59+
// Generics of both methods match generics of their signature
60+
// functions in `Trait` declaration, no matter specified infers.
61+
reuse Trait::<'static, X, Y>::foo { self.0 }
62+
reuse Trait::<'static, X, Y, _, _>::foo as bar { self.0 }
63+
}
64+
}
65+
66+
fn main() {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![no_std]
33
//@ pretty-compare-only
44
//@ pretty-mode:expanded
5-
//@ pp-exact:delegation-impl-reuse.pp
5+
//@ pp-exact:impl-reuse.pp
66

77
#![allow(incomplete_features)]
88
#![feature(fn_delegation)]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ pretty-compare-only
22
//@ pretty-mode:expanded
3-
//@ pp-exact:delegation-impl-reuse.pp
3+
//@ pp-exact:impl-reuse.pp
44

55
#![allow(incomplete_features)]
66
#![feature(fn_delegation)]

tests/pretty/delegation-inherit-attributes.pp renamed to tests/pretty/delegation/inherit-attributes.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ aux-crate:to_reuse_functions=to-reuse-functions.rs
33
//@ pretty-mode:hir
44
//@ pretty-compare-only
5-
//@ pp-exact:delegation-inherit-attributes.pp
5+
//@ pp-exact:inherit-attributes.pp
66

77
#![allow(incomplete_features)]
88
#![attr = Feature([fn_delegation#0])]

tests/pretty/delegation-inherit-attributes.rs renamed to tests/pretty/delegation/inherit-attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ aux-crate:to_reuse_functions=to-reuse-functions.rs
33
//@ pretty-mode:hir
44
//@ pretty-compare-only
5-
//@ pp-exact:delegation-inherit-attributes.pp
5+
//@ pp-exact:inherit-attributes.pp
66

77
#![allow(incomplete_features)]
88
#![feature(fn_delegation)]

tests/pretty/delegation-inline-attribute.pp renamed to tests/pretty/delegation/inline-attribute.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ pretty-compare-only
22
//@ pretty-mode:hir
3-
//@ pp-exact:delegation-inline-attribute.pp
3+
//@ pp-exact:inline-attribute.pp
44

55
#![allow(incomplete_features)]
66
#![attr = Feature([fn_delegation#0])]

tests/pretty/delegation-inline-attribute.rs renamed to tests/pretty/delegation/inline-attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ pretty-compare-only
22
//@ pretty-mode:hir
3-
//@ pp-exact:delegation-inline-attribute.pp
3+
//@ pp-exact:inline-attribute.pp
44

55
#![allow(incomplete_features)]
66
#![feature(fn_delegation)]

0 commit comments

Comments
 (0)