1 use syntax::{register_diagnostics, register_long_diagnostics};
3 // Error messages for EXXXX errors. Each message should start and end with a
4 // new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
5 // use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
6 register_long_diagnostics! {
9 Type parameter defaults can only use parameters that occur before them.
10 Erroneous code example:
13 struct Foo<T=U, U=()> {
17 // error: type parameters with a default cannot use forward declared
21 Since type parameters are evaluated in-order, you may be able to fix this issue
25 struct Foo<U=(), T=U> {
31 Please also verify that this wasn't because of a name-clash and rename the type
36 #### Note: this error code is no longer emitted by the compiler.
38 Imports (`use` statements) are not allowed after non-item statements, such as
39 variable declarations and expression statements.
41 Here is an example that demonstrates the error:
45 // Variable declaration before import
52 The solution is to declare the imports at the top of the block, function, or
55 Here is the previous example again, with the correct order:
65 See the Declaration Statements section of the reference for more information
66 about what constitutes an Item declaration and what does not:
68 https://doc.rust-lang.org/reference.html#statements
72 #### Note: this error code is no longer emitted by the compiler.
74 Two items of the same name cannot be imported without rebinding one of the
75 items under a new local name.
77 An example of this error:
81 use bar::*; // error, do `use foo::baz as quux` instead on the previous line
96 Two items of the same name cannot be imported without rebinding one of the
97 items under a new local name.
99 Erroneous code example:
101 ```compile_fail,E0252
103 use bar::baz; // error, do `use bar::baz as quux` instead
116 You can use aliases in order to fix this error. Example:
119 use foo::baz as foo_baz;
133 Or you can reference the item with its parent:
139 let x = foo::baz; // ok!
153 Attempt was made to import an unimportable value. This can happen when trying
154 to import a method from a trait.
156 Erroneous code example:
158 ```compile_fail,E0253
165 use foo::MyTrait::do_something;
166 // error: `do_something` is not directly importable
171 It's invalid to directly import methods belonging to a trait or concrete type.
175 Attempt was made to import an item whereas an extern crate with this name has
176 already been imported.
178 Erroneous code example:
180 ```compile_fail,E0254
189 use foo::core; // error: an extern crate named `core` has already
190 // been imported in this module
195 To fix this issue, you have to rename at least one of the two imports.
199 extern crate core as libcore; // ok!
214 You can't import a value whose name is the same as another value defined in the
217 Erroneous code example:
219 ```compile_fail,E0255
220 use bar::foo; // error: an item named `foo` is already in scope
231 You can use aliases in order to fix this error. Example:
234 use bar::foo as bar_foo; // ok!
245 Or you can reference the item with its parent:
255 bar::foo(); // we get the item by referring to its parent
261 #### Note: this error code is no longer emitted by the compiler.
263 You can't import a type or module when the name of the item being imported is
264 the same as another type or submodule defined in the module.
266 An example of this error:
269 use foo::Bar; // error
282 The name chosen for an external crate conflicts with another external crate
283 that has been imported into the current module.
285 Erroneous code example:
287 ```compile_fail,E0259
289 extern crate std as core;
294 The solution is to choose a different name that doesn't conflict with any
295 external crate imported into the current module.
301 extern crate std as other_name;
308 The name for an item declaration conflicts with an external crate's name.
310 Erroneous code example:
312 ```compile_fail,E0260
320 There are two possible solutions:
322 Solution #1: Rename the item.
330 Solution #2: Import the crate with a different name.
333 extern crate core as xyz;
338 See the Declaration Statements section of the reference for more information
339 about what constitutes an Item declaration and what does not:
341 https://doc.rust-lang.org/reference.html#statements
345 Private items cannot be publicly re-exported. This error indicates that you
346 attempted to `pub use` a type or value that was not itself public.
348 Erroneous code example:
360 The solution to this problem is to ensure that the items that you are
361 re-exporting are themselves marked with `pub`:
365 pub const X: u32 = 1;
373 See the 'Use Declarations' section of the reference for more information on
376 https://doc.rust-lang.org/reference.html#use-declarations
380 Private modules cannot be publicly re-exported. This error indicates that you
381 attempted to `pub use` a module that was not itself public.
383 Erroneous code example:
385 ```compile_fail,E0365
387 pub const X: u32 = 1;
395 The solution to this problem is to ensure that the module that you are
396 re-exporting is itself marked with `pub`:
400 pub const X: u32 = 1;
408 See the 'Use Declarations' section of the reference for more information
411 https://doc.rust-lang.org/reference.html#use-declarations
415 Inner items do not inherit type or const parameters from the functions
416 they are embedded in.
418 Erroneous code example:
420 ```compile_fail,E0401
422 fn bar(y: T) { // T is defined in the "outer" function
431 ```compile_fail,E0401
433 type MaybeT = Option<T>;
440 ```compile_fail,E0401
449 Items inside functions are basically just like top-level items, except
450 that they can only be used from the function they are in.
452 There are a couple of solutions for this.
454 If the item is a function, you may use a closure:
458 let bar = |y: T| { // explicit type annotation may not be necessary
465 For a generic item, you can copy over the parameters:
478 type MaybeT<T> = Option<T>;
482 Be sure to copy over any bounds as well:
485 fn foo<T: Copy>(x: T) {
486 fn bar<T: Copy>(y: T) {
494 fn foo<T: Copy>(x: T) {
495 struct Foo<T: Copy> {
501 This may require additional type hints in the function body.
503 In case the item is a function inside an `impl`, defining a private helper
504 function might be easier:
509 pub fn foo(&self, x: T) {
513 fn bar(&self, y: T) {
519 For default impls in traits, the private helper solution won't work, however
520 closures or copying the parameters should still work.
524 Some type parameters have the same name.
526 Erroneous code example:
528 ```compile_fail,E0403
529 fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
530 // parameter in this type parameter list
533 Please verify that none of the type parameters are misspelled, and rename any
534 clashing parameters. Example:
537 fn foo<T, Y>(s: T, u: Y) {} // ok!
542 You tried to use something which is not a trait in a trait position, such as
545 Erroneous code example:
547 ```compile_fail,E0404
551 impl Foo for Bar {} // error: `Foo` is not a trait
554 Another erroneous code example:
556 ```compile_fail,E0404
559 fn bar<T: Foo>(t: T) {} // error: `Foo` is not a trait
562 Please verify that you didn't misspell the trait's name or otherwise use the
563 wrong identifier. Example:
571 impl Foo for Bar { // ok!
572 // functions implementation
583 fn bar<T: Foo>(t: T) {} // ok!
589 The code refers to a trait that is not in scope.
591 Erroneous code example:
593 ```compile_fail,E0405
596 impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
599 Please verify that the name of the trait wasn't misspelled and ensure that it
600 was imported. Example:
603 # #[cfg(for_demonstration_only)]
605 use some_file::SomeTrait;
614 impl SomeTrait for Foo { // ok!
615 // implements functions
621 A definition of a method not in the implemented trait was given in a trait
624 Erroneous code example:
626 ```compile_fail,E0407
635 fn b() {} // error: method `b` is not a member of trait `Foo`
639 Please verify you didn't misspell the method name and you used the correct
640 trait. First example:
676 An "or" pattern was used where the variable bindings are not consistently bound
679 Erroneous code example:
681 ```compile_fail,E0408
683 Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
684 // not bound in pattern #2
689 Here, `y` is bound to the contents of the `Some` and can be used within the
690 block corresponding to the match arm. However, in case `x` is `None`, we have
691 not specified what `y` is, and the block will use a nonexistent variable.
693 To fix this error, either split into multiple match arms:
698 Some(y) => { /* use y */ }
699 None => { /* ... */ }
703 or, bind the variable to a field of the same type in all sub-patterns of the
709 (0, y) | (y, 0) => { /* use y */}
714 In this example, if `x` matches the pattern `(0, _)`, the second field is set
715 to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
716 cases `y` is set to some value.
720 An "or" pattern was used where the variable bindings are not consistently bound
723 Erroneous code example:
725 ```compile_fail,E0409
728 (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
729 // different mode in pattern #2
730 // than in pattern #1
735 Here, `y` is bound by-value in one case and by-reference in the other.
737 To fix this error, just use the same mode in both cases.
738 Generally using `ref` or `ref mut` where not already used will fix this:
743 (0, ref y) | (ref y, 0) => { /* use y */}
748 Alternatively, split the pattern:
753 (y, 0) => { /* use y */ }
754 (0, ref y) => { /* use y */}
761 The `Self` keyword was used outside an impl, trait, or type definition.
763 Erroneous code example:
765 ```compile_fail,E0411
766 <Self>::foo; // error: use of `Self` outside of an impl, trait, or type
770 The `Self` keyword represents the current type, which explains why it can only
771 be used inside an impl, trait, or type definition. It gives access to the
772 associated items of a type:
780 fn bar() -> Self::Bar; // like this
784 However, be careful when two types have a common associated type:
795 trait Baz : Foo + Foo2 {
796 fn bar() -> Self::Bar;
797 // error: ambiguous associated type `Bar` in bounds of `Self`
801 This problem can be solved by specifying from which trait we want to use the
813 trait Baz : Foo + Foo2 {
814 fn bar() -> <Self as Foo>::Bar; // ok!
820 The type name used is not in scope.
822 Erroneous code examples:
824 ```compile_fail,E0412
825 impl Something {} // error: type name `Something` is not in scope
830 fn bar(N); // error: type name `N` is not in scope
835 fn foo(x: T) {} // type name `T` is not in scope
838 To fix this error, please verify you didn't misspell the type name, you did
839 declare it or imported it into the scope. Examples:
844 impl Something {} // ok!
851 fn bar(_: Self::N); // ok!
856 fn foo<T>(x: T) {} // ok!
859 Another case that causes this error is when a type is imported into a parent
860 module. To fix this, you can follow the suggestion and use File directly or
861 `use super::File;` which will import the types from the parent namespace. An
862 example that causes this error is below:
864 ```compile_fail,E0412
868 fn some_function(f: File) {}
879 // use std::fs::File;
882 # fn main() {} // don't insert it for us; that'll break imports
887 More than one function parameter have the same name.
889 Erroneous code example:
891 ```compile_fail,E0415
892 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
893 // once in this parameter list
896 Please verify you didn't misspell parameters' name. Example:
899 fn foo(f: i32, g: i32) {} // ok!
904 An identifier is bound more than once in a pattern.
906 Erroneous code example:
908 ```compile_fail,E0416
910 (x, x) => {} // error: identifier `x` is bound more than once in the
915 Please verify you didn't misspell identifiers' name. Example:
923 Or maybe did you mean to unify? Consider using a guard:
926 # let (A, B, C) = (1, 2, 3);
928 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
929 (y, z, see) => { /* A and B unequal; do another thing */ }
935 You are trying to use an identifier that is either undefined or not a struct.
936 Erroneous code example:
938 ```compile_fail,E0422
940 let x = Foo { x: 1, y: 2 };
944 In this case, `Foo` is undefined, so it inherently isn't anything, and
945 definitely not a struct.
950 let x = foo { x: 1, y: 2 };
954 In this case, `foo` is defined, but is not a struct, so Rust can't use it as
959 An identifier was used like a function name or a value was expected and the
960 identifier exists but it belongs to a different namespace.
962 For (an erroneous) example, here a `struct` variant name were used as a
965 ```compile_fail,E0423
966 struct Foo { a: bool };
969 // error: expected function, found `Foo`
970 // `Foo` is a struct name, but this expression uses it like a function name
973 Please verify you didn't misspell the name of what you actually wanted to use
977 fn Foo() -> u32 { 0 }
979 let f = Foo(); // ok!
982 It is common to forget the trailing `!` on macro invocations, which would also
985 ```compile_fail,E0423
987 // error: expected function, found macro `println`
988 // did you mean `println!(...)`? (notice the trailing `!`)
991 Another case where this error is emitted is when a value is expected, but
992 something else is found:
994 ```compile_fail,E0423
996 pub const I: i32 = 1;
1001 //~^ ERROR expected value, found module `a`
1002 // did you mean `a::I`?
1008 The `self` keyword was used in a static method.
1010 Erroneous code example:
1012 ```compile_fail,E0424
1019 self.bar(); // error: `self` is not available in a static method.
1024 Please check if the method's argument list should have contained `self`,
1025 `&self`, or `&mut self` (in case you didn't want to create a static
1026 method), and add it if so. Example:
1042 An unresolved name was used.
1044 Erroneous code examples:
1046 ```compile_fail,E0425
1047 something_that_doesnt_exist::foo;
1048 // error: unresolved name `something_that_doesnt_exist::foo`
1054 Self; // error: unresolved name `Self`
1060 let x = unknown_variable; // error: unresolved name `unknown_variable`
1063 Please verify that the name wasn't misspelled and ensure that the
1064 identifier being referred to is valid for the given situation. Example:
1067 enum something_that_does_exist {
1075 mod something_that_does_exist {
1076 pub static foo : i32 = 0i32;
1079 something_that_does_exist::foo; // ok!
1085 let unknown_variable = 12u32;
1086 let x = unknown_variable; // ok!
1089 If the item is not defined in the current module, it must be imported using a
1090 `use` statement, like so:
1093 # mod foo { pub fn bar() {} }
1100 If the item you are importing is not defined in some super-module of the
1101 current module, then it must also be declared as public (e.g., `pub fn`).
1105 An undeclared label was used.
1107 Erroneous code example:
1109 ```compile_fail,E0426
1111 break 'a; // error: use of undeclared label `'a`
1115 Please verify you spelt or declare the label correctly. Example:
1125 A type or module has been defined more than once.
1127 Erroneous code example:
1129 ```compile_fail,E0428
1131 struct Bar; // error: duplicate definition of value `Bar`
1134 Please verify you didn't misspell the type/module's name or remove/rename the
1135 duplicated one. Example:
1144 The `self` keyword cannot appear alone as the last segment in a `use`
1147 Erroneous code example:
1149 ```compile_fail,E0429
1150 use std::fmt::self; // error: `self` imports are only allowed within a { } list
1153 To use a namespace itself in addition to some of its members, `self` may appear
1154 as part of a brace-enclosed list of imports:
1157 use std::fmt::{self, Debug};
1160 If you only want to import the namespace, do so directly:
1168 The `self` import appears more than once in the list.
1170 Erroneous code example:
1172 ```compile_fail,E0430
1173 use something::{self, self}; // error: `self` import can only appear once in
1177 Please verify you didn't misspell the import name or remove the duplicated
1178 `self` import. Example:
1183 use something::{self}; // ok!
1189 An invalid `self` import was made.
1191 Erroneous code example:
1193 ```compile_fail,E0431
1194 use {self}; // error: `self` import can only appear in an import list with a
1198 You cannot import the current module into itself, please remove this import
1199 or verify you didn't misspell it.
1203 An import was unresolved.
1205 Erroneous code example:
1207 ```compile_fail,E0432
1208 use something::Foo; // error: unresolved import `something::Foo`.
1211 Paths in `use` statements are relative to the crate root. To import items
1212 relative to the current and parent modules, use the `self::` and `super::`
1213 prefixes, respectively. Also verify that you didn't misspell the import
1214 name and that the import exists in the module from where you tried to
1218 use self::something::Foo; // ok!
1226 Or, if you tried to use a module from an external crate, you may have missed
1227 the `extern crate` declaration (which is usually placed in the crate root):
1230 extern crate core; // Required to use the `core` crate
1238 An undeclared type or module was used.
1240 Erroneous code example:
1242 ```compile_fail,E0433
1243 let map = HashMap::new();
1244 // error: failed to resolve: use of undeclared type or module `HashMap`
1247 Please verify you didn't misspell the type/module's name or that you didn't
1248 forget to import it:
1252 use std::collections::HashMap; // HashMap has been imported.
1253 let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1258 This error indicates that a variable usage inside an inner function is invalid
1259 because the variable comes from a dynamic environment. Inner functions do not
1260 have access to their containing environment.
1262 Erroneous code example:
1264 ```compile_fail,E0434
1268 y // error: can't capture dynamic environment in a fn item; use the
1269 // || { ... } closure form instead.
1274 Functions do not capture local variables. To fix this error, you can replace the
1275 function with a closure:
1286 or replace the captured variable with a constant or a static item:
1290 static mut X: u32 = 4;
1303 A non-constant value was used in a constant expression.
1305 Erroneous code example:
1307 ```compile_fail,E0435
1309 let a: [u8; foo]; // error: attempt to use a non-constant value in a constant
1312 To fix this error, please replace the value with a constant. Example:
1315 let a: [u8; 42]; // ok!
1321 const FOO: usize = 42;
1322 let a: [u8; FOO]; // ok!
1327 Trait implementations can only implement associated types that are members of
1328 the trait in question. This error indicates that you attempted to implement
1329 an associated type whose name does not match the name of any associated type
1332 Erroneous code example:
1334 ```compile_fail,E0437
1342 The solution to this problem is to remove the extraneous associated type:
1352 Trait implementations can only implement associated constants that are
1353 members of the trait in question. This error indicates that you
1354 attempted to implement an associated constant whose name does not
1355 match the name of any associated constant in the trait.
1357 Erroneous code example:
1359 ```compile_fail,E0438
1363 const BAR: bool = true;
1367 The solution to this problem is to remove the extraneous associated constant:
1377 Macro import declarations were malformed.
1379 Erroneous code examples:
1381 ```compile_fail,E0466
1382 #[macro_use(a_macro(another_macro))] // error: invalid import declaration
1383 extern crate core as some_crate;
1385 #[macro_use(i_want = "some_macros")] // error: invalid import declaration
1386 extern crate core as another_crate;
1389 This is a syntax error at the level of attribute declarations. The proper
1390 syntax for macro imports is the following:
1392 ```ignore (cannot-doctest-multicrate-project)
1395 macro_rules! get_tacos {
1400 macro_rules! get_pimientos {
1405 #[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
1406 extern crate some_crate; // `get_pimientos` macros from some_crate
1409 If you would like to import all exported macros, write `macro_use` with no
1414 A non-root module attempts to import macros from another crate.
1416 Example of erroneous code:
1418 ```compile_fail,E0468
1420 #[macro_use(debug_assert)] // error: must be at crate root to import
1421 extern crate core; // macros from another crate
1422 fn run_macro() { debug_assert!(true); }
1426 Only `extern crate` imports at the crate root level are allowed to import
1429 Either move the macro import to crate root or do without the foreign macros.
1433 #[macro_use(debug_assert)]
1437 fn run_macro() { debug_assert!(true); }
1444 A macro listed for import was not found.
1446 Erroneous code example:
1448 ```compile_fail,E0469
1449 #[macro_use(drink, be_merry)] // error: imported macro not found
1457 Either the listed macro is not contained in the imported crate, or it is not
1458 exported from the given crate.
1460 This could be caused by a typo. Did you misspell the macro's name?
1462 Double-check the names of the macros listed for import, and that the crate
1463 in question exports them.
1465 A working version would be:
1467 ```ignore (cannot-doctest-multicrate-project)
1468 // In some_crate crate:
1475 macro_rules! drink {
1480 #[macro_use(eat, drink)]
1481 extern crate some_crate; //ok!
1486 A binding shadowed something it shouldn't.
1488 Erroneous code example:
1490 ```compile_fail,E0530
1491 static TEST: i32 = 0;
1493 let r: (i32, i32) = (0, 0);
1495 TEST => {} // error: match bindings cannot shadow statics
1499 To fix this error, just change the binding's name in order to avoid shadowing
1500 one of the following:
1503 * struct/enum variant
1511 static TEST: i32 = 0;
1513 let r: (i32, i32) = (0, 0);
1515 something => {} // ok!
1521 Pattern arm did not match expected kind.
1523 Erroneous code example:
1525 ```compile_fail,E0532
1531 fn print_on_failure(state: &State) {
1533 // error: expected unit struct/variant or constant, found tuple
1534 // variant `State::Failed`
1535 State::Failed => println!("Failed"),
1541 To fix this error, ensure the match arm kind is the same as the expression
1552 fn print_on_failure(state: &State) {
1554 State::Failed(ref msg) => println!("Failed with {}", msg),
1562 A private item was used outside its scope.
1564 Erroneous code example:
1566 ```compile_fail,E0603
1568 const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
1569 // can't use it outside of the
1570 // `SomeModule` module.
1573 println!("const value: {}", SomeModule::PRIVATE); // error: constant `PRIVATE`
1577 In order to fix this error, you need to make the item public by using the `pub`
1582 pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
1586 println!("const value: {}", SomeModule::PRIVATE); // ok!
1591 An item usage is ambiguous.
1593 Erroneous code example:
1595 ```compile_fail,E0659
1610 collider::foo(); // ERROR: `foo` is ambiguous
1614 This error generally appears when two items with the same name are imported into
1615 a module. Here, the `foo` functions are imported and reexported from the
1616 `collider` module and therefore, when we're using `collider::foo()`, both
1619 To solve this error, the best solution is generally to keep the path before the
1620 item when using it. Example:
1637 collider::moon::foo(); // ok!
1638 collider::earth::foo(); // ok!
1644 Const parameters cannot depend on type parameters.
1645 The following is therefore invalid:
1646 ```compile_fail,E0671
1647 #![feature(const_generics)]
1649 fn const_id<T, const N: T>() -> T { // error: const parameter
1650 // depends on type parameter
1658 register_diagnostics! {
1659 // E0153, unused error code
1660 // E0157, unused error code
1663 // E0402, // cannot use an outer type parameter in this context
1664 // E0406, merged into 420
1665 // E0410, merged into 408
1666 // E0413, merged into 530
1667 // E0414, merged into 530
1668 // E0417, merged into 532
1669 // E0418, merged into 532
1670 // E0419, merged into 531
1671 // E0420, merged into 532
1672 // E0421, merged into 531
1673 E0531, // unresolved pattern path kind `name`
1674 // E0427, merged into 530