Skip to content

Improve documentation for E0223 #109565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions compiler/rustc_error_codes/src/error_codes/E0223.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ An attempt was made to retrieve an associated type, but the type was ambiguous.
Erroneous code example:

```compile_fail,E0223
trait MyTrait {type X; }
trait Trait { type X; }

fn main() {
let foo: MyTrait::X;
let foo: Trait::X;
}
```

The problem here is that we're attempting to take the type of X from MyTrait.
Unfortunately, the type of X is not defined, because it's only made concrete in
implementations of the trait. A working version of this code might look like:
The problem here is that we're attempting to take the associated type of `X`
from `Trait`. Unfortunately, the type of `X` is not defined, because it's only
made concrete in implementations of the trait. A working version of this code
might look like:

```
trait MyTrait {type X; }
struct MyStruct;
trait Trait { type X; }

impl MyTrait for MyStruct {
struct Struct;
impl Trait for Struct {
type X = u32;
}

fn main() {
let foo: <MyStruct as MyTrait>::X;
let foo: <Struct as Trait>::X;
}
```

This syntax specifies that we want the X type from MyTrait, as made concrete in
MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
might implement two different traits with identically-named associated types.
This syntax allows disambiguation between the two.
This syntax specifies that we want the associated type `X` from `Struct`'s
implementation of `Trait`.

Due to internal limitations of the current compiler implementation we cannot
simply use `Struct::X`.