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