diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-08-09 06:41:14 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-09 06:41:14 +0900 |
| commit | 5bbdc73ebc208246755285f17485ba93305686ae (patch) | |
| tree | 27afe20d547fe40fe31b4df76a668c1473d8db4e /src/librustc_error_codes/error_codes | |
| parent | 1facd4a77b181ad44b9c9a64f0fd21b6d5180458 (diff) | |
| parent | 7e68b7d10a7ce505fd41ebc99bd77276ab093a16 (diff) | |
| download | rust-5bbdc73ebc208246755285f17485ba93305686ae.tar.gz rust-5bbdc73ebc208246755285f17485ba93305686ae.zip | |
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.
Diffstat (limited to 'src/librustc_error_codes/error_codes')
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0271.md | 43 |
1 files changed, 13 insertions, 30 deletions
diff --git a/src/librustc_error_codes/error_codes/E0271.md b/src/librustc_error_codes/error_codes/E0271.md index 31334069ed8..ddd245b1a2b 100644 --- a/src/librustc_error_codes/error_codes/E0271.md +++ b/src/librustc_error_codes/error_codes/E0271.md @@ -6,25 +6,6 @@ Erroneous code example: trait Trait { type AssociatedType; } fn foo<T>(t: T) where T: Trait<AssociatedType=u32> { - println!("in foo"); -} - -impl Trait for i8 { type AssociatedType = &'static str; } - -foo(3_i8); -``` - -This is because of a type mismatch between the associated type of some -trait (e.g., `T::Bar`, where `T` implements `trait Quux { type Bar; }`) -and another type `U` that is required to be equal to `T::Bar`, but is not. -Examples follow. - -Here is that same example again, with some explanatory comments: - -```compile_fail,E0271 -trait Trait { type AssociatedType; } - -fn foo<T>(t: T) where T: Trait<AssociatedType=u32> { // ~~~~~~~~ ~~~~~~~~~~~~~~~~~~ // | | // This says `foo` can | @@ -56,11 +37,9 @@ foo(3_i8); // therefore the type-checker complains with this error code. ``` -To avoid those issues, you have to make the types match correctly. -So we can fix the previous examples like this: - +The issue can be resolved by changing the associated type: +1) in the `foo` implementation: ``` -// Basic Example: trait Trait { type AssociatedType; } 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> { impl Trait for i8 { type AssociatedType = &'static str; } foo(3_i8); +``` -// For-Loop Example: -let vs = vec![1, 2, 3, 4]; -for v in &vs { - match v { - &1 => {} - _ => {} - } +2) in the `Trait` implementation for `i8`: +``` +trait Trait { type AssociatedType; } + +fn foo<T>(t: T) where T: Trait<AssociatedType = u32> { + println!("in foo"); } + +impl Trait for i8 { type AssociatedType = u32; } + +foo(3_i8); ``` |
