Skip to content

Commit 408b8cf

Browse files
authored
Rollup merge of #103757 - ffmancera:ff/clarify_E0207, r=jackh726
Mention const and lifetime parameters in error E0207 Error Index for E0207 must mention const and lifetime parameters. In addition, add code examples for these situations. Fixes #80862
2 parents ca08a32 + 2159df2 commit 408b8cf

File tree

1 file changed

+71
-4
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+71
-4
lines changed

compiler/rustc_error_codes/src/error_codes/E0207.md

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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.
23

34
Erroneous code example:
45

@@ -14,15 +15,18 @@ impl<T: Default> Foo {
1415
}
1516
```
1617

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:
1920

2021
- it appears in the _implementing type_ of the impl, e.g. `impl<T> Foo<T>`
2122
- for a trait impl, it appears in the _implemented trait_, e.g.
2223
`impl<T> SomeTrait<T> for Foo`
2324
- it is bound as an associated type, e.g. `impl<T, U> SomeTrait for T
2425
where T: AnotherTrait<AssocType=U>`
2526

27+
Any unconstrained lifetime parameter of an `impl` is not supported if the
28+
lifetime parameter is used by an associated type.
29+
2630
### Error example 1
2731

2832
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
3236
(`Foo`) of the impl. In this case, we can fix the error by moving the type
3337
parameter from the `impl` to the method `get`:
3438

35-
3639
```
3740
struct Foo;
3841
@@ -128,6 +131,70 @@ impl<T: Default> Maker<Foo<T>> for FooMaker {
128131
}
129132
```
130133

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+
131198
### Additional information
132199

133200
For more information, please see [RFC 447].

0 commit comments

Comments
 (0)