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