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.
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.
11 #![allow(non_snake_case)]
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! {
19 Imports (`use` statements) are not allowed after non-item statements, such as
20 variable declarations and expression statements.
22 Here is an example that demonstrates the error:
26 // Variable declaration before import
33 The solution is to declare the imports at the top of the block, function, or
36 Here is the previous example again, with the correct order:
46 See the Declaration Statements section of the reference for more information
47 about what constitutes an Item declaration and what does not:
49 https://doc.rust-lang.org/reference.html#statements
53 Two items of the same name cannot be imported without rebinding one of the
54 items under a new local name.
56 An example of this error:
60 use bar::*; // error, do `use foo::baz as quux` instead on the previous line
75 Two items of the same name cannot be imported without rebinding one of the
76 items under a new local name.
78 An example of this error:
82 use bar::baz; // error, do `use bar::baz as quux` instead
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:
106 use foo::MyTrait::do_something;
109 It's invalid to directly import methods belonging to a trait or concrete type.
113 You can't import a value whose name is the same as another value defined in the
116 An example of this error:
119 use bar::foo; // error, do `use bar::foo as baz` instead
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.
135 An example of this error:
138 use foo::Bar; // error
151 The name chosen for an external crate conflicts with another external crate that
152 has been imported into the current module.
158 extern crate crate_a as a;
161 The solution is to choose a different name that doesn't conflict with any
162 external crate imported into the current module.
168 extern crate crate_a as other_name;
173 The name for an item declaration conflicts with an external crate's name.
183 There are two possible solutions:
185 Solution #1: Rename the item.
193 Solution #2: Import the crate with a different name.
196 extern crate abc as xyz;
201 See the Declaration Statements section of the reference for more information
202 about what constitutes an Item declaration and what does not:
204 https://doc.rust-lang.org/reference.html#statements
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:
218 To fix this, simply name it something else.
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:
224 impl<u8> MyTrait for Option<u8> {
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:
233 impl<T> MyTrait for Option<T> {
238 On the other hand, if you wished to refer to the specific type `u8`, remove it
239 from the type parameter list:
242 impl MyTrait for Option<u8> {
246 See the Types section of the reference for more information about the primitive
249 https://doc.rust-lang.org/reference.html#types
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.
256 Here is an example that demonstrates the error:
265 The solution to this problem is to ensure that the items that you are
266 re-exporting are themselves marked with `pub`:
270 pub const X: u32 = 1;
275 See the 'Use Declarations' section of the reference for more information
278 https://doc.rust-lang.org/reference.html#use-declarations
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.
285 Here is an example that demonstrates the error:
289 pub const X: u32 = 1;
294 The solution to this problem is to ensure that the module that you are
295 re-exporting is itself marked with `pub`:
299 pub const X: u32 = 1;
304 See the 'Use Declarations' section of the reference for more information
307 https://doc.rust-lang.org/reference.html#use-declarations
311 Inner items do not inherit type parameters from the functions they are
312 embedded in. For example, this will not compile:
316 fn bar(y: T) { // T is defined in the "outer" function
327 type MaybeT = Option<T>;
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.
346 There are a couple of solutions for this.
348 If the item is a function, you may use a closure:
352 let bar = |y: T| { // explicit type annotation may not be necessary
359 For a generic item, you can copy over the parameters:
372 type MaybeT<T> = Option<T>;
376 Be sure to copy over any bounds as well:
379 fn foo<T: Copy>(x: T) {
380 fn bar<T: Copy>(y: T) {
388 fn foo<T: Copy>(x: T) {
389 struct Foo<T: Copy> {
395 This may require additional type hints in the function body.
397 In case the item is a function inside an `impl`, defining a private helper
398 function might be easier:
402 pub fn foo(&self, x: T) {
405 fn bar(&self, y: T) {
411 For default impls in traits, the private helper solution won't work, however
412 closures or copying the parameters should still work.
416 Some type parameters have the same name. Example of erroneous code:
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
423 Please verify that none of the type parameterss are misspelled, and rename any
424 clashing parameters. Example:
427 fn foo<T, Y>(s: T, u: Y) {} // ok!
432 You tried to implement something which was not a trait on an object. Example of
439 impl Foo for Bar {} // error: `Foo` is not a trait
442 Please verify that you didn't misspell the trait's name or otherwise use the
443 wrong identifier. Example:
451 impl Foo for Bar { // ok!
452 // functions implementation
458 An unknown trait was implemented. Example of erroneous code:
463 impl SomeTrait for Foo {} // error: use of undeclared trait name `SomeTrait`
466 Please verify that the name of the trait wasn't misspelled and ensure that it
467 was imported. Example:
471 use some_file::SomeTrait;
480 impl SomeTrait for Foo { // ok!
481 // implements functions
487 A definition of a method not in the implemented trait was given in a trait
488 implementation. Example of erroneous code:
499 fn b() {} // error: method `b` is not a member of trait `Foo`
503 Please verify you didn't misspell the method name and you used the correct
504 trait. First example:
540 The `Self` keyword was used outside an impl or a trait. Erroneous
544 <Self>::foo; // error: use of `Self` outside of an impl or trait
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:
557 fn bar() -> Self::Bar; // like this
561 However, be careful when two types has a common associated type:
572 trait Baz : Foo + Foo2 {
573 fn bar() -> Self::Bar;
574 // error: ambiguous associated type `Bar` in bounds of `Self`
578 This problem can be solved by specifying from which trait we want
579 to use the `Bar` type:
582 trait Baz : Foo + Foo2 {
583 fn bar() -> <Self as Foo>::Bar; // ok!
589 An undeclared type name was used. Example of erroneous codes:
592 impl Something {} // error: use of undeclared type name `Something`
595 fn bar(N); // error: use of undeclared type name `N`
598 fn foo(x: T) {} // error: use of undeclared type name `T`
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:
607 impl Something {} // ok!
612 fn bar(Self::N); // ok!
615 fn foo<T>(x: T) {} // ok!
620 A declaration shadows an enum variant or unit-like struct in scope.
621 Example of erroneous code:
626 let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
627 // unit-like struct in scope
631 To fix this error, rename the variable such that it doesn't shadow any enum
632 variable or structure in scope. Example:
637 let foo = 12i32; // ok!
645 let Foo = 12i32; // ok!
648 The goal here is to avoid a conflict of names.
652 More than one function parameter have the same name. Example of erroneous
656 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
657 // once in this parameter list
660 Please verify you didn't misspell parameters' name. Example:
663 fn foo(f: i32, g: i32) {} // ok!
668 An identifier is bound more than once in a pattern. Example of erroneous
673 (x, x) => {} // error: identifier `x` is bound more than once in the
678 Please verify you didn't misspell identifiers' name. Example:
686 Or maybe did you mean to unify? Consider using a guard:
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 */ }
697 A static variable was referenced in a pattern. Example of erroneous code:
700 static FOO : i32 = 0;
703 FOO => {} // error: static variables cannot be referenced in a
704 // pattern, use a `const` instead
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:
725 An unknown enum variant, struct or const was used. Example of
730 Something::Foo => {} // error: unresolved enum variant, struct
735 Please verify you didn't misspell it and the enum variant, struct or const has
736 been declared and imported into scope. Example:
744 match Something::NotFoo {
745 Something::Foo => {} // ok!
752 You are trying to use an identifier that is either undefined or not a
753 struct. For instance:
756 let x = Foo { x: 1, y: 2 };
760 In this case, `Foo` is undefined, so it inherently isn't anything, and
761 definitely not a struct.
766 let x = foo { x: 1, y: 2 };
770 In this case, `foo` is defined, but is not a struct, so Rust can't use
775 A `struct` variant name was used like a function name. Example of
779 struct Foo { a: bool};
782 // error: `Foo` is a struct variant name, but this expression uses
783 // it like a function name
786 Please verify you didn't misspell the name of what you actually wanted
787 to use here. Example:
790 fn Foo() -> u32 { 0 }
792 let f = Foo(); // ok!
797 The `self` keyword was used in a static method. Example of erroneous code:
806 self.bar(); // error: `self` is not available in a static method.
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:
829 An unresolved name was used. Example of erroneous codes:
832 something_that_doesnt_exist::foo;
833 // error: unresolved name `something_that_doesnt_exist::foo`
838 Self; // error: unresolved name `Self`
843 let x = unknown_variable; // error: unresolved name `unknown_variable`
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:
850 enum something_that_does_exist {
855 mod something_that_does_exist {
856 pub static foo : i32 = 0i32;
859 something_that_does_exist::foo; // ok!
862 let unknown_variable = 12u32;
863 let x = unknown_variable; // ok!
868 An undeclared label was used. Example of erroneous code:
872 break 'a; // error: use of undeclared label `'a`
876 Please verify you spelt or declare the label correctly. Example:
886 A type or module has been defined more than once. Example of erroneous
891 struct Bar; // error: duplicate definition of value `Bar`
894 Please verify you didn't misspell the type/module's name or remove/rename the
895 duplicated one. Example:
904 The `self` import appears more than once in the list. Erroneous code example:
907 use something::{self, self}; // error: `self` import can only appear once in
911 Please verify you didn't misspell the import name or remove the duplicated
912 `self` import. Example:
915 use something::self; // ok!
920 `self` import was made. Erroneous code example:
923 use {self}; // error: `self` import can only appear in an import list with a
927 You cannot import the current module into itself, please remove this import
928 or verify you didn't misspell it.
932 An import was unresolved. Erroneous code example:
935 use something::Foo; // error: unresolved import `something::Foo`.
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:
942 use something::Foo; // ok!
949 Or, if you tried to use a module from an external crate, you may have missed
950 the `extern crate` declaration:
953 extern crate homura; // Required to use the `homura` crate
960 Invalid import. Example of erroneous code:
963 use something_which_doesnt_exist;
964 // error: unresolved import `something_which_doesnt_exist`
967 Please verify you didn't misspell the import's name.
971 A non-constant value was used to initialise a constant. Example of erroneous
976 const FOO : u32 = foo; // error: attempt to use a non-constant value in a
980 To fix this error, please replace the value with a constant. Example:
983 const FOO : u32 = 42u32; // ok!
986 const OTHER_FOO : u32 = 42u32;
987 const FOO : u32 = OTHER_FOO; // ok!
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
997 Here is an example that demonstrates the error:
1007 The solution to this problem is to remove the extraneous associated type:
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.
1022 Here is an example that demonstrates the error:
1025 #![feature(associated_consts)]
1030 const BAR: bool = true;
1034 The solution to this problem is to remove the extraneous associated constant:
1045 register_diagnostics! {
1046 // E0153, unused error code
1047 // E0157, unused error code
1048 E0254, // import conflicts with imported crate in this module
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
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