Skip to content

Commit 5bbdc73

Browse files
authored
Rollup merge of #74712 - strom-und-spiele:E0271-cleanup, r=Mark-Simulacrum
Update E0271 explanation When reading the explanation, I got confused by it. I hope the wording is clearer now.
2 parents 1facd4a + 7e68b7d commit 5bbdc73

File tree

1 file changed

+13
-30
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+13
-30
lines changed

src/librustc_error_codes/error_codes/E0271.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ Erroneous code example:
55
```compile_fail,E0271
66
trait Trait { type AssociatedType; }
77
8-
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
9-
println!("in foo");
10-
}
11-
12-
impl Trait for i8 { type AssociatedType = &'static str; }
13-
14-
foo(3_i8);
15-
```
16-
17-
This is because of a type mismatch between the associated type of some
18-
trait (e.g., `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
19-
and another type `U` that is required to be equal to `T::Bar`, but is not.
20-
Examples follow.
21-
22-
Here is that same example again, with some explanatory comments:
23-
24-
```compile_fail,E0271
25-
trait Trait { type AssociatedType; }
26-
278
fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
289
// ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
2910
// | |
@@ -56,11 +37,9 @@ foo(3_i8);
5637
// therefore the type-checker complains with this error code.
5738
```
5839

59-
To avoid those issues, you have to make the types match correctly.
60-
So we can fix the previous examples like this:
61-
40+
The issue can be resolved by changing the associated type:
41+
1) in the `foo` implementation:
6242
```
63-
// Basic Example:
6443
trait Trait { type AssociatedType; }
6544
6645
fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
@@ -70,13 +49,17 @@ fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
7049
impl Trait for i8 { type AssociatedType = &'static str; }
7150
7251
foo(3_i8);
52+
```
7353

74-
// For-Loop Example:
75-
let vs = vec![1, 2, 3, 4];
76-
for v in &vs {
77-
match v {
78-
&1 => {}
79-
_ => {}
80-
}
54+
2) in the `Trait` implementation for `i8`:
55+
```
56+
trait Trait { type AssociatedType; }
57+
58+
fn foo<T>(t: T) where T: Trait<AssociatedType = u32> {
59+
println!("in foo");
8160
}
61+
62+
impl Trait for i8 { type AssociatedType = u32; }
63+
64+
foo(3_i8);
8265
```

0 commit comments

Comments
 (0)