]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/error_codes.rs
32e5eb50a76da564c93e836dd1f87abc139c2324
[rust.git] / src / librustc_resolve / error_codes.rs
1 // Error messages for EXXXX errors.  Each message should start and end with a
2 // new line, and be wrapped to 80 characters.  In vim you can `:set tw=80` and
3 // use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
4 syntax::register_diagnostics! {
5
6 E0128: r##"
7 Type parameter defaults can only use parameters that occur before them.
8 Erroneous code example:
9
10 ```compile_fail,E0128
11 struct Foo<T = U, U = ()> {
12     field1: T,
13     field2: U,
14 }
15 // error: type parameters with a default cannot use forward declared
16 // identifiers
17 ```
18
19 Since type parameters are evaluated in-order, you may be able to fix this issue
20 by doing:
21
22 ```
23 struct Foo<U = (), T = U> {
24     field1: T,
25     field2: U,
26 }
27 ```
28
29 Please also verify that this wasn't because of a name-clash and rename the type
30 parameter if so.
31 "##,
32
33 E0154: r##"
34 #### Note: this error code is no longer emitted by the compiler.
35
36 Imports (`use` statements) are not allowed after non-item statements, such as
37 variable declarations and expression statements.
38
39 Here is an example that demonstrates the error:
40
41 ```
42 fn f() {
43     // Variable declaration before import
44     let x = 0;
45     use std::io::Read;
46     // ...
47 }
48 ```
49
50 The solution is to declare the imports at the top of the block, function, or
51 file.
52
53 Here is the previous example again, with the correct order:
54
55 ```
56 fn f() {
57     use std::io::Read;
58     let x = 0;
59     // ...
60 }
61 ```
62
63 See the Declaration Statements section of the reference for more information
64 about what constitutes an Item declaration and what does not:
65
66 https://doc.rust-lang.org/reference.html#statements
67 "##,
68
69 E0251: r##"
70 #### Note: this error code is no longer emitted by the compiler.
71
72 Two items of the same name cannot be imported without rebinding one of the
73 items under a new local name.
74
75 An example of this error:
76
77 ```
78 use foo::baz;
79 use bar::*; // error, do `use foo::baz as quux` instead on the previous line
80
81 fn main() {}
82
83 mod foo {
84     pub struct baz;
85 }
86
87 mod bar {
88     pub mod baz {}
89 }
90 ```
91 "##,
92
93 E0252: r##"
94 Two items of the same name cannot be imported without rebinding one of the
95 items under a new local name.
96
97 Erroneous code example:
98
99 ```compile_fail,E0252
100 use foo::baz;
101 use bar::baz; // error, do `use bar::baz as quux` instead
102
103 fn main() {}
104
105 mod foo {
106     pub struct baz;
107 }
108
109 mod bar {
110     pub mod baz {}
111 }
112 ```
113
114 You can use aliases in order to fix this error. Example:
115
116 ```
117 use foo::baz as foo_baz;
118 use bar::baz; // ok!
119
120 fn main() {}
121
122 mod foo {
123     pub struct baz;
124 }
125
126 mod bar {
127     pub mod baz {}
128 }
129 ```
130
131 Or you can reference the item with its parent:
132
133 ```
134 use bar::baz;
135
136 fn main() {
137     let x = foo::baz; // ok!
138 }
139
140 mod foo {
141     pub struct baz;
142 }
143
144 mod bar {
145     pub mod baz {}
146 }
147 ```
148 "##,
149
150 E0253: r##"
151 Attempt was made to import an unimportable value. This can happen when trying
152 to import a method from a trait.
153
154 Erroneous code example:
155
156 ```compile_fail,E0253
157 mod foo {
158     pub trait MyTrait {
159         fn do_something();
160     }
161 }
162
163 use foo::MyTrait::do_something;
164 // error: `do_something` is not directly importable
165
166 fn main() {}
167 ```
168
169 It's invalid to directly import methods belonging to a trait or concrete type.
170 "##,
171
172 E0254: r##"
173 Attempt was made to import an item whereas an extern crate with this name has
174 already been imported.
175
176 Erroneous code example:
177
178 ```compile_fail,E0254
179 extern crate core;
180
181 mod foo {
182     pub trait core {
183         fn do_something();
184     }
185 }
186
187 use foo::core;  // error: an extern crate named `core` has already
188                 //        been imported in this module
189
190 fn main() {}
191 ```
192
193 To fix this issue, you have to rename at least one of the two imports.
194 Example:
195
196 ```
197 extern crate core as libcore; // ok!
198
199 mod foo {
200     pub trait core {
201         fn do_something();
202     }
203 }
204
205 use foo::core;
206
207 fn main() {}
208 ```
209 "##,
210
211 E0255: r##"
212 You can't import a value whose name is the same as another value defined in the
213 module.
214
215 Erroneous code example:
216
217 ```compile_fail,E0255
218 use bar::foo; // error: an item named `foo` is already in scope
219
220 fn foo() {}
221
222 mod bar {
223      pub fn foo() {}
224 }
225
226 fn main() {}
227 ```
228
229 You can use aliases in order to fix this error. Example:
230
231 ```
232 use bar::foo as bar_foo; // ok!
233
234 fn foo() {}
235
236 mod bar {
237      pub fn foo() {}
238 }
239
240 fn main() {}
241 ```
242
243 Or you can reference the item with its parent:
244
245 ```
246 fn foo() {}
247
248 mod bar {
249      pub fn foo() {}
250 }
251
252 fn main() {
253     bar::foo(); // we get the item by referring to its parent
254 }
255 ```
256 "##,
257
258 E0256: r##"
259 #### Note: this error code is no longer emitted by the compiler.
260
261 You can't import a type or module when the name of the item being imported is
262 the same as another type or submodule defined in the module.
263
264 An example of this error:
265
266 ```compile_fail
267 use foo::Bar; // error
268
269 type Bar = u32;
270
271 mod foo {
272     pub mod Bar { }
273 }
274
275 fn main() {}
276 ```
277 "##,
278
279 E0259: r##"
280 The name chosen for an external crate conflicts with another external crate
281 that has been imported into the current module.
282
283 Erroneous code example:
284
285 ```compile_fail,E0259
286 extern crate core;
287 extern crate std as core;
288
289 fn main() {}
290 ```
291
292 The solution is to choose a different name that doesn't conflict with any
293 external crate imported into the current module.
294
295 Correct example:
296
297 ```
298 extern crate core;
299 extern crate std as other_name;
300
301 fn main() {}
302 ```
303 "##,
304
305 E0260: r##"
306 The name for an item declaration conflicts with an external crate's name.
307
308 Erroneous code example:
309
310 ```compile_fail,E0260
311 extern crate core;
312
313 struct core;
314
315 fn main() {}
316 ```
317
318 There are two possible solutions:
319
320 Solution #1: Rename the item.
321
322 ```
323 extern crate core;
324
325 struct xyz;
326 ```
327
328 Solution #2: Import the crate with a different name.
329
330 ```
331 extern crate core as xyz;
332
333 struct abc;
334 ```
335
336 See the Declaration Statements section of the reference for more information
337 about what constitutes an Item declaration and what does not:
338
339 https://doc.rust-lang.org/reference.html#statements
340 "##,
341
342 E0364: r##"
343 Private items cannot be publicly re-exported. This error indicates that you
344 attempted to `pub use` a type or value that was not itself public.
345
346 Erroneous code example:
347
348 ```compile_fail
349 mod foo {
350     const X: u32 = 1;
351 }
352
353 pub use foo::X;
354
355 fn main() {}
356 ```
357
358 The solution to this problem is to ensure that the items that you are
359 re-exporting are themselves marked with `pub`:
360
361 ```
362 mod foo {
363     pub const X: u32 = 1;
364 }
365
366 pub use foo::X;
367
368 fn main() {}
369 ```
370
371 See the 'Use Declarations' section of the reference for more information on
372 this topic:
373
374 https://doc.rust-lang.org/reference.html#use-declarations
375 "##,
376
377 E0365: r##"
378 Private modules cannot be publicly re-exported. This error indicates that you
379 attempted to `pub use` a module that was not itself public.
380
381 Erroneous code example:
382
383 ```compile_fail,E0365
384 mod foo {
385     pub const X: u32 = 1;
386 }
387
388 pub use foo as foo2;
389
390 fn main() {}
391 ```
392
393 The solution to this problem is to ensure that the module that you are
394 re-exporting is itself marked with `pub`:
395
396 ```
397 pub mod foo {
398     pub const X: u32 = 1;
399 }
400
401 pub use foo as foo2;
402
403 fn main() {}
404 ```
405
406 See the 'Use Declarations' section of the reference for more information
407 on this topic:
408
409 https://doc.rust-lang.org/reference.html#use-declarations
410 "##,
411
412 E0401: r##"
413 Inner items do not inherit type or const parameters from the functions
414 they are embedded in.
415
416 Erroneous code example:
417
418 ```compile_fail,E0401
419 fn foo<T>(x: T) {
420     fn bar(y: T) { // T is defined in the "outer" function
421         // ..
422     }
423     bar(x);
424 }
425 ```
426
427 Nor will this:
428
429 ```compile_fail,E0401
430 fn foo<T>(x: T) {
431     type MaybeT = Option<T>;
432     // ...
433 }
434 ```
435
436 Or this:
437
438 ```compile_fail,E0401
439 fn foo<T>(x: T) {
440     struct Foo {
441         x: T,
442     }
443     // ...
444 }
445 ```
446
447 Items inside functions are basically just like top-level items, except
448 that they can only be used from the function they are in.
449
450 There are a couple of solutions for this.
451
452 If the item is a function, you may use a closure:
453
454 ```
455 fn foo<T>(x: T) {
456     let bar = |y: T| { // explicit type annotation may not be necessary
457         // ..
458     };
459     bar(x);
460 }
461 ```
462
463 For a generic item, you can copy over the parameters:
464
465 ```
466 fn foo<T>(x: T) {
467     fn bar<T>(y: T) {
468         // ..
469     }
470     bar(x);
471 }
472 ```
473
474 ```
475 fn foo<T>(x: T) {
476     type MaybeT<T> = Option<T>;
477 }
478 ```
479
480 Be sure to copy over any bounds as well:
481
482 ```
483 fn foo<T: Copy>(x: T) {
484     fn bar<T: Copy>(y: T) {
485         // ..
486     }
487     bar(x);
488 }
489 ```
490
491 ```
492 fn foo<T: Copy>(x: T) {
493     struct Foo<T: Copy> {
494         x: T,
495     }
496 }
497 ```
498
499 This may require additional type hints in the function body.
500
501 In case the item is a function inside an `impl`, defining a private helper
502 function might be easier:
503
504 ```
505 # struct Foo<T>(T);
506 impl<T> Foo<T> {
507     pub fn foo(&self, x: T) {
508         self.bar(x);
509     }
510
511     fn bar(&self, y: T) {
512         // ..
513     }
514 }
515 ```
516
517 For default impls in traits, the private helper solution won't work, however
518 closures or copying the parameters should still work.
519 "##,
520
521 E0403: r##"
522 Some type parameters have the same name.
523
524 Erroneous code example:
525
526 ```compile_fail,E0403
527 fn f<T, T>(s: T, u: T) {} // error: the name `T` is already used for a generic
528                           //        parameter in this item's generic parameters
529 ```
530
531 Please verify that none of the type parameters are misspelled, and rename any
532 clashing parameters. Example:
533
534 ```
535 fn f<T, Y>(s: T, u: Y) {} // ok!
536 ```
537
538 Type parameters in an associated item also cannot shadow parameters from the
539 containing item:
540
541 ```compile_fail,E0403
542 trait Foo<T> {
543     fn do_something(&self) -> T;
544     fn do_something_else<T: Clone>(&self, bar: T);
545 }
546 ```
547 "##,
548
549 E0404: r##"
550 You tried to use something which is not a trait in a trait position, such as
551 a bound or `impl`.
552
553 Erroneous code example:
554
555 ```compile_fail,E0404
556 struct Foo;
557 struct Bar;
558
559 impl Foo for Bar {} // error: `Foo` is not a trait
560 ```
561
562 Another erroneous code example:
563
564 ```compile_fail,E0404
565 struct Foo;
566
567 fn bar<T: Foo>(t: T) {} // error: `Foo` is not a trait
568 ```
569
570 Please verify that you didn't misspell the trait's name or otherwise use the
571 wrong identifier. Example:
572
573 ```
574 trait Foo {
575     // some functions
576 }
577 struct Bar;
578
579 impl Foo for Bar { // ok!
580     // functions implementation
581 }
582 ```
583
584 or
585
586 ```
587 trait Foo {
588     // some functions
589 }
590
591 fn bar<T: Foo>(t: T) {} // ok!
592 ```
593
594 "##,
595
596 E0405: r##"
597 The code refers to a trait that is not in scope.
598
599 Erroneous code example:
600
601 ```compile_fail,E0405
602 struct Foo;
603
604 impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
605 ```
606
607 Please verify that the name of the trait wasn't misspelled and ensure that it
608 was imported. Example:
609
610 ```
611 # #[cfg(for_demonstration_only)]
612 // solution 1:
613 use some_file::SomeTrait;
614
615 // solution 2:
616 trait SomeTrait {
617     // some functions
618 }
619
620 struct Foo;
621
622 impl SomeTrait for Foo { // ok!
623     // implements functions
624 }
625 ```
626 "##,
627
628 E0407: r##"
629 A definition of a method not in the implemented trait was given in a trait
630 implementation.
631
632 Erroneous code example:
633
634 ```compile_fail,E0407
635 trait Foo {
636     fn a();
637 }
638
639 struct Bar;
640
641 impl Foo for Bar {
642     fn a() {}
643     fn b() {} // error: method `b` is not a member of trait `Foo`
644 }
645 ```
646
647 Please verify you didn't misspell the method name and you used the correct
648 trait. First example:
649
650 ```
651 trait Foo {
652     fn a();
653     fn b();
654 }
655
656 struct Bar;
657
658 impl Foo for Bar {
659     fn a() {}
660     fn b() {} // ok!
661 }
662 ```
663
664 Second example:
665
666 ```
667 trait Foo {
668     fn a();
669 }
670
671 struct Bar;
672
673 impl Foo for Bar {
674     fn a() {}
675 }
676
677 impl Bar {
678     fn b() {}
679 }
680 ```
681 "##,
682
683 E0408: r##"
684 An "or" pattern was used where the variable bindings are not consistently bound
685 across patterns.
686
687 Erroneous code example:
688
689 ```compile_fail,E0408
690 match x {
691     Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
692                                       //        not bound in pattern #2
693     _ => ()
694 }
695 ```
696
697 Here, `y` is bound to the contents of the `Some` and can be used within the
698 block corresponding to the match arm. However, in case `x` is `None`, we have
699 not specified what `y` is, and the block will use a nonexistent variable.
700
701 To fix this error, either split into multiple match arms:
702
703 ```
704 let x = Some(1);
705 match x {
706     Some(y) => { /* use y */ }
707     None => { /* ... */ }
708 }
709 ```
710
711 or, bind the variable to a field of the same type in all sub-patterns of the
712 or pattern:
713
714 ```
715 let x = (0, 2);
716 match x {
717     (0, y) | (y, 0) => { /* use y */}
718     _ => {}
719 }
720 ```
721
722 In this example, if `x` matches the pattern `(0, _)`, the second field is set
723 to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
724 cases `y` is set to some value.
725 "##,
726
727 E0409: r##"
728 An "or" pattern was used where the variable bindings are not consistently bound
729 across patterns.
730
731 Erroneous code example:
732
733 ```compile_fail,E0409
734 let x = (0, 2);
735 match x {
736     (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
737                                           //        different mode in pattern #2
738                                           //        than in pattern #1
739     _ => ()
740 }
741 ```
742
743 Here, `y` is bound by-value in one case and by-reference in the other.
744
745 To fix this error, just use the same mode in both cases.
746 Generally using `ref` or `ref mut` where not already used will fix this:
747
748 ```
749 let x = (0, 2);
750 match x {
751     (0, ref y) | (ref y, 0) => { /* use y */}
752     _ => ()
753 }
754 ```
755
756 Alternatively, split the pattern:
757
758 ```
759 let x = (0, 2);
760 match x {
761     (y, 0) => { /* use y */ }
762     (0, ref y) => { /* use y */}
763     _ => ()
764 }
765 ```
766 "##,
767
768 E0411: r##"
769 The `Self` keyword was used outside an impl, trait, or type definition.
770
771 Erroneous code example:
772
773 ```compile_fail,E0411
774 <Self>::foo; // error: use of `Self` outside of an impl, trait, or type
775              // definition
776 ```
777
778 The `Self` keyword represents the current type, which explains why it can only
779 be used inside an impl, trait, or type definition. It gives access to the
780 associated items of a type:
781
782 ```
783 trait Foo {
784     type Bar;
785 }
786
787 trait Baz : Foo {
788     fn bar() -> Self::Bar; // like this
789 }
790 ```
791
792 However, be careful when two types have a common associated type:
793
794 ```compile_fail
795 trait Foo {
796     type Bar;
797 }
798
799 trait Foo2 {
800     type Bar;
801 }
802
803 trait Baz : Foo + Foo2 {
804     fn bar() -> Self::Bar;
805     // error: ambiguous associated type `Bar` in bounds of `Self`
806 }
807 ```
808
809 This problem can be solved by specifying from which trait we want to use the
810 `Bar` type:
811
812 ```
813 trait Foo {
814     type Bar;
815 }
816
817 trait Foo2 {
818     type Bar;
819 }
820
821 trait Baz : Foo + Foo2 {
822     fn bar() -> <Self as Foo>::Bar; // ok!
823 }
824 ```
825 "##,
826
827 E0412: r##"
828 The type name used is not in scope.
829
830 Erroneous code examples:
831
832 ```compile_fail,E0412
833 impl Something {} // error: type name `Something` is not in scope
834
835 // or:
836
837 trait Foo {
838     fn bar(N); // error: type name `N` is not in scope
839 }
840
841 // or:
842
843 fn foo(x: T) {} // type name `T` is not in scope
844 ```
845
846 To fix this error, please verify you didn't misspell the type name, you did
847 declare it or imported it into the scope. Examples:
848
849 ```
850 struct Something;
851
852 impl Something {} // ok!
853
854 // or:
855
856 trait Foo {
857     type N;
858
859     fn bar(_: Self::N); // ok!
860 }
861
862 // or:
863
864 fn foo<T>(x: T) {} // ok!
865 ```
866
867 Another case that causes this error is when a type is imported into a parent
868 module. To fix this, you can follow the suggestion and use File directly or
869 `use super::File;` which will import the types from the parent namespace. An
870 example that causes this error is below:
871
872 ```compile_fail,E0412
873 use std::fs::File;
874
875 mod foo {
876     fn some_function(f: File) {}
877 }
878 ```
879
880 ```
881 use std::fs::File;
882
883 mod foo {
884     // either
885     use super::File;
886     // or
887     // use std::fs::File;
888     fn foo(f: File) {}
889 }
890 # fn main() {} // don't insert it for us; that'll break imports
891 ```
892 "##,
893
894 E0415: r##"
895 More than one function parameter have the same name.
896
897 Erroneous code example:
898
899 ```compile_fail,E0415
900 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
901                           //        once in this parameter list
902 ```
903
904 Please verify you didn't misspell parameters' name. Example:
905
906 ```
907 fn foo(f: i32, g: i32) {} // ok!
908 ```
909 "##,
910
911 E0416: r##"
912 An identifier is bound more than once in a pattern.
913
914 Erroneous code example:
915
916 ```compile_fail,E0416
917 match (1, 2) {
918     (x, x) => {} // error: identifier `x` is bound more than once in the
919                  //        same pattern
920 }
921 ```
922
923 Please verify you didn't misspell identifiers' name. Example:
924
925 ```
926 match (1, 2) {
927     (x, y) => {} // ok!
928 }
929 ```
930
931 Or maybe did you mean to unify? Consider using a guard:
932
933 ```
934 # let (A, B, C) = (1, 2, 3);
935 match (A, B, C) {
936     (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
937     (y, z, see) => { /* A and B unequal; do another thing */ }
938 }
939 ```
940 "##,
941
942 E0422: r##"
943 You are trying to use an identifier that is either undefined or not a struct.
944 Erroneous code example:
945
946 ```compile_fail,E0422
947 fn main () {
948     let x = Foo { x: 1, y: 2 };
949 }
950 ```
951
952 In this case, `Foo` is undefined, so it inherently isn't anything, and
953 definitely not a struct.
954
955 ```compile_fail
956 fn main () {
957     let foo = 1;
958     let x = foo { x: 1, y: 2 };
959 }
960 ```
961
962 In this case, `foo` is defined, but is not a struct, so Rust can't use it as
963 one.
964 "##,
965
966 E0423: r##"
967 An identifier was used like a function name or a value was expected and the
968 identifier exists but it belongs to a different namespace.
969
970 For (an erroneous) example, here a `struct` variant name were used as a
971 function:
972
973 ```compile_fail,E0423
974 struct Foo { a: bool };
975
976 let f = Foo();
977 // error: expected function, tuple struct or tuple variant, found `Foo`
978 // `Foo` is a struct name, but this expression uses it like a function name
979 ```
980
981 Please verify you didn't misspell the name of what you actually wanted to use
982 here. Example:
983
984 ```
985 fn Foo() -> u32 { 0 }
986
987 let f = Foo(); // ok!
988 ```
989
990 It is common to forget the trailing `!` on macro invocations, which would also
991 yield this error:
992
993 ```compile_fail,E0423
994 println("");
995 // error: expected function, tuple struct or tuple variant,
996 // found macro `println`
997 // did you mean `println!(...)`? (notice the trailing `!`)
998 ```
999
1000 Another case where this error is emitted is when a value is expected, but
1001 something else is found:
1002
1003 ```compile_fail,E0423
1004 pub mod a {
1005     pub const I: i32 = 1;
1006 }
1007
1008 fn h1() -> i32 {
1009     a.I
1010     //~^ ERROR expected value, found module `a`
1011     // did you mean `a::I`?
1012 }
1013 ```
1014 "##,
1015
1016 E0424: r##"
1017 The `self` keyword was used inside of an associated function without a "`self`
1018 receiver" parameter.
1019
1020 Erroneous code example:
1021
1022 ```compile_fail,E0424
1023 struct Foo;
1024
1025 impl Foo {
1026     // `bar` is a method, because it has a receiver parameter.
1027     fn bar(&self) {}
1028
1029     // `foo` is not a method, because it has no receiver parameter.
1030     fn foo() {
1031         self.bar(); // error: `self` value is a keyword only available in
1032                     //        methods with a `self` parameter
1033     }
1034 }
1035 ```
1036
1037 The `self` keyword can only be used inside methods, which are associated
1038 functions (functions defined inside of a `trait` or `impl` block) that have a
1039 `self` receiver as its first parameter, like `self`, `&self`, `&mut self` or
1040 `self: &mut Pin<Self>` (this last one is an example of an ["abitrary `self`
1041 type"](https://github.com/rust-lang/rust/issues/44874)).
1042
1043 Check if the associated function's parameter list should have contained a `self`
1044 receiver for it to be a method, and add it if so. Example:
1045
1046 ```
1047 struct Foo;
1048
1049 impl Foo {
1050     fn bar(&self) {}
1051
1052     fn foo(self) { // `foo` is now a method.
1053         self.bar(); // ok!
1054     }
1055 }
1056 ```
1057 "##,
1058
1059 E0425: r##"
1060 An unresolved name was used.
1061
1062 Erroneous code examples:
1063
1064 ```compile_fail,E0425
1065 something_that_doesnt_exist::foo;
1066 // error: unresolved name `something_that_doesnt_exist::foo`
1067
1068 // or:
1069
1070 trait Foo {
1071     fn bar() {
1072         Self; // error: unresolved name `Self`
1073     }
1074 }
1075
1076 // or:
1077
1078 let x = unknown_variable;  // error: unresolved name `unknown_variable`
1079 ```
1080
1081 Please verify that the name wasn't misspelled and ensure that the
1082 identifier being referred to is valid for the given situation. Example:
1083
1084 ```
1085 enum something_that_does_exist {
1086     Foo,
1087 }
1088 ```
1089
1090 Or:
1091
1092 ```
1093 mod something_that_does_exist {
1094     pub static foo : i32 = 0i32;
1095 }
1096
1097 something_that_does_exist::foo; // ok!
1098 ```
1099
1100 Or:
1101
1102 ```
1103 let unknown_variable = 12u32;
1104 let x = unknown_variable; // ok!
1105 ```
1106
1107 If the item is not defined in the current module, it must be imported using a
1108 `use` statement, like so:
1109
1110 ```
1111 # mod foo { pub fn bar() {} }
1112 # fn main() {
1113 use foo::bar;
1114 bar();
1115 # }
1116 ```
1117
1118 If the item you are importing is not defined in some super-module of the
1119 current module, then it must also be declared as public (e.g., `pub fn`).
1120 "##,
1121
1122 E0426: r##"
1123 An undeclared label was used.
1124
1125 Erroneous code example:
1126
1127 ```compile_fail,E0426
1128 loop {
1129     break 'a; // error: use of undeclared label `'a`
1130 }
1131 ```
1132
1133 Please verify you spelt or declare the label correctly. Example:
1134
1135 ```
1136 'a: loop {
1137     break 'a; // ok!
1138 }
1139 ```
1140 "##,
1141
1142 E0428: r##"
1143 A type or module has been defined more than once.
1144
1145 Erroneous code example:
1146
1147 ```compile_fail,E0428
1148 struct Bar;
1149 struct Bar; // error: duplicate definition of value `Bar`
1150 ```
1151
1152 Please verify you didn't misspell the type/module's name or remove/rename the
1153 duplicated one. Example:
1154
1155 ```
1156 struct Bar;
1157 struct Bar2; // ok!
1158 ```
1159 "##,
1160
1161 E0429: r##"
1162 The `self` keyword cannot appear alone as the last segment in a `use`
1163 declaration.
1164
1165 Erroneous code example:
1166
1167 ```compile_fail,E0429
1168 use std::fmt::self; // error: `self` imports are only allowed within a { } list
1169 ```
1170
1171 To use a namespace itself in addition to some of its members, `self` may appear
1172 as part of a brace-enclosed list of imports:
1173
1174 ```
1175 use std::fmt::{self, Debug};
1176 ```
1177
1178 If you only want to import the namespace, do so directly:
1179
1180 ```
1181 use std::fmt;
1182 ```
1183 "##,
1184
1185 E0430: r##"
1186 The `self` import appears more than once in the list.
1187
1188 Erroneous code example:
1189
1190 ```compile_fail,E0430
1191 use something::{self, self}; // error: `self` import can only appear once in
1192                              //        the list
1193 ```
1194
1195 Please verify you didn't misspell the import name or remove the duplicated
1196 `self` import. Example:
1197
1198 ```
1199 # mod something {}
1200 # fn main() {
1201 use something::{self}; // ok!
1202 # }
1203 ```
1204 "##,
1205
1206 E0431: r##"
1207 An invalid `self` import was made.
1208
1209 Erroneous code example:
1210
1211 ```compile_fail,E0431
1212 use {self}; // error: `self` import can only appear in an import list with a
1213             //        non-empty prefix
1214 ```
1215
1216 You cannot import the current module into itself, please remove this import
1217 or verify you didn't misspell it.
1218 "##,
1219
1220 E0432: r##"
1221 An import was unresolved.
1222
1223 Erroneous code example:
1224
1225 ```compile_fail,E0432
1226 use something::Foo; // error: unresolved import `something::Foo`.
1227 ```
1228
1229 Paths in `use` statements are relative to the crate root. To import items
1230 relative to the current and parent modules, use the `self::` and `super::`
1231 prefixes, respectively. Also verify that you didn't misspell the import
1232 name and that the import exists in the module from where you tried to
1233 import it. Example:
1234
1235 ```
1236 use self::something::Foo; // ok!
1237
1238 mod something {
1239     pub struct Foo;
1240 }
1241 # fn main() {}
1242 ```
1243
1244 Or, if you tried to use a module from an external crate, you may have missed
1245 the `extern crate` declaration (which is usually placed in the crate root):
1246
1247 ```
1248 extern crate core; // Required to use the `core` crate
1249
1250 use core::any;
1251 # fn main() {}
1252 ```
1253 "##,
1254
1255 E0433: r##"
1256 An undeclared type or module was used.
1257
1258 Erroneous code example:
1259
1260 ```compile_fail,E0433
1261 let map = HashMap::new();
1262 // error: failed to resolve: use of undeclared type or module `HashMap`
1263 ```
1264
1265 Please verify you didn't misspell the type/module's name or that you didn't
1266 forget to import it:
1267
1268
1269 ```
1270 use std::collections::HashMap; // HashMap has been imported.
1271 let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1272 ```
1273 "##,
1274
1275 E0434: r##"
1276 This error indicates that a variable usage inside an inner function is invalid
1277 because the variable comes from a dynamic environment. Inner functions do not
1278 have access to their containing environment.
1279
1280 Erroneous code example:
1281
1282 ```compile_fail,E0434
1283 fn foo() {
1284     let y = 5;
1285     fn bar() -> u32 {
1286         y // error: can't capture dynamic environment in a fn item; use the
1287           //        || { ... } closure form instead.
1288     }
1289 }
1290 ```
1291
1292 Functions do not capture local variables. To fix this error, you can replace the
1293 function with a closure:
1294
1295 ```
1296 fn foo() {
1297     let y = 5;
1298     let bar = || {
1299         y
1300     };
1301 }
1302 ```
1303
1304 or replace the captured variable with a constant or a static item:
1305
1306 ```
1307 fn foo() {
1308     static mut X: u32 = 4;
1309     const Y: u32 = 5;
1310     fn bar() -> u32 {
1311         unsafe {
1312             X = 3;
1313         }
1314         Y
1315     }
1316 }
1317 ```
1318 "##,
1319
1320 E0435: r##"
1321 A non-constant value was used in a constant expression.
1322
1323 Erroneous code example:
1324
1325 ```compile_fail,E0435
1326 let foo = 42;
1327 let a: [u8; foo]; // error: attempt to use a non-constant value in a constant
1328 ```
1329
1330 To fix this error, please replace the value with a constant. Example:
1331
1332 ```
1333 let a: [u8; 42]; // ok!
1334 ```
1335
1336 Or:
1337
1338 ```
1339 const FOO: usize = 42;
1340 let a: [u8; FOO]; // ok!
1341 ```
1342 "##,
1343
1344 E0437: r##"
1345 Trait implementations can only implement associated types that are members of
1346 the trait in question. This error indicates that you attempted to implement
1347 an associated type whose name does not match the name of any associated type
1348 in the trait.
1349
1350 Erroneous code example:
1351
1352 ```compile_fail,E0437
1353 trait Foo {}
1354
1355 impl Foo for i32 {
1356     type Bar = bool;
1357 }
1358 ```
1359
1360 The solution to this problem is to remove the extraneous associated type:
1361
1362 ```
1363 trait Foo {}
1364
1365 impl Foo for i32 {}
1366 ```
1367 "##,
1368
1369 E0438: r##"
1370 Trait implementations can only implement associated constants that are
1371 members of the trait in question. This error indicates that you
1372 attempted to implement an associated constant whose name does not
1373 match the name of any associated constant in the trait.
1374
1375 Erroneous code example:
1376
1377 ```compile_fail,E0438
1378 trait Foo {}
1379
1380 impl Foo for i32 {
1381     const BAR: bool = true;
1382 }
1383 ```
1384
1385 The solution to this problem is to remove the extraneous associated constant:
1386
1387 ```
1388 trait Foo {}
1389
1390 impl Foo for i32 {}
1391 ```
1392 "##,
1393
1394 E0466: r##"
1395 Macro import declarations were malformed.
1396
1397 Erroneous code examples:
1398
1399 ```compile_fail,E0466
1400 #[macro_use(a_macro(another_macro))] // error: invalid import declaration
1401 extern crate core as some_crate;
1402
1403 #[macro_use(i_want = "some_macros")] // error: invalid import declaration
1404 extern crate core as another_crate;
1405 ```
1406
1407 This is a syntax error at the level of attribute declarations. The proper
1408 syntax for macro imports is the following:
1409
1410 ```ignore (cannot-doctest-multicrate-project)
1411 // In some_crate:
1412 #[macro_export]
1413 macro_rules! get_tacos {
1414     ...
1415 }
1416
1417 #[macro_export]
1418 macro_rules! get_pimientos {
1419     ...
1420 }
1421
1422 // In your crate:
1423 #[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
1424 extern crate some_crate;               // `get_pimientos` macros from some_crate
1425 ```
1426
1427 If you would like to import all exported macros, write `macro_use` with no
1428 arguments.
1429 "##,
1430
1431 E0468: r##"
1432 A non-root module attempts to import macros from another crate.
1433
1434 Example of erroneous code:
1435
1436 ```compile_fail,E0468
1437 mod foo {
1438     #[macro_use(debug_assert)]  // error: must be at crate root to import
1439     extern crate core;          //        macros from another crate
1440     fn run_macro() { debug_assert!(true); }
1441 }
1442 ```
1443
1444 Only `extern crate` imports at the crate root level are allowed to import
1445 macros.
1446
1447 Either move the macro import to crate root or do without the foreign macros.
1448 This will work:
1449
1450 ```
1451 #[macro_use(debug_assert)]
1452 extern crate core;
1453
1454 mod foo {
1455     fn run_macro() { debug_assert!(true); }
1456 }
1457 # fn main() {}
1458 ```
1459 "##,
1460
1461 E0469: r##"
1462 A macro listed for import was not found.
1463
1464 Erroneous code example:
1465
1466 ```compile_fail,E0469
1467 #[macro_use(drink, be_merry)] // error: imported macro not found
1468 extern crate alloc;
1469
1470 fn main() {
1471     // ...
1472 }
1473 ```
1474
1475 Either the listed macro is not contained in the imported crate, or it is not
1476 exported from the given crate.
1477
1478 This could be caused by a typo. Did you misspell the macro's name?
1479
1480 Double-check the names of the macros listed for import, and that the crate
1481 in question exports them.
1482
1483 A working version would be:
1484
1485 ```ignore (cannot-doctest-multicrate-project)
1486 // In some_crate crate:
1487 #[macro_export]
1488 macro_rules! eat {
1489     ...
1490 }
1491
1492 #[macro_export]
1493 macro_rules! drink {
1494     ...
1495 }
1496
1497 // In your crate:
1498 #[macro_use(eat, drink)]
1499 extern crate some_crate; //ok!
1500 ```
1501 "##,
1502
1503 E0530: r##"
1504 A binding shadowed something it shouldn't.
1505
1506 Erroneous code example:
1507
1508 ```compile_fail,E0530
1509 static TEST: i32 = 0;
1510
1511 let r: (i32, i32) = (0, 0);
1512 match r {
1513     TEST => {} // error: match bindings cannot shadow statics
1514 }
1515 ```
1516
1517 To fix this error, just change the binding's name in order to avoid shadowing
1518 one of the following:
1519
1520 * struct name
1521 * struct/enum variant
1522 * static
1523 * const
1524 * associated const
1525
1526 Fixed example:
1527
1528 ```
1529 static TEST: i32 = 0;
1530
1531 let r: (i32, i32) = (0, 0);
1532 match r {
1533     something => {} // ok!
1534 }
1535 ```
1536 "##,
1537
1538 E0531: r##"
1539 An unknown tuple struct/variant has been used.
1540
1541 Erroneous code example:
1542
1543 ```compile_fail,E0531
1544 let Type(x) = Type(12); // error!
1545 match Bar(12) {
1546     Bar(x) => {} // error!
1547     _ => {}
1548 }
1549 ```
1550
1551 In most cases, it's either a forgotten import or a typo. However, let's look at
1552 how you can have such a type:
1553
1554 ```edition2018
1555 struct Type(u32); // this is a tuple struct
1556
1557 enum Foo {
1558     Bar(u32), // this is a tuple variant
1559 }
1560
1561 use Foo::*; // To use Foo's variant directly, we need to import them in
1562             // the scope.
1563 ```
1564
1565 Either way, it should work fine with our previous code:
1566
1567 ```edition2018
1568 struct Type(u32);
1569
1570 enum Foo {
1571     Bar(u32),
1572 }
1573 use Foo::*;
1574
1575 let Type(x) = Type(12); // ok!
1576 match Type(12) {
1577     Type(x) => {} // ok!
1578     _ => {}
1579 }
1580 ```
1581 "##,
1582
1583 E0532: r##"
1584 Pattern arm did not match expected kind.
1585
1586 Erroneous code example:
1587
1588 ```compile_fail,E0532
1589 enum State {
1590     Succeeded,
1591     Failed(String),
1592 }
1593
1594 fn print_on_failure(state: &State) {
1595     match *state {
1596         // error: expected unit struct, unit variant or constant, found tuple
1597         //        variant `State::Failed`
1598         State::Failed => println!("Failed"),
1599         _ => ()
1600     }
1601 }
1602 ```
1603
1604 To fix this error, ensure the match arm kind is the same as the expression
1605 matched.
1606
1607 Fixed example:
1608
1609 ```
1610 enum State {
1611     Succeeded,
1612     Failed(String),
1613 }
1614
1615 fn print_on_failure(state: &State) {
1616     match *state {
1617         State::Failed(ref msg) => println!("Failed with {}", msg),
1618         _ => ()
1619     }
1620 }
1621 ```
1622 "##,
1623
1624 E0573: r##"
1625 Something other than a type has been used when one was expected.
1626
1627 Erroneous code examples:
1628
1629 ```compile_fail,E0573
1630 enum Dragon {
1631     Born,
1632 }
1633
1634 fn oblivion() -> Dragon::Born { // error!
1635     Dragon::Born
1636 }
1637
1638 const HOBBIT: u32 = 2;
1639 impl HOBBIT {} // error!
1640
1641 enum Wizard {
1642     Gandalf,
1643     Saruman,
1644 }
1645
1646 trait Isengard {
1647     fn wizard(_: Wizard::Saruman); // error!
1648 }
1649 ```
1650
1651 In all these errors, a type was expected. For example, in the first error, if
1652 we want to return the `Born` variant from the `Dragon` enum, we must set the
1653 function to return the enum and not its variant:
1654
1655 ```
1656 enum Dragon {
1657     Born,
1658 }
1659
1660 fn oblivion() -> Dragon { // ok!
1661     Dragon::Born
1662 }
1663 ```
1664
1665 In the second error, you can't implement something on an item, only on types.
1666 We would need to create a new type if we wanted to do something similar:
1667
1668 ```
1669 struct Hobbit(u32); // we create a new type
1670
1671 const HOBBIT: Hobbit = Hobbit(2);
1672 impl Hobbit {} // ok!
1673 ```
1674
1675 In the third case, we tried to only expect one variant of the `Wizard` enum,
1676 which is not possible. To make this work, we need to using pattern matching
1677 over the `Wizard` enum:
1678
1679 ```
1680 enum Wizard {
1681     Gandalf,
1682     Saruman,
1683 }
1684
1685 trait Isengard {
1686     fn wizard(w: Wizard) { // ok!
1687         match w {
1688             Wizard::Saruman => {
1689                 // do something
1690             }
1691             _ => {} // ignore everything else
1692         }
1693     }
1694 }
1695 ```
1696 "##,
1697
1698 E0574: r##"
1699 Something other than a struct, variant or union has been used when one was
1700 expected.
1701
1702 Erroneous code example:
1703
1704 ```compile_fail,E0574
1705 mod Mordor {}
1706
1707 let sauron = Mordor { x: () }; // error!
1708
1709 enum Jak {
1710     Daxter { i: isize },
1711 }
1712
1713 let eco = Jak::Daxter { i: 1 };
1714 match eco {
1715     Jak { i } => {} // error!
1716 }
1717 ```
1718
1719 In all these errors, a type was expected. For example, in the first error,
1720 we tried to instantiate the `Mordor` module, which is impossible. If you want
1721 to instantiate a type inside a module, you can do it as follow:
1722
1723 ```
1724 mod Mordor {
1725     pub struct TheRing {
1726         pub x: usize,
1727     }
1728 }
1729
1730 let sauron = Mordor::TheRing { x: 1 }; // ok!
1731 ```
1732
1733 In the second error, we tried to bind the `Jak` enum directly, which is not
1734 possible: you can only bind one of its variants. To do so:
1735
1736 ```
1737 enum Jak {
1738     Daxter { i: isize },
1739 }
1740
1741 let eco = Jak::Daxter { i: 1 };
1742 match eco {
1743     Jak::Daxter { i } => {} // ok!
1744 }
1745 ```
1746 "##,
1747
1748 E0575: r##"
1749 Something other than a type or an associated type was given.
1750
1751 Erroneous code example:
1752
1753 ```compile_fail,E0575
1754 enum Rick { Morty }
1755
1756 let _: <u8 as Rick>::Morty; // error!
1757
1758 trait Age {
1759     type Empire;
1760     fn Mythology() {}
1761 }
1762
1763 impl Age for u8 {
1764     type Empire = u16;
1765 }
1766
1767 let _: <u8 as Age>::Mythology; // error!
1768 ```
1769
1770 In both cases, we're declaring a variable (called `_`) and we're giving it a
1771 type. However, `<u8 as Rick>::Morty` and `<u8 as Age>::Mythology` aren't types,
1772 therefore the compiler throws an error.
1773
1774 `<u8 as Rick>::Morty` is an enum variant, you cannot use a variant as a type,
1775 you have to use the enum directly:
1776
1777 ```
1778 enum Rick { Morty }
1779
1780 let _: Rick; // ok!
1781 ```
1782
1783 `<u8 as Age>::Mythology` is a trait method, which is definitely not a type.
1784 However, the `Age` trait provides an associated type `Empire` which can be
1785 used as a type:
1786
1787 ```
1788 trait Age {
1789     type Empire;
1790     fn Mythology() {}
1791 }
1792
1793 impl Age for u8 {
1794     type Empire = u16;
1795 }
1796
1797 let _: <u8 as Age>::Empire; // ok!
1798 ```
1799 "##,
1800
1801 E0576: r##"
1802 An associated item wasn't found in the given type.
1803
1804 Erroneous code example:
1805
1806 ```compile_fail,E0576
1807 trait Hello {
1808     type Who;
1809
1810     fn hello() -> <Self as Hello>::You; // error!
1811 }
1812 ```
1813
1814 In this example, we tried to use the non-existent associated type `You` of the
1815 `Hello` trait. To fix this error, use an existing associated type:
1816
1817 ```
1818 trait Hello {
1819     type Who;
1820
1821     fn hello() -> <Self as Hello>::Who; // ok!
1822 }
1823 ```
1824 "##,
1825
1826 E0603: r##"
1827 A private item was used outside its scope.
1828
1829 Erroneous code example:
1830
1831 ```compile_fail,E0603
1832 mod SomeModule {
1833     const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
1834                                             // can't use it outside of the
1835                                             // `SomeModule` module.
1836 }
1837
1838 println!("const value: {}", SomeModule::PRIVATE); // error: constant `PRIVATE`
1839                                                   //        is private
1840 ```
1841
1842 In order to fix this error, you need to make the item public by using the `pub`
1843 keyword. Example:
1844
1845 ```
1846 mod SomeModule {
1847     pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
1848                                                 // `pub` keyword.
1849 }
1850
1851 println!("const value: {}", SomeModule::PRIVATE); // ok!
1852 ```
1853 "##,
1854
1855 E0659: r##"
1856 An item usage is ambiguous.
1857
1858 Erroneous code example:
1859
1860 ```compile_fail,edition2018,E0659
1861 pub mod moon {
1862     pub fn foo() {}
1863 }
1864
1865 pub mod earth {
1866     pub fn foo() {}
1867 }
1868
1869 mod collider {
1870     pub use crate::moon::*;
1871     pub use crate::earth::*;
1872 }
1873
1874 fn main() {
1875     crate::collider::foo(); // ERROR: `foo` is ambiguous
1876 }
1877 ```
1878
1879 This error generally appears when two items with the same name are imported into
1880 a module. Here, the `foo` functions are imported and reexported from the
1881 `collider` module and therefore, when we're using `collider::foo()`, both
1882 functions collide.
1883
1884 To solve this error, the best solution is generally to keep the path before the
1885 item when using it. Example:
1886
1887 ```edition2018
1888 pub mod moon {
1889     pub fn foo() {}
1890 }
1891
1892 pub mod earth {
1893     pub fn foo() {}
1894 }
1895
1896 mod collider {
1897     pub use crate::moon;
1898     pub use crate::earth;
1899 }
1900
1901 fn main() {
1902     crate::collider::moon::foo(); // ok!
1903     crate::collider::earth::foo(); // ok!
1904 }
1905 ```
1906 "##,
1907
1908 E0671: r##"
1909 #### Note: this error code is no longer emitted by the compiler.
1910
1911 Const parameters cannot depend on type parameters.
1912 The following is therefore invalid:
1913 ```compile_fail,E0741
1914 #![feature(const_generics)]
1915
1916 fn const_id<T, const N: T>() -> T { // error
1917     N
1918 }
1919 ```
1920 "##,
1921
1922 E0735: r##"
1923 Type parameter defaults cannot use `Self` on structs, enums, or unions.
1924
1925 Erroneous code example:
1926
1927 ```compile_fail,E0735
1928 struct Foo<X = Box<Self>> {
1929     field1: Option<X>,
1930     field2: Option<X>,
1931 }
1932 // error: type parameters cannot use `Self` in their defaults.
1933 ```
1934 "##,
1935
1936 E0741: r##"
1937 Visibility is restricted to a module which isn't an ancestor of the current
1938 item.
1939
1940 Erroneous code example:
1941
1942 ```compile_fail,E0741,edition2018
1943 pub mod Sea {}
1944
1945 pub (in crate::Sea) struct Shark; // error!
1946
1947 fn main() {}
1948 ```
1949
1950 To fix this error, we need to move the `Shark` struct inside the `Sea` module:
1951
1952 ```edition2018
1953 pub mod Sea {
1954     pub (in crate::Sea) struct Shark; // ok!
1955 }
1956
1957 fn main() {}
1958 ```
1959
1960 Of course, you can do it as long as the module you're referring to is an
1961 ancestor:
1962
1963 ```edition2018
1964 pub mod Earth {
1965     pub mod Sea {
1966         pub (in crate::Earth) struct Shark; // ok!
1967     }
1968 }
1969
1970 fn main() {}
1971 ```
1972 "##,
1973
1974 ;
1975 //  E0153, unused error code
1976 //  E0157, unused error code
1977 //  E0257,
1978 //  E0258,
1979 //  E0402, // cannot use an outer type parameter in this context
1980 //  E0406, merged into 420
1981 //  E0410, merged into 408
1982 //  E0413, merged into 530
1983 //  E0414, merged into 530
1984 //  E0417, merged into 532
1985 //  E0418, merged into 532
1986 //  E0419, merged into 531
1987 //  E0420, merged into 532
1988 //  E0421, merged into 531
1989 //  E0427, merged into 530
1990 //  E0467, removed
1991 //  E0470, removed
1992     E0577,
1993     E0578,
1994 }