1 #![feature(generic_associated_types)]
2
3 // check-fail
4
5 use std::fmt::Debug;
6
7 // We have a `&'a self`, so we need a `Self: 'a`
8 trait Iterable {
9 type Item<'x>;
10 //~^ Missing required bounds
iter<'a>(&'a self) -> Self::Item<'a>11 fn iter<'a>(&'a self) -> Self::Item<'a>;
12 }
13
14 /*
15 impl<T> Iterable for T {
16 type Item<'a> = &'a T;
17 fn iter<'a>(&'a self) -> Self::Item<'a> {
18 self
19 }
20 }
21 */
22
23 // We have a `&'a T`, so we need a `T: 'x`
24 trait Deserializer<T> {
25 type Out<'x>;
26 //~^ Missing required bounds
deserialize<'a>(&self, input: &'a T) -> Self::Out<'a>27 fn deserialize<'a>(&self, input: &'a T) -> Self::Out<'a>;
28 }
29
30 /*
31 impl<T> Deserializer<T> for () {
32 type Out<'a> = &'a T;
33 fn deserialize<'a>(&self, input: &'a T) -> Self::Out<'a> { input }
34 }
35 */
36
37 // We have a `&'b T` and a `'b: 'a`, so it is implied that `T: 'a`. Therefore, we need a `T: 'x`
38 trait Deserializer2<T> {
39 type Out<'x>;
40 //~^ Missing required bounds
deserialize2<'a, 'b: 'a>(&self, input1: &'b T) -> Self::Out<'a>41 fn deserialize2<'a, 'b: 'a>(&self, input1: &'b T) -> Self::Out<'a>;
42 }
43
44 // We have a `&'a T` and a `&'b U`, so we need a `T: 'x` and a `U: 'y`
45 trait Deserializer3<T, U> {
46 type Out<'x, 'y>;
47 //~^ Missing required bounds
deserialize2<'a, 'b>(&self, input: &'a T, input2: &'b U) -> Self::Out<'a, 'b>48 fn deserialize2<'a, 'b>(&self, input: &'a T, input2: &'b U) -> Self::Out<'a, 'b>;
49 }
50
51 // `T` is a param on the function, so it can't be named by the associated type
52 trait Deserializer4 {
53 type Out<'x>;
deserialize<'a, T>(&self, input: &'a T) -> Self::Out<'a>54 fn deserialize<'a, T>(&self, input: &'a T) -> Self::Out<'a>;
55 }
56
57 struct Wrap<T>(T);
58
59 // We pass `Wrap<T>` and we see `&'z Wrap<T>`, so we require `D: 'x`
60 trait Des {
61 type Out<'x, D>;
62 //~^ Missing required bounds
des<'z, T>(&self, data: &'z Wrap<T>) -> Self::Out<'z, Wrap<T>>63 fn des<'z, T>(&self, data: &'z Wrap<T>) -> Self::Out<'z, Wrap<T>>;
64 }
65 /*
66 impl Des for () {
67 type Out<'x, D> = &'x D; // Not okay
68 fn des<'a, T>(&self, data: &'a Wrap<T>) -> Self::Out<'a, Wrap<T>> {
69 data
70 }
71 }
72 */
73
74 // We have `T` and `'z` as GAT substs. Because of `&'z Wrap<T>`, there is an
75 // implied bound that `T: 'z`, so we require `D: 'x`
76 trait Des2 {
77 type Out<'x, D>;
78 //~^ Missing required bounds
des<'z, T>(&self, data: &'z Wrap<T>) -> Self::Out<'z, T>79 fn des<'z, T>(&self, data: &'z Wrap<T>) -> Self::Out<'z, T>;
80 }
81 /*
82 impl Des2 for () {
83 type Out<'x, D> = &'x D;
84 fn des<'a, T>(&self, data: &'a Wrap<T>) -> Self::Out<'a, T> {
85 &data.0
86 }
87 }
88 */
89
90 // We see `&'z T`, so we require `D: 'x`
91 trait Des3 {
92 type Out<'x, D>;
93 //~^ Missing required bounds
des<'z, T>(&self, data: &'z T) -> Self::Out<'z, T>94 fn des<'z, T>(&self, data: &'z T) -> Self::Out<'z, T>;
95 }
96 /*
97 impl Des3 for () {
98 type Out<'x, D> = &'x D;
99 fn des<'a, T>(&self, data: &'a T) -> Self::Out<'a, T> {
100 data
101 }
102 }
103 */
104
105 // Similar case to before, except with GAT.
106 trait NoGat<'a> {
107 type Bar;
method(&'a self) -> Self::Bar108 fn method(&'a self) -> Self::Bar;
109 }
110
111 // Lifetime is not on function; except `Self: 'a`
112 // FIXME: we require two bounds (`where Self: 'a, Self: 'b`) when we should only require one
113 trait TraitLifetime<'a> {
114 type Bar<'b>;
115 //~^ Missing required bounds
method(&'a self) -> Self::Bar<'a>116 fn method(&'a self) -> Self::Bar<'a>;
117 }
118
119 // Like above, but we have a where clause that can prove what we want
120 // FIXME: we require two bounds (`where Self: 'a, Self: 'b`) when we should only require one
121 trait TraitLifetimeWhere<'a> where Self: 'a {
122 type Bar<'b>;
123 //~^ Missing required bounds
method(&'a self) -> Self::Bar<'a>124 fn method(&'a self) -> Self::Bar<'a>;
125 }
126
127 // Explicit bound instead of implicit; we want to still error
128 trait ExplicitBound {
129 type Bar<'b>;
130 //~^ Missing required bounds
method<'b>(&self, token: &'b ()) -> Self::Bar<'b> where Self: 'b131 fn method<'b>(&self, token: &'b ()) -> Self::Bar<'b> where Self: 'b;
132 }
133
134 // The use of the GAT here is not in the return, we don't want to error
135 trait NotInReturn {
136 type Bar<'b>;
method<'b>(&'b self) where Self::Bar<'b>: Debug137 fn method<'b>(&'b self) where Self::Bar<'b>: Debug;
138 }
139
140 // We obviously error for `Iterator`, but we should also error for `Item`
141 trait IterableTwo {
142 type Item<'a>;
143 type Iterator<'a>: Iterator<Item = Self::Item<'a>>;
144 //~^ Missing required bounds
iter<'a>(&'a self) -> Self::Iterator<'a>145 fn iter<'a>(&'a self) -> Self::Iterator<'a>;
146 }
147
148 // We also should report region outlives clauses
149 trait RegionOutlives {
150 type Bar<'a, 'b>;
151 //~^ Missing required bounds
foo<'x, 'y>(&self, input: &'x &'y ()) -> Self::Bar<'x, 'y>152 fn foo<'x, 'y>(&self, input: &'x &'y ()) -> Self::Bar<'x, 'y>;
153 }
154
155 /*
156 impl Foo for () {
157 type Bar<'a, 'b> = &'a &'b ();
158 fn foo<'x, 'y>(&self, input: &'x &'y ()) -> Self::Bar<'x, 'y> {
159 input
160 }
161 }
162 */
163
164 // If there are multiple methods that return the GAT, require a set of clauses
165 // that can be satisfied by *all* methods
166 trait MultipleMethods {
167 type Bar<'me>;
168
gimme<'a>(&'a self) -> Self::Bar<'a>169 fn gimme<'a>(&'a self) -> Self::Bar<'a>;
gimme_default(&self) -> Self::Bar<'static>170 fn gimme_default(&self) -> Self::Bar<'static>;
171 }
172
main()173 fn main() {}
174