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:
107 use foo::MyTrait::do_something;
110 It's invalid to directly import methods belonging to a trait or concrete type.
114 You can't import a value whose name is the same as another value defined in the
117 An example of this error:
120 use bar::foo; // error, do `use bar::foo as baz` instead
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.
136 An example of this error:
139 use foo::Bar; // error
152 The name chosen for an external crate conflicts with another external crate that
153 has been imported into the current module.
159 extern crate crate_a as a;
162 The solution is to choose a different name that doesn't conflict with any
163 external crate imported into the current module.
169 extern crate crate_a as other_name;
174 The name for an item declaration conflicts with an external crate's name.
184 There are two possible solutions:
186 Solution #1: Rename the item.
194 Solution #2: Import the crate with a different name.
197 extern crate abc as xyz;
202 See the Declaration Statements section of the reference for more information
203 about what constitutes an Item declaration and what does not:
205 https://doc.rust-lang.org/reference.html#statements
209 User-defined types or type parameters cannot shadow the primitive types.
210 This error indicates you tried to define a type, struct or enum with the same
211 name as an existing primitive type:
219 To fix this, simply name it something else.
221 Such an error may also occur if you define a type parameter which shadows a
222 primitive type. An example would be something like:
225 impl<u8> MyTrait for Option<u8> {
230 In such a case, if you meant for `u8` to be a generic type parameter (i.e. any
231 type can be used in its place), use something like `T` instead:
234 impl<T> MyTrait for Option<T> {
239 On the other hand, if you wished to refer to the specific type `u8`, remove it
240 from the type parameter list:
243 impl MyTrait for Option<u8> {
247 See the Types section of the reference for more information about the primitive
250 https://doc.rust-lang.org/reference.html#types
254 Private items cannot be publicly re-exported. This error indicates that
255 you attempted to `pub use` a type or value that was not itself public.
257 Here is an example that demonstrates the error:
267 The solution to this problem is to ensure that the items that you are
268 re-exporting are themselves marked with `pub`:
272 pub const X: u32 = 1;
278 See the 'Use Declarations' section of the reference for more information
281 https://doc.rust-lang.org/reference.html#use-declarations
285 Private modules cannot be publicly re-exported. This error indicates
286 that you attempted to `pub use` a module that was not itself public.
288 Here is an example that demonstrates the error:
292 pub const X: u32 = 1;
298 The solution to this problem is to ensure that the module that you are
299 re-exporting is itself marked with `pub`:
303 pub const X: u32 = 1;
309 See the 'Use Declarations' section of the reference for more information
312 https://doc.rust-lang.org/reference.html#use-declarations
316 Inner items do not inherit type parameters from the functions they are
317 embedded in. For example, this will not compile:
321 fn bar(y: T) { // T is defined in the "outer" function
332 type MaybeT = Option<T>;
348 Items inside functions are basically just like top-level items, except
349 that they can only be used from the function they are in.
351 There are a couple of solutions for this.
353 If the item is a function, you may use a closure:
357 let bar = |y: T| { // explicit type annotation may not be necessary
364 For a generic item, you can copy over the parameters:
377 type MaybeT<T> = Option<T>;
381 Be sure to copy over any bounds as well:
384 fn foo<T: Copy>(x: T) {
385 fn bar<T: Copy>(y: T) {
393 fn foo<T: Copy>(x: T) {
394 struct Foo<T: Copy> {
400 This may require additional type hints in the function body.
402 In case the item is a function inside an `impl`, defining a private helper
403 function might be easier:
407 pub fn foo(&self, x: T) {
411 fn bar(&self, y: T) {
417 For default impls in traits, the private helper solution won't work, however
418 closures or copying the parameters should still work.
422 Some type parameters have the same name. Example of erroneous code:
425 fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
426 // parameter in this type parameter list
429 Please verify that none of the type parameterss are misspelled, and rename any
430 clashing parameters. Example:
433 fn foo<T, Y>(s: T, u: Y) {} // ok!
438 You tried to implement something which was not a trait on an object. Example of
445 impl Foo for Bar {} // error: `Foo` is not a trait
448 Please verify that you didn't misspell the trait's name or otherwise use the
449 wrong identifier. Example:
457 impl Foo for Bar { // ok!
458 // functions implementation
464 An unknown trait was implemented. Example of erroneous code:
469 impl SomeTrait for Foo {} // error: use of undeclared trait name `SomeTrait`
472 Please verify that the name of the trait wasn't misspelled and ensure that it
473 was imported. Example:
477 use some_file::SomeTrait;
486 impl SomeTrait for Foo { // ok!
487 // implements functions
493 A definition of a method not in the implemented trait was given in a trait
494 implementation. Example of erroneous code:
505 fn b() {} // error: method `b` is not a member of trait `Foo`
509 Please verify you didn't misspell the method name and you used the correct
510 trait. First example:
546 The `Self` keyword was used outside an impl or a trait. Erroneous
550 <Self>::foo; // error: use of `Self` outside of an impl or trait
553 The `Self` keyword represents the current type, which explains why it
554 can only be used inside an impl or a trait. It gives access to the
555 associated items of a type:
563 fn bar() -> Self::Bar; // like this
567 However, be careful when two types has a common associated type:
578 trait Baz : Foo + Foo2 {
579 fn bar() -> Self::Bar;
580 // error: ambiguous associated type `Bar` in bounds of `Self`
584 This problem can be solved by specifying from which trait we want
585 to use the `Bar` type:
596 trait Baz : Foo + Foo2 {
597 fn bar() -> <Self as Foo>::Bar; // ok!
603 An undeclared type name was used. Example of erroneous codes:
606 impl Something {} // error: use of undeclared type name `Something`
609 fn bar(N); // error: use of undeclared type name `N`
612 fn foo(x: T) {} // error: use of undeclared type name `T`
615 To fix this error, please verify you didn't misspell the type name,
616 you did declare it or imported it into the scope. Examples:
621 impl Something {} // ok!
628 fn bar(Self::N); // ok!
633 fn foo<T>(x: T) {} // ok!
638 A declaration shadows an enum variant or unit-like struct in scope.
639 Example of erroneous code:
644 let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
645 // unit-like struct in scope
648 To fix this error, rename the variable such that it doesn't shadow any enum
649 variable or structure in scope. Example:
654 let foo = 12i32; // ok!
662 let Foo = 12i32; // ok!
665 The goal here is to avoid a conflict of names.
669 More than one function parameter have the same name. Example of erroneous
673 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
674 // once in this parameter list
677 Please verify you didn't misspell parameters' name. Example:
680 fn foo(f: i32, g: i32) {} // ok!
685 An identifier is bound more than once in a pattern. Example of erroneous
690 (x, x) => {} // error: identifier `x` is bound more than once in the
695 Please verify you didn't misspell identifiers' name. Example:
703 Or maybe did you mean to unify? Consider using a guard:
707 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
708 (y, z, see) => { /* A and B unequal; do another thing */ }
714 A static variable was referenced in a pattern. Example of erroneous code:
717 static FOO : i32 = 0;
720 FOO => {} // error: static variables cannot be referenced in a
721 // pattern, use a `const` instead
726 The compiler needs to know the value of the pattern at compile time;
727 compile-time patterns can defined via const or enum items. Please verify
728 that the identifier is spelled correctly, and if so, use a const instead
729 of static to define it. Example:
742 An unknown enum variant, struct or const was used. Example of
747 Something::Foo => {} // error: unresolved enum variant, struct
752 Please verify you didn't misspell it and the enum variant, struct or const has
753 been declared and imported into scope. Example:
761 match Something::NotFoo {
762 Something::Foo => {} // ok!
769 You are trying to use an identifier that is either undefined or not a
770 struct. For instance:
774 let x = Foo { x: 1, y: 2 };
778 In this case, `Foo` is undefined, so it inherently isn't anything, and
779 definitely not a struct.
784 let x = foo { x: 1, y: 2 };
788 In this case, `foo` is defined, but is not a struct, so Rust can't use
793 A `struct` variant name was used like a function name. Example of
797 struct Foo { a: bool};
800 // error: `Foo` is a struct variant name, but this expression uses
801 // it like a function name
804 Please verify you didn't misspell the name of what you actually wanted
805 to use here. Example:
808 fn Foo() -> u32 { 0 }
810 let f = Foo(); // ok!
815 The `self` keyword was used in a static method. Example of erroneous code:
824 self.bar(); // error: `self` is not available in a static method.
829 Please check if the method's argument list should have contained `self`,
830 `&self`, or `&mut self` (in case you didn't want to create a static
831 method), and add it if so. Example:
847 An unresolved name was used. Example of erroneous codes:
850 something_that_doesnt_exist::foo;
851 // error: unresolved name `something_that_doesnt_exist::foo`
856 Self; // error: unresolved name `Self`
861 let x = unknown_variable; // error: unresolved name `unknown_variable`
864 Please verify that the name wasn't misspelled and ensure that the
865 identifier being referred to is valid for the given situation. Example:
868 enum something_that_does_exist {
876 mod something_that_does_exist {
877 pub static foo : i32 = 0i32;
880 something_that_does_exist::foo; // ok!
886 let unknown_variable = 12u32;
887 let x = unknown_variable; // ok!
892 An undeclared label was used. Example of erroneous code:
896 break 'a; // error: use of undeclared label `'a`
900 Please verify you spelt or declare the label correctly. Example:
910 A type or module has been defined more than once. Example of erroneous
915 struct Bar; // error: duplicate definition of value `Bar`
918 Please verify you didn't misspell the type/module's name or remove/rename the
919 duplicated one. Example:
928 The `self` import appears more than once in the list. Erroneous code example:
931 use something::{self, self}; // error: `self` import can only appear once in
935 Please verify you didn't misspell the import name or remove the duplicated
936 `self` import. Example:
939 use something::self; // ok!
944 `self` import was made. Erroneous code example:
947 use {self}; // error: `self` import can only appear in an import list with a
951 You cannot import the current module into itself, please remove this import
952 or verify you didn't misspell it.
956 An import was unresolved. Erroneous code example:
959 use something::Foo; // error: unresolved import `something::Foo`.
962 Please verify you didn't misspell the import name or the import does exist
963 in the module from where you tried to import it. Example:
966 use something::Foo; // ok!
973 Or, if you tried to use a module from an external crate, you may have missed
974 the `extern crate` declaration:
977 extern crate homura; // Required to use the `homura` crate
984 Invalid import. Example of erroneous code:
987 use something_which_doesnt_exist;
988 // error: unresolved import `something_which_doesnt_exist`
991 Please verify you didn't misspell the import's name.
995 A non-constant value was used to initialise a constant. Example of erroneous
1000 const FOO : u32 = foo; // error: attempt to use a non-constant value in a
1004 To fix this error, please replace the value with a constant. Example:
1007 const FOO : u32 = 42u32; // ok!
1013 const OTHER_FOO : u32 = 42u32;
1014 const FOO : u32 = OTHER_FOO; // ok!
1019 Trait implementations can only implement associated types that are members of
1020 the trait in question. This error indicates that you attempted to implement
1021 an associated type whose name does not match the name of any associated type
1024 Here is an example that demonstrates the error:
1034 The solution to this problem is to remove the extraneous associated type:
1044 Trait implementations can only implement associated constants that are
1045 members of the trait in question. This error indicates that you
1046 attempted to implement an associated constant whose name does not
1047 match the name of any associated constant in the trait.
1049 Here is an example that demonstrates the error:
1052 #![feature(associated_consts)]
1057 const BAR: bool = true;
1061 The solution to this problem is to remove the extraneous associated constant:
1072 register_diagnostics! {
1073 // E0153, unused error code
1074 // E0157, unused error code
1075 E0254, // import conflicts with imported crate in this module
1078 E0402, // cannot use an outer type parameter in this context
1079 E0406, // undeclared associated type
1080 E0408, // variable from pattern #1 is not bound in pattern #
1081 E0409, // variable is bound with different mode in pattern # than in
1083 E0410, // variable from pattern is not bound in pattern 1
1084 E0414, // only irrefutable patterns allowed here
1085 E0418, // is not an enum variant, struct or const
1086 E0420, // is not an associated const
1087 E0421, // unresolved associated const
1088 E0427, // cannot use `ref` binding mode with ...
1089 E0429, // `self` imports are only allowed within a { } list
1090 E0434, // can't capture dynamic environment in a fn item