]> git.lizzy.rs Git - rust.git/blob - src/librustc_resolve/diagnostics.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[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 E0154: r##"
19 Imports (`use` statements) are not allowed after non-item statements, such as
20 variable declarations and expression statements.
21
22 Here is an example that demonstrates the error:
23
24 ```
25 fn f() {
26     // Variable declaration before import
27     let x = 0;
28     use std::io::Read;
29     ...
30 }
31 ```
32
33 The solution is to declare the imports at the top of the block, function, or
34 file.
35
36 Here is the previous example again, with the correct order:
37
38 ```
39 fn f() {
40     use std::io::Read;
41     let x = 0;
42     ...
43 }
44 ```
45
46 See the Declaration Statements section of the reference for more information
47 about what constitutes an Item declaration and what does not:
48
49 https://doc.rust-lang.org/reference.html#statements
50 "##,
51
52 E0251: r##"
53 Two items of the same name cannot be imported without rebinding one of the
54 items under a new local name.
55
56 An example of this error:
57
58 ```
59 use foo::baz;
60 use bar::*; // error, do `use foo::baz as quux` instead on the previous line
61
62 fn main() {}
63
64 mod foo {
65     pub struct baz;
66 }
67
68 mod bar {
69     pub mod baz {}
70 }
71 ```
72 "##,
73
74 E0252: r##"
75 Two items of the same name cannot be imported without rebinding one of the
76 items under a new local name.
77
78 An example of this error:
79
80 ```
81 use foo::baz;
82 use bar::baz; // error, do `use bar::baz as quux` instead
83
84 fn main() {}
85
86 mod foo {
87     pub struct baz;
88 }
89
90 mod bar {
91     pub mod baz {}
92 }
93 ```
94 "##,
95
96 E0253: r##"
97 Attempt was made to import an unimportable value. This can happen when
98 trying to import a method from a trait. An example of this error:
99
100 ```
101 mod foo {
102     pub trait MyTrait {
103         fn do_something();
104     }
105 }
106 use foo::MyTrait::do_something;
107 ```
108
109 It's invalid to directly import methods belonging to a trait or concrete type.
110 "##,
111
112 E0255: r##"
113 You can't import a value whose name is the same as another value defined in the
114 module.
115
116 An example of this error:
117
118 ```
119 use bar::foo; // error, do `use bar::foo as baz` instead
120
121 fn foo() {}
122
123 mod bar {
124      pub fn foo() {}
125 }
126
127 fn main() {}
128 ```
129 "##,
130
131 E0256: r##"
132 You can't import a type or module when the name of the item being imported is
133 the same as another type or submodule defined in the module.
134
135 An example of this error:
136
137 ```
138 use foo::Bar; // error
139
140 type Bar = u32;
141
142 mod foo {
143     pub mod Bar { }
144 }
145
146 fn main() {}
147 ```
148 "##,
149
150 E0259: r##"
151 The name chosen for an external crate conflicts with another external crate that
152 has been imported into the current module.
153
154 Wrong example:
155
156 ```
157 extern crate a;
158 extern crate crate_a as a;
159 ```
160
161 The solution is to choose a different name that doesn't conflict with any
162 external crate imported into the current module.
163
164 Correct example:
165
166 ```
167 extern crate a;
168 extern crate crate_a as other_name;
169 ```
170 "##,
171
172 E0260: r##"
173 The name for an item declaration conflicts with an external crate's name.
174
175 For instance,
176
177 ```
178 extern crate abc;
179
180 struct abc;
181 ```
182
183 There are two possible solutions:
184
185 Solution #1: Rename the item.
186
187 ```
188 extern crate abc;
189
190 struct xyz;
191 ```
192
193 Solution #2: Import the crate with a different name.
194
195 ```
196 extern crate abc as xyz;
197
198 struct abc;
199 ```
200
201 See the Declaration Statements section of the reference for more information
202 about what constitutes an Item declaration and what does not:
203
204 https://doc.rust-lang.org/reference.html#statements
205 "##,
206
207 E0317: r##"
208 User-defined types or type parameters cannot shadow the primitive types.
209 This error indicates you tried to define a type, struct or enum with the same
210 name as an existing primitive type:
211
212 ```
213 struct u8 {
214     // ...
215 }
216 ```
217
218 To fix this, simply name it something else.
219
220 Such an error may also occur if you define a type parameter which shadows a
221 primitive type. An example would be something like:
222
223 ```
224 impl<u8> MyTrait for Option<u8> {
225     // ...
226 }
227 ```
228
229 In such a case, if you meant for `u8` to be a generic type parameter (i.e. any
230 type can be used in its place), use something like `T` instead:
231
232 ```
233 impl<T> MyTrait for Option<T> {
234     // ...
235 }
236 ```
237
238 On the other hand, if you wished to refer to the specific type `u8`, remove it
239 from the type parameter list:
240
241 ```
242 impl MyTrait for Option<u8> {
243     // ...
244 }
245
246 See the Types section of the reference for more information about the primitive
247 types:
248
249 https://doc.rust-lang.org/reference.html#types
250 "##,
251
252 E0364: r##"
253 Private items cannot be publicly re-exported.  This error indicates that
254 you attempted to `pub use` a type or value that was not itself public.
255
256 Here is an example that demonstrates the error:
257
258 ```
259 mod foo {
260     const X: u32 = 1;
261 }
262 pub use foo::X;
263 ```
264
265 The solution to this problem is to ensure that the items that you are
266 re-exporting are themselves marked with `pub`:
267
268 ```
269 mod foo {
270     pub const X: u32 = 1;
271 }
272 pub use foo::X;
273 ```
274
275 See the 'Use Declarations' section of the reference for more information
276 on this topic:
277
278 https://doc.rust-lang.org/reference.html#use-declarations
279 "##,
280
281 E0365: r##"
282 Private modules cannot be publicly re-exported.  This error indicates
283 that you attempted to `pub use` a module that was not itself public.
284
285 Here is an example that demonstrates the error:
286
287 ```
288 mod foo {
289     pub const X: u32 = 1;
290 }
291 pub use foo as foo2;
292
293 ```
294 The solution to this problem is to ensure that the module that you are
295 re-exporting is itself marked with `pub`:
296
297 ```
298 pub mod foo {
299     pub const X: u32 = 1;
300 }
301 pub use foo as foo2;
302 ```
303
304 See the 'Use Declarations' section of the reference for more information
305 on this topic:
306
307 https://doc.rust-lang.org/reference.html#use-declarations
308 "##,
309
310 E0401: r##"
311 Inner items do not inherit type parameters from the functions they are
312 embedded in. For example, this will not compile:
313
314 ```
315 fn foo<T>(x: T) {
316     fn bar(y: T) { // T is defined in the "outer" function
317         // ..
318     }
319     bar(x);
320 }
321 ```
322
323 nor will this:
324
325 ```
326 fn foo<T>(x: T) {
327     type MaybeT = Option<T>;
328     // ...
329 }
330 ```
331
332 or this:
333
334 ```
335 fn foo<T>(x: T) {
336     struct Foo {
337         x: T,
338     }
339     // ...
340 }
341 ```
342
343 Items inside functions are basically just like top-level items, except
344 that they can only be used from the function they are in.
345
346 There are a couple of solutions for this.
347
348 If the item is a function, you may use a closure:
349
350 ```
351 fn foo<T>(x: T) {
352     let bar = |y: T| { // explicit type annotation may not be necessary
353         // ..
354     }
355     bar(x);
356 }
357 ```
358
359 For a generic item, you can copy over the parameters:
360
361 ```
362 fn foo<T>(x: T) {
363     fn bar<T>(y: T) {
364         // ..
365     }
366     bar(x);
367 }
368 ```
369
370 ```
371 fn foo<T>(x: T) {
372     type MaybeT<T> = Option<T>;
373 }
374 ```
375
376 Be sure to copy over any bounds as well:
377
378 ```
379 fn foo<T: Copy>(x: T) {
380     fn bar<T: Copy>(y: T) {
381         // ..
382     }
383     bar(x);
384 }
385 ```
386
387 ```
388 fn foo<T: Copy>(x: T) {
389     struct Foo<T: Copy> {
390         x: T,
391     }
392 }
393 ```
394
395 This may require additional type hints in the function body.
396
397 In case the item is a function inside an `impl`, defining a private helper
398 function might be easier:
399
400 ```
401 impl<T> Foo<T> {
402     pub fn foo(&self, x: T) {
403         self.bar(x);
404     }
405     fn bar(&self, y: T) {
406         // ..
407     }
408 }
409 ```
410
411 For default impls in traits, the private helper solution won't work, however
412 closures or copying the parameters should still work.
413 "##,
414
415 E0403: r##"
416 Some type parameters have the same name. Example of erroneous code:
417
418 ```
419 fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
420                             //        parameter in this type parameter list
421 ```
422
423 Please verify that none of the type parameterss are misspelled, and rename any
424 clashing parameters. Example:
425
426 ```
427 fn foo<T, Y>(s: T, u: Y) {} // ok!
428 ```
429 "##,
430
431 E0404: r##"
432 You tried to implement something which was not a trait on an object. Example of
433 erroneous code:
434
435 ```
436 struct Foo;
437 struct Bar;
438
439 impl Foo for Bar {} // error: `Foo` is not a trait
440 ```
441
442 Please verify that you didn't misspell the trait's name or otherwise use the
443 wrong identifier. Example:
444
445 ```
446 trait Foo {
447     // some functions
448 }
449 struct Bar;
450
451 impl Foo for Bar { // ok!
452     // functions implementation
453 }
454 ```
455 "##,
456
457 E0405: r##"
458 An unknown trait was implemented. Example of erroneous code:
459
460 ```
461 struct Foo;
462
463 impl SomeTrait for Foo {} // error: use of undeclared trait name `SomeTrait`
464 ```
465
466 Please verify that the name of the trait wasn't misspelled and ensure that it
467 was imported. Example:
468
469 ```
470 // solution 1:
471 use some_file::SomeTrait;
472
473 // solution 2:
474 trait SomeTrait {
475     // some functions
476 }
477
478 struct Foo;
479
480 impl SomeTrait for Foo { // ok!
481     // implements functions
482 }
483 ```
484 "##,
485
486 E0407: r##"
487 A definition of a method not in the implemented trait was given in a trait
488 implementation. Example of erroneous code:
489
490 ```
491 trait Foo {
492     fn a();
493 }
494
495 struct Bar;
496
497 impl Foo for Bar {
498     fn a() {}
499     fn b() {} // error: method `b` is not a member of trait `Foo`
500 }
501 ```
502
503 Please verify you didn't misspell the method name and you used the correct
504 trait. First example:
505
506 ```
507 trait Foo {
508     fn a();
509     fn b();
510 }
511
512 struct Bar;
513
514 impl Foo for Bar {
515     fn a() {}
516     fn b() {} // ok!
517 }
518 ```
519
520 Second example:
521
522 ```
523 trait Foo {
524     fn a();
525 }
526
527 struct Bar;
528
529 impl Foo for Bar {
530     fn a() {}
531 }
532
533 impl Bar {
534     fn b() {}
535 }
536 ```
537 "##,
538
539 E0411: r##"
540 The `Self` keyword was used outside an impl or a trait. Erroneous
541 code example:
542
543 ```
544 <Self>::foo; // error: use of `Self` outside of an impl or trait
545 ```
546
547 The `Self` keyword represents the current type, which explains why it
548 can only be used inside an impl or a trait. It gives access to the
549 associated items of a type:
550
551 ```
552 trait Foo {
553     type Bar;
554 }
555
556 trait Baz : Foo {
557     fn bar() -> Self::Bar; // like this
558 }
559 ```
560
561 However, be careful when two types has a common associated type:
562
563 ```
564 trait Foo {
565     type Bar;
566 }
567
568 trait Foo2 {
569     type Bar;
570 }
571
572 trait Baz : Foo + Foo2 {
573     fn bar() -> Self::Bar;
574     // error: ambiguous associated type `Bar` in bounds of `Self`
575 }
576 ```
577
578 This problem can be solved by specifying from which trait we want
579 to use the `Bar` type:
580
581 ```
582 trait Baz : Foo + Foo2 {
583     fn bar() -> <Self as Foo>::Bar; // ok!
584 }
585 ```
586 "##,
587
588 E0412: r##"
589 An undeclared type name was used. Example of erroneous codes:
590
591 ```
592 impl Something {} // error: use of undeclared type name `Something`
593 // or:
594 trait Foo {
595     fn bar(N); // error: use of undeclared type name `N`
596 }
597 // or:
598 fn foo(x: T) {} // error: use of undeclared type name `T`
599 ```
600
601 To fix this error, please verify you didn't misspell the type name,
602 you did declare it or imported it into the scope. Examples:
603
604 ```
605 struct Something;
606
607 impl Something {} // ok!
608 // or:
609 trait Foo {
610     type N;
611
612     fn bar(Self::N); // ok!
613 }
614 //or:
615 fn foo<T>(x: T) {} // ok!
616 ```
617 "##,
618
619 E0413: r##"
620 A declaration shadows an enum variant or unit-like struct in scope.
621 Example of erroneous code:
622
623 ```
624 struct Foo;
625
626 let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
627                  //        unit-like struct in scope
628 ```
629
630
631 To fix this error, rename the variable such that it doesn't shadow any enum
632 variable or structure in scope. Example:
633
634 ```
635 struct Foo;
636
637 let foo = 12i32; // ok!
638 ```
639
640 Or:
641
642 ```
643 struct FooStruct;
644
645 let Foo = 12i32; // ok!
646 ```
647
648 The goal here is to avoid a conflict of names.
649 "##,
650
651 E0415: r##"
652 More than one function parameter have the same name. Example of erroneous
653 code:
654
655 ```
656 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
657                           //        once in this parameter list
658 ```
659
660 Please verify you didn't misspell parameters' name. Example:
661
662 ```
663 fn foo(f: i32, g: i32) {} // ok!
664 ```
665 "##,
666
667 E0416: r##"
668 An identifier is bound more than once in a pattern. Example of erroneous
669 code:
670
671 ```
672 match (1, 2) {
673     (x, x) => {} // error: identifier `x` is bound more than once in the
674                  //        same pattern
675 }
676 ```
677
678 Please verify you didn't misspell identifiers' name. Example:
679
680 ```
681 match (1, 2) {
682     (x, y) => {} // ok!
683 }
684 ```
685
686 Or maybe did you mean to unify? Consider using a guard:
687
688 ```
689 match (A, B, C) {
690     (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
691     (y, z, see) => { /* A and B unequal; do another thing */ }
692 }
693 ```
694 "##,
695
696 E0417: r##"
697 A static variable was referenced in a pattern. Example of erroneous code:
698
699 ```
700 static FOO : i32 = 0;
701
702 match 0 {
703     FOO => {} // error: static variables cannot be referenced in a
704               //        pattern, use a `const` instead
705     _ => {}
706 }
707 ```
708
709 The compiler needs to know the value of the pattern at compile time;
710 compile-time patterns can defined via const or enum items. Please verify
711 that the identifier is spelled correctly, and if so, use a const instead
712 of static to define it. Example:
713
714 ```
715 const FOO : i32 = 0;
716
717 match 0 {
718     FOO => {} // ok!
719     _ => {}
720 }
721 ```
722 "##,
723
724 E0419: r##"
725 An unknown enum variant, struct or const was used. Example of
726 erroneous code:
727
728 ```
729 match 0 {
730     Something::Foo => {} // error: unresolved enum variant, struct
731                          //        or const `Foo`
732 }
733 ```
734
735 Please verify you didn't misspell it and the enum variant, struct or const has
736 been declared and imported into scope. Example:
737
738 ```
739 enum Something {
740     Foo,
741     NotFoo,
742 }
743
744 match Something::NotFoo {
745     Something::Foo => {} // ok!
746     _ => {}
747 }
748 ```
749 "##,
750
751 E0422: r##"
752 You are trying to use an identifier that is either undefined or not a
753 struct. For instance:
754 ```
755 fn main () {
756     let x = Foo { x: 1, y: 2 };
757 }
758 ```
759
760 In this case, `Foo` is undefined, so it inherently isn't anything, and
761 definitely not a struct.
762
763 ```
764 fn main () {
765     let foo = 1;
766     let x = foo { x: 1, y: 2 };
767 }
768 ```
769
770 In this case, `foo` is defined, but is not a struct, so Rust can't use
771 it as one.
772 "##,
773
774 E0423: r##"
775 A `struct` variant name was used like a function name. Example of
776 erroneous code:
777
778 ```
779 struct Foo { a: bool};
780
781 let f = Foo();
782 // error: `Foo` is a struct variant name, but this expression uses
783 //        it like a function name
784 ```
785
786 Please verify you didn't misspell the name of what you actually wanted
787 to use here. Example:
788
789 ```
790 fn Foo() -> u32 { 0 }
791
792 let f = Foo(); // ok!
793 ```
794 "##,
795
796 E0424: r##"
797 The `self` keyword was used in a static method. Example of erroneous code:
798
799 ```
800 struct Foo;
801
802 impl Foo {
803     fn bar(self) {}
804
805     fn foo() {
806         self.bar(); // error: `self` is not available in a static method.
807     }
808 }
809 ```
810
811 Please check if the method's argument list should have contained `self`,
812 `&self`, or `&mut self` (in case you didn't want to create a static
813 method), and add it if so. Example:
814
815 ```
816 struct Foo;
817
818 impl Foo {
819     fn bar(self) {}
820
821     fn foo(self) {
822         self.bar(); // ok!
823     }
824 }
825 ```
826 "##,
827
828 E0425: r##"
829 An unresolved name was used. Example of erroneous codes:
830
831 ```
832 something_that_doesnt_exist::foo;
833 // error: unresolved name `something_that_doesnt_exist::foo`
834
835 // or:
836 trait Foo {
837     fn bar() {
838         Self; // error: unresolved name `Self`
839     }
840 }
841
842 // or:
843 let x = unknown_variable;  // error: unresolved name `unknown_variable`
844 ```
845
846 Please verify that the name wasn't misspelled and ensure that the
847 identifier being referred to is valid for the given situation. Example:
848
849 ```
850 enum something_that_does_exist {
851     foo
852 }
853
854 // or:
855 mod something_that_does_exist {
856     pub static foo : i32 = 0i32;
857 }
858
859 something_that_does_exist::foo; // ok!
860
861 // or:
862 let unknown_variable = 12u32;
863 let x = unknown_variable; // ok!
864 ```
865 "##,
866
867 E0426: r##"
868 An undeclared label was used. Example of erroneous code:
869
870 ```
871 loop {
872     break 'a; // error: use of undeclared label `'a`
873 }
874 ```
875
876 Please verify you spelt or declare the label correctly. Example:
877
878 ```
879 'a: loop {
880     break 'a; // ok!
881 }
882 ```
883 "##,
884
885 E0428: r##"
886 A type or module has been defined more than once. Example of erroneous
887 code:
888
889 ```
890 struct Bar;
891 struct Bar; // error: duplicate definition of value `Bar`
892 ```
893
894 Please verify you didn't misspell the type/module's name or remove/rename the
895 duplicated one. Example:
896
897 ```
898 struct Bar;
899 struct Bar2; // ok!
900 ```
901 "##,
902
903 E0430: r##"
904 The `self` import appears more than once in the list. Erroneous code example:
905
906 ```
907 use something::{self, self}; // error: `self` import can only appear once in
908                              //        the list
909 ```
910
911 Please verify you didn't misspell the import name or remove the duplicated
912 `self` import. Example:
913
914 ```
915 use something::self; // ok!
916 ```
917 "##,
918
919 E0431: r##"
920 `self` import was made. Erroneous code example:
921
922 ```
923 use {self}; // error: `self` import can only appear in an import list with a
924             //        non-empty prefix
925 ```
926
927 You cannot import the current module into itself, please remove this import
928 or verify you didn't misspell it.
929 "##,
930
931 E0432: r##"
932 An import was unresolved. Erroneous code example:
933
934 ```
935 use something::Foo; // error: unresolved import `something::Foo`.
936 ```
937
938 Please verify you didn't misspell the import name or the import does exist
939 in the module from where you tried to import it. Example:
940
941 ```
942 use something::Foo; // ok!
943
944 mod something {
945     pub struct Foo;
946 }
947 ```
948
949 Or, if you tried to use a module from an external crate, you may have missed
950 the `extern crate` declaration:
951
952 ```
953 extern crate homura; // Required to use the `homura` crate
954
955 use homura::Madoka;
956 ```
957 "##,
958
959 E0433: r##"
960 Invalid import. Example of erroneous code:
961
962 ```
963 use something_which_doesnt_exist;
964 // error: unresolved import `something_which_doesnt_exist`
965 ```
966
967 Please verify you didn't misspell the import's name.
968 "##,
969
970 E0435: r##"
971 A non-constant value was used to initialise a constant. Example of erroneous
972 code:
973
974 ```
975 let foo = 42u32;
976 const FOO : u32 = foo; // error: attempt to use a non-constant value in a
977                        //        constant
978 ```
979
980 To fix this error, please replace the value with a constant. Example:
981
982 ```
983 const FOO : u32 = 42u32; // ok!
984
985 // or:
986 const OTHER_FOO : u32 = 42u32;
987 const FOO : u32 = OTHER_FOO; // ok!
988 ```
989 "##,
990
991 E0437: r##"
992 Trait implementations can only implement associated types that are members of
993 the trait in question. This error indicates that you attempted to implement
994 an associated type whose name does not match the name of any associated type
995 in the trait.
996
997 Here is an example that demonstrates the error:
998
999 ```
1000 trait Foo {}
1001
1002 impl Foo for i32 {
1003     type Bar = bool;
1004 }
1005 ```
1006
1007 The solution to this problem is to remove the extraneous associated type:
1008
1009 ```
1010 trait Foo {}
1011
1012 impl Foo for i32 {}
1013 ```
1014 "##,
1015
1016 E0438: r##"
1017 Trait implementations can only implement associated constants that are
1018 members of the trait in question. This error indicates that you
1019 attempted to implement an associated constant whose name does not
1020 match the name of any associated constant in the trait.
1021
1022 Here is an example that demonstrates the error:
1023
1024 ```
1025 #![feature(associated_consts)]
1026
1027 trait Foo {}
1028
1029 impl Foo for i32 {
1030     const BAR: bool = true;
1031 }
1032 ```
1033
1034 The solution to this problem is to remove the extraneous associated constant:
1035
1036 ```
1037 trait Foo {}
1038
1039 impl Foo for i32 {}
1040 ```
1041 "##
1042
1043 }
1044
1045 register_diagnostics! {
1046 //  E0153, unused error code
1047 //  E0157, unused error code
1048     E0254, // import conflicts with imported crate in this module
1049     E0257,
1050     E0258,
1051     E0402, // cannot use an outer type parameter in this context
1052     E0406, // undeclared associated type
1053     E0408, // variable from pattern #1 is not bound in pattern #
1054     E0409, // variable is bound with different mode in pattern # than in
1055            // pattern #1
1056     E0410, // variable from pattern is not bound in pattern 1
1057     E0414, // only irrefutable patterns allowed here
1058     E0418, // is not an enum variant, struct or const
1059     E0420, // is not an associated const
1060     E0421, // unresolved associated const
1061     E0427, // cannot use `ref` binding mode with ...
1062     E0429, // `self` imports are only allowed within a { } list
1063     E0434, // can't capture dynamic environment in a fn item
1064 }