1
- A type parameter that is specified for ` impl ` is not constrained.
1
+ A type, const or lifetime parameter that is specified for ` impl ` is not
2
+ constrained.
2
3
3
4
Erroneous code example:
4
5
@@ -14,15 +15,18 @@ impl<T: Default> Foo {
14
15
}
15
16
```
16
17
17
- Any type parameter of an ` impl ` must meet at least one of
18
- the following criteria:
18
+ Any type or const parameter of an ` impl ` must meet at least one of the
19
+ following criteria:
19
20
20
21
- it appears in the _ implementing type_ of the impl, e.g. ` impl<T> Foo<T> `
21
22
- for a trait impl, it appears in the _ implemented trait_ , e.g.
22
23
` impl<T> SomeTrait<T> for Foo `
23
24
- it is bound as an associated type, e.g. `impl<T, U> SomeTrait for T
24
25
where T: AnotherTrait<AssocType=U>`
25
26
27
+ Any unconstrained lifetime parameter of an ` impl ` is not supported if the
28
+ lifetime parameter is used by an associated type.
29
+
26
30
### Error example 1
27
31
28
32
Suppose we have a struct ` Foo ` and we would like to define some methods for it.
@@ -32,7 +36,6 @@ The problem is that the parameter `T` does not appear in the implementing type
32
36
(` Foo ` ) of the impl. In this case, we can fix the error by moving the type
33
37
parameter from the ` impl ` to the method ` get ` :
34
38
35
-
36
39
```
37
40
struct Foo;
38
41
@@ -128,6 +131,70 @@ impl<T: Default> Maker<Foo<T>> for FooMaker {
128
131
}
129
132
```
130
133
134
+ ### Error example 3
135
+
136
+ Suppose we have a struct ` Foo ` and we would like to define some methods for it.
137
+ The following code example has a definition which leads to a compiler error:
138
+
139
+ ``` compile_fail,E0207
140
+ struct Foo;
141
+
142
+ impl<const T: i32> Foo {
143
+ // error: the const parameter `T` is not constrained by the impl trait, self
144
+ // type, or predicates [E0207]
145
+ fn get(&self) -> i32 {
146
+ i32::default()
147
+ }
148
+ }
149
+ ```
150
+
151
+ The problem is that the const parameter ` T ` does not appear in the implementing
152
+ type (` Foo ` ) of the impl. In this case, we can fix the error by moving the type
153
+ parameter from the ` impl ` to the method ` get ` :
154
+
155
+
156
+ ```
157
+ struct Foo;
158
+
159
+ // Move the const parameter from the impl to the method
160
+ impl Foo {
161
+ fn get<const T: i32>(&self) -> i32 {
162
+ i32::default()
163
+ }
164
+ }
165
+ ```
166
+
167
+ ### Error example 4
168
+
169
+ Suppose we have a struct ` Foo ` and a struct ` Bar ` that uses lifetime ` 'a ` . We
170
+ would like to implement trait ` Contains ` for ` Foo ` . The trait ` Contains ` have
171
+ the associated type ` B ` . The following code example has a definition which
172
+ leads to a compiler error:
173
+
174
+ ``` compile_fail,E0207
175
+ struct Foo;
176
+ struct Bar<'a>;
177
+
178
+ trait Contains {
179
+ type B;
180
+
181
+ fn get(&self) -> i32;
182
+ }
183
+
184
+ impl<'a> Contains for Foo {
185
+ type B = Bar<'a>;
186
+
187
+ // error: the lifetime parameter `'a` is not constrained by the impl trait,
188
+ // self type, or predicates [E0207]
189
+ fn get(&self) -> i32 {
190
+ i32::default()
191
+ }
192
+ }
193
+ ```
194
+
195
+ Please note that unconstrained lifetime parameters are not supported if they are
196
+ being used by an associated type.
197
+
131
198
### Additional information
132
199
133
200
For more information, please see [ RFC 447] .
0 commit comments