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 An "or" pattern was used where the variable bindings are not consistently bound
504 Example of erroneous code:
508 Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
509 // not bound in pattern #2
514 Here, `y` is bound to the contents of the `Some` and can be used within the
515 block corresponding to the match arm. However, in case `x` is `None`, we have
516 not specified what `y` is, and the block will use a nonexistent variable.
518 To fix this error, either split into multiple match arms:
523 Some(y) => { /* use y */ }
524 None => { /* ... */ }
528 or, bind the variable to a field of the same type in all sub-patterns of the
534 (0, y) | (y, 0) => { /* use y */}
539 In this example, if `x` matches the pattern `(0, _)`, the second field is set
540 to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
541 cases `y` is set to some value.
545 An "or" pattern was used where the variable bindings are not consistently bound
548 Example of erroneous code:
553 (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
554 // different mode in pattern #2
555 // than in pattern #1
560 Here, `y` is bound by-value in one case and by-reference in the other.
562 To fix this error, just use the same mode in both cases.
563 Generally using `ref` or `ref mut` where not already used will fix this:
568 (0, ref y) | (ref y, 0) => { /* use y */}
573 Alternatively, split the pattern:
578 (y, 0) => { /* use y */ }
579 (0, ref y) => { /* use y */}
586 The `Self` keyword was used outside an impl or a trait. Erroneous code example:
589 <Self>::foo; // error: use of `Self` outside of an impl or trait
592 The `Self` keyword represents the current type, which explains why it can only
593 be used inside an impl or a trait. It gives access to the associated items of a
602 fn bar() -> Self::Bar; // like this
606 However, be careful when two types have a common associated type:
617 trait Baz : Foo + Foo2 {
618 fn bar() -> Self::Bar;
619 // error: ambiguous associated type `Bar` in bounds of `Self`
623 This problem can be solved by specifying from which trait we want to use the
635 trait Baz : Foo + Foo2 {
636 fn bar() -> <Self as Foo>::Bar; // ok!
642 The type name used is not in scope. Example of erroneous codes:
645 impl Something {} // error: type name `Something` is not in scope
650 fn bar(N); // error: type name `N` is not in scope
655 fn foo(x: T) {} // type name `T` is not in scope
658 To fix this error, please verify you didn't misspell the type name, you did
659 declare it or imported it into the scope. Examples:
664 impl Something {} // ok!
671 fn bar(Self::N); // ok!
676 fn foo<T>(x: T) {} // ok!
681 A declaration shadows an enum variant or unit-like struct in scope. Example of
687 let Foo = 12i32; // error: declaration of `Foo` shadows an enum variant or
688 // unit-like struct in scope
691 To fix this error, rename the variable such that it doesn't shadow any enum
692 variable or structure in scope. Example:
697 let foo = 12i32; // ok!
705 let Foo = 12i32; // ok!
708 The goal here is to avoid a conflict of names.
712 A variable binding in an irrefutable pattern is shadowing the name of a
713 constant. Example of erroneous code:
718 let FOO = 5; // error: variable bindings cannot shadow constants
722 fn bar(FOO: u8) { // error: variable bindings cannot shadow constants
733 Introducing a new variable in Rust is done through a pattern. Thus you can have
734 `let` bindings like `let (a, b) = ...`. However, patterns also allow constants
735 in them, e.g. if you want to match over a constant:
741 (3, 4) => { .. }, // it is (3,4)
742 (FOO, 1) => { .. }, // it is (1,1)
743 (foo, 1) => { .. }, // it is (anything, 1)
744 // call the value in the first slot "foo"
745 _ => { .. } // it is anything
749 Here, the second arm matches the value of `x` against the constant `FOO`,
750 whereas the third arm will accept any value of `x` and call it `foo`.
752 This works for `match`, however in cases where an irrefutable pattern is
753 required, constants can't be used. An irrefutable pattern is one which always
754 matches, whose purpose is only to bind variable names to values. These are
755 required by let, for, and function argument patterns.
757 Refutable patterns in such a situation do not make sense, for example:
760 let Some(x) = foo; // what if foo is None, instead?
762 let (1, x) = foo; // what if foo.0 is not 1?
764 let (SOME_CONST, x) = foo; // what if foo.0 is not SOME_CONST?
766 let SOME_CONST = foo; // what if foo is not SOME_CONST?
769 Thus, an irrefutable variable binding can't contain a constant.
771 To fix this error, just give the marked variable a different name.
775 More than one function parameter have the same name. Example of erroneous code:
778 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
779 // once in this parameter list
782 Please verify you didn't misspell parameters' name. Example:
785 fn foo(f: i32, g: i32) {} // ok!
790 An identifier is bound more than once in a pattern. Example of erroneous code:
794 (x, x) => {} // error: identifier `x` is bound more than once in the
799 Please verify you didn't misspell identifiers' name. Example:
807 Or maybe did you mean to unify? Consider using a guard:
811 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
812 (y, z, see) => { /* A and B unequal; do another thing */ }
818 A static variable was referenced in a pattern. Example of erroneous code:
821 static FOO : i32 = 0;
824 FOO => {} // error: static variables cannot be referenced in a
825 // pattern, use a `const` instead
830 The compiler needs to know the value of the pattern at compile time;
831 compile-time patterns can defined via const or enum items. Please verify
832 that the identifier is spelled correctly, and if so, use a const instead
833 of static to define it. Example:
846 An unknown enum variant, struct or const was used. Example of erroneous code:
850 Something::Foo => {} // error: unresolved enum variant, struct
855 Please verify you didn't misspell it and the enum variant, struct or const has
856 been declared and imported into scope. Example:
864 match Something::NotFoo {
865 Something::Foo => {} // ok!
872 You are trying to use an identifier that is either undefined or not a struct.
877 let x = Foo { x: 1, y: 2 };
881 In this case, `Foo` is undefined, so it inherently isn't anything, and
882 definitely not a struct.
887 let x = foo { x: 1, y: 2 };
891 In this case, `foo` is defined, but is not a struct, so Rust can't use it as
896 A `struct` variant name was used like a function name. Example of erroneous
900 struct Foo { a: bool};
903 // error: `Foo` is a struct variant name, but this expression uses
904 // it like a function name
907 Please verify you didn't misspell the name of what you actually wanted to use
911 fn Foo() -> u32 { 0 }
913 let f = Foo(); // ok!
918 The `self` keyword was used in a static method. Example of erroneous code:
927 self.bar(); // error: `self` is not available in a static method.
932 Please check if the method's argument list should have contained `self`,
933 `&self`, or `&mut self` (in case you didn't want to create a static
934 method), and add it if so. Example:
950 An unresolved name was used. Example of erroneous codes:
953 something_that_doesnt_exist::foo;
954 // error: unresolved name `something_that_doesnt_exist::foo`
960 Self; // error: unresolved name `Self`
966 let x = unknown_variable; // error: unresolved name `unknown_variable`
969 Please verify that the name wasn't misspelled and ensure that the
970 identifier being referred to is valid for the given situation. Example:
973 enum something_that_does_exist {
981 mod something_that_does_exist {
982 pub static foo : i32 = 0i32;
985 something_that_does_exist::foo; // ok!
991 let unknown_variable = 12u32;
992 let x = unknown_variable; // ok!
997 An undeclared label was used. Example of erroneous code:
1001 break 'a; // error: use of undeclared label `'a`
1005 Please verify you spelt or declare the label correctly. Example:
1015 A type or module has been defined more than once. Example of erroneous
1020 struct Bar; // error: duplicate definition of value `Bar`
1023 Please verify you didn't misspell the type/module's name or remove/rename the
1024 duplicated one. Example:
1033 The `self` import appears more than once in the list. Erroneous code example:
1036 use something::{self, self}; // error: `self` import can only appear once in
1040 Please verify you didn't misspell the import name or remove the duplicated
1041 `self` import. Example:
1044 use something::self; // ok!
1049 An invalid `self` import was made. Erroneous code example:
1052 use {self}; // error: `self` import can only appear in an import list with a
1056 You cannot import the current module into itself, please remove this import
1057 or verify you didn't misspell it.
1061 An import was unresolved. Erroneous code example:
1064 use something::Foo; // error: unresolved import `something::Foo`.
1067 Paths in `use` statements are relative to the crate root. To import items
1068 relative to the current and parent modules, use the `self::` and `super::`
1069 prefixes, respectively. Also verify that you didn't misspell the import
1070 name and that the import exists in the module from where you tried to
1074 use self::something::Foo; // ok!
1081 Or, if you tried to use a module from an external crate, you may have missed
1082 the `extern crate` declaration (which is usually placed in the crate root):
1085 extern crate homura; // Required to use the `homura` crate
1092 Invalid import. Example of erroneous code:
1095 use something_which_doesnt_exist;
1096 // error: unresolved import `something_which_doesnt_exist`
1099 Please verify you didn't misspell the import's name.
1103 This error indicates that a variable usage inside an inner function is invalid
1104 because the variable comes from a dynamic environment. Inner functions do not
1105 have access to their containing environment.
1107 Example of erroneous code:
1113 y // error: can't capture dynamic environment in a fn item; use the
1114 // || { ... } closure form instead.
1119 Functions do not capture local variables. To fix this error, you can replace the
1120 function with a closure:
1131 or replace the captured variable with a constant or a static item:
1135 static mut X: u32 = 4;
1148 A non-constant value was used to initialise a constant. Example of erroneous
1153 const FOO : u32 = foo; // error: attempt to use a non-constant value in a
1157 To fix this error, please replace the value with a constant. Example:
1160 const FOO : u32 = 42u32; // ok!
1166 const OTHER_FOO : u32 = 42u32;
1167 const FOO : u32 = OTHER_FOO; // ok!
1172 Trait implementations can only implement associated types that are members of
1173 the trait in question. This error indicates that you attempted to implement
1174 an associated type whose name does not match the name of any associated type
1177 Here is an example that demonstrates the error:
1187 The solution to this problem is to remove the extraneous associated type:
1197 Trait implementations can only implement associated constants that are
1198 members of the trait in question. This error indicates that you
1199 attempted to implement an associated constant whose name does not
1200 match the name of any associated constant in the trait.
1202 Here is an example that demonstrates the error:
1205 #![feature(associated_consts)]
1210 const BAR: bool = true;
1214 The solution to this problem is to remove the extraneous associated constant:
1225 register_diagnostics! {
1226 // E0153, unused error code
1227 // E0157, unused error code
1228 E0254, // import conflicts with imported crate in this module
1231 E0402, // cannot use an outer type parameter in this context
1232 E0406, // undeclared associated type
1233 // E0410, merged into 408
1234 E0418, // is not an enum variant, struct or const
1235 E0420, // is not an associated const
1236 E0421, // unresolved associated const
1237 E0427, // cannot use `ref` binding mode with ...
1238 E0429, // `self` imports are only allowed within a { } list