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 trying
98 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
153 that has been imported into the current module.
155 Erroneous code example:
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 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.
212 Here is an example that demonstrates the error:
222 The solution to this problem is to ensure that the items that you are
223 re-exporting are themselves marked with `pub`:
227 pub const X: u32 = 1;
233 See the 'Use Declarations' section of the reference for more information on
236 https://doc.rust-lang.org/reference.html#use-declarations
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.
243 Here is an example that demonstrates the error:
247 pub const X: u32 = 1;
253 The solution to this problem is to ensure that the module that you are
254 re-exporting is itself marked with `pub`:
258 pub const X: u32 = 1;
264 See the 'Use Declarations' section of the reference for more information
267 https://doc.rust-lang.org/reference.html#use-declarations
271 Inner items do not inherit type parameters from the functions they are embedded
272 in. For example, this will not compile:
276 fn bar(y: T) { // T is defined in the "outer" function
287 type MaybeT = Option<T>;
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.
306 There are a couple of solutions for this.
308 If the item is a function, you may use a closure:
312 let bar = |y: T| { // explicit type annotation may not be necessary
319 For a generic item, you can copy over the parameters:
332 type MaybeT<T> = Option<T>;
336 Be sure to copy over any bounds as well:
339 fn foo<T: Copy>(x: T) {
340 fn bar<T: Copy>(y: T) {
348 fn foo<T: Copy>(x: T) {
349 struct Foo<T: Copy> {
355 This may require additional type hints in the function body.
357 In case the item is a function inside an `impl`, defining a private helper
358 function might be easier:
362 pub fn foo(&self, x: T) {
366 fn bar(&self, y: T) {
372 For default impls in traits, the private helper solution won't work, however
373 closures or copying the parameters should still work.
377 Some type parameters have the same name. Example of erroneous code:
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
384 Please verify that none of the type parameterss are misspelled, and rename any
385 clashing parameters. Example:
388 fn foo<T, Y>(s: T, u: Y) {} // ok!
393 You tried to implement something which was not a trait on an object. Example of
400 impl Foo for Bar {} // error: `Foo` is not a trait
403 Please verify that you didn't misspell the trait's name or otherwise use the
404 wrong identifier. Example:
412 impl Foo for Bar { // ok!
413 // functions implementation
419 The code refers to a trait that is not in scope. Example of erroneous code:
424 impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
427 Please verify that the name of the trait wasn't misspelled and ensure that it
428 was imported. Example:
432 use some_file::SomeTrait;
441 impl SomeTrait for Foo { // ok!
442 // implements functions
448 A definition of a method not in the implemented trait was given in a trait
449 implementation. Example of erroneous code:
460 fn b() {} // error: method `b` is not a member of trait `Foo`
464 Please verify you didn't misspell the method name and you used the correct
465 trait. First example:
501 The `Self` keyword was used outside an impl or a trait. Erroneous code example:
504 <Self>::foo; // error: use of `Self` outside of an impl or trait
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
517 fn bar() -> Self::Bar; // like this
521 However, be careful when two types have a common associated type:
532 trait Baz : Foo + Foo2 {
533 fn bar() -> Self::Bar;
534 // error: ambiguous associated type `Bar` in bounds of `Self`
538 This problem can be solved by specifying from which trait we want to use the
550 trait Baz : Foo + Foo2 {
551 fn bar() -> <Self as Foo>::Bar; // ok!
557 The type name used is not in scope. Example of erroneous codes:
560 impl Something {} // error: type name `Something` is not in scope
565 fn bar(N); // error: type name `N` is not in scope
570 fn foo(x: T) {} // type name `T` is not in scope
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:
579 impl Something {} // ok!
586 fn bar(Self::N); // ok!
591 fn foo<T>(x: T) {} // ok!
596 A declaration shadows an enum variant or unit-like struct in scope. Example of
602 let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
603 // unit-like struct in scope
606 To fix this error, rename the variable such that it doesn't shadow any enum
607 variable or structure in scope. Example:
612 let foo = 12i32; // ok!
620 let Foo = 12i32; // ok!
623 The goal here is to avoid a conflict of names.
627 A variable binding in an irrefutable pattern is shadowing the name of a
628 constant. Example of erroneous code:
633 let FOO = 5; // error: variable bindings cannot shadow constants
637 fn bar(FOO: u8) { // error: variable bindings cannot shadow constants
648 Introducing a new variable in Rust is done through a pattern. Thus you can have
649 `let` bindings like `let (a, b) = ...`. However, patterns also allow constants
650 in them, e.g. if you want to match over a constant:
656 (3, 4) => { .. }, // it is (3,4)
657 (FOO, 1) => { .. }, // it is (1,1)
658 (foo, 1) => { .. }, // it is (anything, 1)
659 // call the value in the first slot "foo"
660 _ => { .. } // it is anything
664 Here, the second arm matches the value of `x` against the constant `FOO`,
665 whereas the third arm will accept any value of `x` and call it `foo`.
667 This works for `match`, however in cases where an irrefutable pattern is
668 required, constants can't be used. An irrefutable pattern is one which always
669 matches, whose purpose is only to bind variable names to values. These are
670 required by let, for, and function argument patterns.
672 Refutable patterns in such a situation do not make sense, for example:
675 let Some(x) = foo; // what if foo is None, instead?
677 let (1, x) = foo; // what if foo.0 is not 1?
679 let (SOME_CONST, x) = foo; // what if foo.0 is not SOME_CONST?
681 let SOME_CONST = foo; // what if foo is not SOME_CONST?
684 Thus, an irrefutable variable binding can't contain a constant.
686 To fix this error, just give the marked variable a different name.
690 More than one function parameter have the same name. Example of erroneous code:
693 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
694 // once in this parameter list
697 Please verify you didn't misspell parameters' name. Example:
700 fn foo(f: i32, g: i32) {} // ok!
705 An identifier is bound more than once in a pattern. Example of erroneous code:
709 (x, x) => {} // error: identifier `x` is bound more than once in the
714 Please verify you didn't misspell identifiers' name. Example:
722 Or maybe did you mean to unify? Consider using a guard:
726 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
727 (y, z, see) => { /* A and B unequal; do another thing */ }
733 A static variable was referenced in a pattern. Example of erroneous code:
736 static FOO : i32 = 0;
739 FOO => {} // error: static variables cannot be referenced in a
740 // pattern, use a `const` instead
745 The compiler needs to know the value of the pattern at compile time;
746 compile-time patterns can defined via const or enum items. Please verify
747 that the identifier is spelled correctly, and if so, use a const instead
748 of static to define it. Example:
761 An unknown enum variant, struct or const was used. Example of erroneous code:
765 Something::Foo => {} // error: unresolved enum variant, struct
770 Please verify you didn't misspell it and the enum variant, struct or const has
771 been declared and imported into scope. Example:
779 match Something::NotFoo {
780 Something::Foo => {} // ok!
787 You are trying to use an identifier that is either undefined or not a struct.
792 let x = Foo { x: 1, y: 2 };
796 In this case, `Foo` is undefined, so it inherently isn't anything, and
797 definitely not a struct.
802 let x = foo { x: 1, y: 2 };
806 In this case, `foo` is defined, but is not a struct, so Rust can't use it as
811 A `struct` variant name was used like a function name. Example of erroneous
815 struct Foo { a: bool};
818 // error: `Foo` is a struct variant name, but this expression uses
819 // it like a function name
822 Please verify you didn't misspell the name of what you actually wanted to use
826 fn Foo() -> u32 { 0 }
828 let f = Foo(); // ok!
833 The `self` keyword was used in a static method. Example of erroneous code:
842 self.bar(); // error: `self` is not available in a static method.
847 Please check if the method's argument list should have contained `self`,
848 `&self`, or `&mut self` (in case you didn't want to create a static
849 method), and add it if so. Example:
865 An unresolved name was used. Example of erroneous codes:
868 something_that_doesnt_exist::foo;
869 // error: unresolved name `something_that_doesnt_exist::foo`
875 Self; // error: unresolved name `Self`
881 let x = unknown_variable; // error: unresolved name `unknown_variable`
884 Please verify that the name wasn't misspelled and ensure that the
885 identifier being referred to is valid for the given situation. Example:
888 enum something_that_does_exist {
896 mod something_that_does_exist {
897 pub static foo : i32 = 0i32;
900 something_that_does_exist::foo; // ok!
906 let unknown_variable = 12u32;
907 let x = unknown_variable; // ok!
912 An undeclared label was used. Example of erroneous code:
916 break 'a; // error: use of undeclared label `'a`
920 Please verify you spelt or declare the label correctly. Example:
930 A type or module has been defined more than once. Example of erroneous
935 struct Bar; // error: duplicate definition of value `Bar`
938 Please verify you didn't misspell the type/module's name or remove/rename the
939 duplicated one. Example:
948 The `self` import appears more than once in the list. Erroneous code example:
951 use something::{self, self}; // error: `self` import can only appear once in
955 Please verify you didn't misspell the import name or remove the duplicated
956 `self` import. Example:
959 use something::self; // ok!
964 An invalid `self` import was made. Erroneous code example:
967 use {self}; // error: `self` import can only appear in an import list with a
971 You cannot import the current module into itself, please remove this import
972 or verify you didn't misspell it.
976 An import was unresolved. Erroneous code example:
979 use something::Foo; // error: unresolved import `something::Foo`.
982 Paths in `use` statements are relative to the crate root. To import items
983 relative to the current and parent modules, use the `self::` and `super::`
984 prefixes, respectively. Also verify that you didn't misspell the import
985 name and that the import exists in the module from where you tried to
989 use self::something::Foo; // ok!
996 Or, if you tried to use a module from an external crate, you may have missed
997 the `extern crate` declaration (which is usually placed in the crate root):
1000 extern crate homura; // Required to use the `homura` crate
1007 Invalid import. Example of erroneous code:
1010 use something_which_doesnt_exist;
1011 // error: unresolved import `something_which_doesnt_exist`
1014 Please verify you didn't misspell the import's name.
1018 This error indicates that a variable usage inside an inner function is invalid
1019 because the variable comes from a dynamic environment. Inner functions do not
1020 have access to their containing environment.
1022 Example of erroneous code:
1028 y // error: can't capture dynamic environment in a fn item; use the
1029 // || { ... } closure form instead.
1034 Functions do not capture local variables. To fix this error, you can replace the
1035 function with a closure:
1046 or replace the captured variable with a constant or a static item:
1050 static mut X: u32 = 4;
1063 A non-constant value was used to initialise a constant. Example of erroneous
1068 const FOO : u32 = foo; // error: attempt to use a non-constant value in a
1072 To fix this error, please replace the value with a constant. Example:
1075 const FOO : u32 = 42u32; // ok!
1081 const OTHER_FOO : u32 = 42u32;
1082 const FOO : u32 = OTHER_FOO; // ok!
1087 Trait implementations can only implement associated types that are members of
1088 the trait in question. This error indicates that you attempted to implement
1089 an associated type whose name does not match the name of any associated type
1092 Here is an example that demonstrates the error:
1102 The solution to this problem is to remove the extraneous associated type:
1112 Trait implementations can only implement associated constants that are
1113 members of the trait in question. This error indicates that you
1114 attempted to implement an associated constant whose name does not
1115 match the name of any associated constant in the trait.
1117 Here is an example that demonstrates the error:
1120 #![feature(associated_consts)]
1125 const BAR: bool = true;
1129 The solution to this problem is to remove the extraneous associated constant:
1140 register_diagnostics! {
1141 // E0153, unused error code
1142 // E0157, unused error code
1143 E0254, // import conflicts with imported crate in this module
1146 E0402, // cannot use an outer type parameter in this context
1147 E0406, // undeclared associated type
1148 E0408, // variable from pattern #1 is not bound in pattern #
1149 E0409, // variable is bound with different mode in pattern # than in
1151 E0410, // variable from pattern is not bound in pattern 1
1152 E0418, // is not an enum variant, struct or const
1153 E0420, // is not an associated const
1154 E0421, // unresolved associated const
1155 E0427, // cannot use `ref` binding mode with ...
1156 E0429, // `self` imports are only allowed within a { } list