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