]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0411.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0411.md
1 The `Self` keyword was used outside an impl, trait, or type definition.
2
3 Erroneous code example:
4
5 ```compile_fail,E0411
6 <Self>::foo; // error: use of `Self` outside of an impl, trait, or type
7              // definition
8 ```
9
10 The `Self` keyword represents the current type, which explains why it can only
11 be used inside an impl, trait, or type definition. It gives access to the
12 associated items of a type:
13
14 ```
15 trait Foo {
16     type Bar;
17 }
18
19 trait Baz : Foo {
20     fn bar() -> Self::Bar; // like this
21 }
22 ```
23
24 However, be careful when two types have a common associated type:
25
26 ```compile_fail
27 trait Foo {
28     type Bar;
29 }
30
31 trait Foo2 {
32     type Bar;
33 }
34
35 trait Baz : Foo + Foo2 {
36     fn bar() -> Self::Bar;
37     // error: ambiguous associated type `Bar` in bounds of `Self`
38 }
39 ```
40
41 This problem can be solved by specifying from which trait we want to use the
42 `Bar` type:
43
44 ```
45 trait Foo {
46     type Bar;
47 }
48
49 trait Foo2 {
50     type Bar;
51 }
52
53 trait Baz : Foo + Foo2 {
54     fn bar() -> <Self as Foo>::Bar; // ok!
55 }
56 ```