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 Type parameter defaults can only use parameters that occur before them.
20 Erroneous code example:
23 struct Foo<T=U, U=()> {
27 // error: type parameters with a default cannot use forward declared
31 Since type parameters are evaluated in-order, you may be able to fix this issue
35 struct Foo<U=(), T=U> {
41 Please also verify that this wasn't because of a name-clash and rename the type
46 #### Note: this error code is no longer emitted by the compiler.
48 Imports (`use` statements) are not allowed after non-item statements, such as
49 variable declarations and expression statements.
51 Here is an example that demonstrates the error:
55 // Variable declaration before import
62 The solution is to declare the imports at the top of the block, function, or
65 Here is the previous example again, with the correct order:
75 See the Declaration Statements section of the reference for more information
76 about what constitutes an Item declaration and what does not:
78 https://doc.rust-lang.org/reference.html#statements
82 #### Note: this error code is no longer emitted by the compiler.
84 Two items of the same name cannot be imported without rebinding one of the
85 items under a new local name.
87 An example of this error:
91 use bar::*; // error, do `use foo::baz as quux` instead on the previous line
106 Two items of the same name cannot be imported without rebinding one of the
107 items under a new local name.
109 Erroneous code example:
111 ```compile_fail,E0252
113 use bar::baz; // error, do `use bar::baz as quux` instead
126 You can use aliases in order to fix this error. Example:
129 use foo::baz as foo_baz;
143 Or you can reference the item with its parent:
149 let x = foo::baz; // ok!
163 Attempt was made to import an unimportable value. This can happen when trying
164 to import a method from a trait.
166 Erroneous code example:
168 ```compile_fail,E0253
175 use foo::MyTrait::do_something;
176 // error: `do_something` is not directly importable
181 It's invalid to directly import methods belonging to a trait or concrete type.
185 Attempt was made to import an item whereas an extern crate with this name has
186 already been imported.
188 Erroneous code example:
190 ```compile_fail,E0254
199 use foo::core; // error: an extern crate named `core` has already
200 // been imported in this module
205 To fix issue issue, you have to rename at least one of the two imports.
209 extern crate core as libcore; // ok!
224 You can't import a value whose name is the same as another value defined in the
227 Erroneous code example:
229 ```compile_fail,E0255
230 use bar::foo; // error: an item named `foo` is already in scope
241 You can use aliases in order to fix this error. Example:
244 use bar::foo as bar_foo; // ok!
255 Or you can reference the item with its parent:
265 bar::foo(); // we get the item by referring to its parent
271 #### Note: this error code is no longer emitted by the compiler.
273 You can't import a type or module when the name of the item being imported is
274 the same as another type or submodule defined in the module.
276 An example of this error:
279 use foo::Bar; // error
292 The name chosen for an external crate conflicts with another external crate
293 that has been imported into the current module.
295 Erroneous code example:
297 ```compile_fail,E0259
300 extern crate libc as core;
305 The solution is to choose a different name that doesn't conflict with any
306 external crate imported into the current module.
313 extern crate libc as other_name;
320 The name for an item declaration conflicts with an external crate's name.
322 Erroneous code example:
324 ```compile_fail,E0260
330 There are two possible solutions:
332 Solution #1: Rename the item.
340 Solution #2: Import the crate with a different name.
343 extern crate core as xyz;
348 See the Declaration Statements section of the reference for more information
349 about what constitutes an Item declaration and what does not:
351 https://doc.rust-lang.org/reference.html#statements
355 Private items cannot be publicly re-exported. This error indicates that you
356 attempted to `pub use` a type or value that was not itself public.
358 Erroneous code example:
370 The solution to this problem is to ensure that the items that you are
371 re-exporting are themselves marked with `pub`:
375 pub const X: u32 = 1;
383 See the 'Use Declarations' section of the reference for more information on
386 https://doc.rust-lang.org/reference.html#use-declarations
390 Private modules cannot be publicly re-exported. This error indicates that you
391 attempted to `pub use` a module that was not itself public.
393 Erroneous code example:
395 ```compile_fail,E0365
397 pub const X: u32 = 1;
405 The solution to this problem is to ensure that the module that you are
406 re-exporting is itself marked with `pub`:
410 pub const X: u32 = 1;
418 See the 'Use Declarations' section of the reference for more information
421 https://doc.rust-lang.org/reference.html#use-declarations
425 Inner items do not inherit type parameters from the functions they are embedded
428 Erroneous code example:
430 ```compile_fail,E0401
432 fn bar(y: T) { // T is defined in the "outer" function
441 ```compile_fail,E0401
443 type MaybeT = Option<T>;
450 ```compile_fail,E0401
459 Items inside functions are basically just like top-level items, except
460 that they can only be used from the function they are in.
462 There are a couple of solutions for this.
464 If the item is a function, you may use a closure:
468 let bar = |y: T| { // explicit type annotation may not be necessary
475 For a generic item, you can copy over the parameters:
488 type MaybeT<T> = Option<T>;
492 Be sure to copy over any bounds as well:
495 fn foo<T: Copy>(x: T) {
496 fn bar<T: Copy>(y: T) {
504 fn foo<T: Copy>(x: T) {
505 struct Foo<T: Copy> {
511 This may require additional type hints in the function body.
513 In case the item is a function inside an `impl`, defining a private helper
514 function might be easier:
519 pub fn foo(&self, x: T) {
523 fn bar(&self, y: T) {
529 For default impls in traits, the private helper solution won't work, however
530 closures or copying the parameters should still work.
534 Some type parameters have the same name.
536 Erroneous code example:
538 ```compile_fail,E0403
539 fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
540 // parameter in this type parameter list
543 Please verify that none of the type parameterss are misspelled, and rename any
544 clashing parameters. Example:
547 fn foo<T, Y>(s: T, u: Y) {} // ok!
552 You tried to implement something which was not a trait on an object.
554 Erroneous code example:
556 ```compile_fail,E0404
560 impl Foo for Bar {} // error: `Foo` is not a trait
563 Please verify that you didn't misspell the trait's name or otherwise use the
564 wrong identifier. Example:
572 impl Foo for Bar { // ok!
573 // functions implementation
579 The code refers to a trait that is not in scope.
581 Erroneous code example:
583 ```compile_fail,E0405
586 impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
589 Please verify that the name of the trait wasn't misspelled and ensure that it
590 was imported. Example:
593 # #[cfg(for_demonstration_only)]
595 use some_file::SomeTrait;
604 impl SomeTrait for Foo { // ok!
605 // implements functions
611 A definition of a method not in the implemented trait was given in a trait
614 Erroneous code example:
616 ```compile_fail,E0407
625 fn b() {} // error: method `b` is not a member of trait `Foo`
629 Please verify you didn't misspell the method name and you used the correct
630 trait. First example:
666 An "or" pattern was used where the variable bindings are not consistently bound
669 Erroneous code example:
671 ```compile_fail,E0408
673 Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
674 // not bound in pattern #2
679 Here, `y` is bound to the contents of the `Some` and can be used within the
680 block corresponding to the match arm. However, in case `x` is `None`, we have
681 not specified what `y` is, and the block will use a nonexistent variable.
683 To fix this error, either split into multiple match arms:
688 Some(y) => { /* use y */ }
689 None => { /* ... */ }
693 or, bind the variable to a field of the same type in all sub-patterns of the
699 (0, y) | (y, 0) => { /* use y */}
704 In this example, if `x` matches the pattern `(0, _)`, the second field is set
705 to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
706 cases `y` is set to some value.
710 An "or" pattern was used where the variable bindings are not consistently bound
713 Erroneous code example:
715 ```compile_fail,E0409
718 (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
719 // different mode in pattern #2
720 // than in pattern #1
725 Here, `y` is bound by-value in one case and by-reference in the other.
727 To fix this error, just use the same mode in both cases.
728 Generally using `ref` or `ref mut` where not already used will fix this:
733 (0, ref y) | (ref y, 0) => { /* use y */}
738 Alternatively, split the pattern:
743 (y, 0) => { /* use y */ }
744 (0, ref y) => { /* use y */}
751 The `Self` keyword was used outside an impl or a trait.
753 Erroneous code example:
755 ```compile_fail,E0411
756 <Self>::foo; // error: use of `Self` outside of an impl or trait
759 The `Self` keyword represents the current type, which explains why it can only
760 be used inside an impl or a trait. It gives access to the associated items of a
769 fn bar() -> Self::Bar; // like this
773 However, be careful when two types have a common associated type:
784 trait Baz : Foo + Foo2 {
785 fn bar() -> Self::Bar;
786 // error: ambiguous associated type `Bar` in bounds of `Self`
790 This problem can be solved by specifying from which trait we want to use the
802 trait Baz : Foo + Foo2 {
803 fn bar() -> <Self as Foo>::Bar; // ok!
809 The type name used is not in scope.
811 Erroneous code examples:
813 ```compile_fail,E0412
814 impl Something {} // error: type name `Something` is not in scope
819 fn bar(N); // error: type name `N` is not in scope
824 fn foo(x: T) {} // type name `T` is not in scope
827 To fix this error, please verify you didn't misspell the type name, you did
828 declare it or imported it into the scope. Examples:
833 impl Something {} // ok!
840 fn bar(_: Self::N); // ok!
845 fn foo<T>(x: T) {} // ok!
848 Another case that causes this error is when a type is imported into a parent
849 module. To fix this, you can follow the suggestion and use File directly or
850 `use super::File;` which will import the types from the parent namespace. An
851 example that causes this error is below:
853 ```compile_fail,E0412
857 fn some_function(f: File) {}
868 // use std::fs::File;
871 # fn main() {} // don't insert it for us; that'll break imports
876 More than one function parameter have the same name.
878 Erroneous code example:
880 ```compile_fail,E0415
881 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
882 // once in this parameter list
885 Please verify you didn't misspell parameters' name. Example:
888 fn foo(f: i32, g: i32) {} // ok!
893 An identifier is bound more than once in a pattern.
895 Erroneous code example:
897 ```compile_fail,E0416
899 (x, x) => {} // error: identifier `x` is bound more than once in the
904 Please verify you didn't misspell identifiers' name. Example:
912 Or maybe did you mean to unify? Consider using a guard:
915 # let (A, B, C) = (1, 2, 3);
917 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
918 (y, z, see) => { /* A and B unequal; do another thing */ }
924 You are trying to use an identifier that is either undefined or not a struct.
925 Erroneous code example:
927 ```compile_fail,E0422
929 let x = Foo { x: 1, y: 2 };
933 In this case, `Foo` is undefined, so it inherently isn't anything, and
934 definitely not a struct.
939 let x = foo { x: 1, y: 2 };
943 In this case, `foo` is defined, but is not a struct, so Rust can't use it as
948 A `struct` variant name was used like a function name.
950 Erroneous code example:
952 ```compile_fail,E0423
953 struct Foo { a: bool };
956 // error: `Foo` is a struct variant name, but this expression uses
957 // it like a function name
960 Please verify you didn't misspell the name of what you actually wanted to use
964 fn Foo() -> u32 { 0 }
966 let f = Foo(); // ok!
971 The `self` keyword was used in a static method.
973 Erroneous code example:
975 ```compile_fail,E0424
982 self.bar(); // error: `self` is not available in a static method.
987 Please check if the method's argument list should have contained `self`,
988 `&self`, or `&mut self` (in case you didn't want to create a static
989 method), and add it if so. Example:
1005 An unresolved name was used.
1007 Erroneous code examples:
1009 ```compile_fail,E0425
1010 something_that_doesnt_exist::foo;
1011 // error: unresolved name `something_that_doesnt_exist::foo`
1017 Self; // error: unresolved name `Self`
1023 let x = unknown_variable; // error: unresolved name `unknown_variable`
1026 Please verify that the name wasn't misspelled and ensure that the
1027 identifier being referred to is valid for the given situation. Example:
1030 enum something_that_does_exist {
1038 mod something_that_does_exist {
1039 pub static foo : i32 = 0i32;
1042 something_that_does_exist::foo; // ok!
1048 let unknown_variable = 12u32;
1049 let x = unknown_variable; // ok!
1052 If the item is not defined in the current module, it must be imported using a
1053 `use` statement, like so:
1056 # mod foo { pub fn bar() {} }
1063 If the item you are importing is not defined in some super-module of the
1064 current module, then it must also be declared as public (e.g., `pub fn`).
1068 An undeclared label was used.
1070 Erroneous code example:
1072 ```compile_fail,E0426
1074 break 'a; // error: use of undeclared label `'a`
1078 Please verify you spelt or declare the label correctly. Example:
1088 A type or module has been defined more than once.
1090 Erroneous code example:
1092 ```compile_fail,E0428
1094 struct Bar; // error: duplicate definition of value `Bar`
1097 Please verify you didn't misspell the type/module's name or remove/rename the
1098 duplicated one. Example:
1107 The `self` keyword cannot appear alone as the last segment in a `use`
1110 Erroneous code example:
1112 ```compile_fail,E0429
1113 use std::fmt::self; // error: `self` imports are only allowed within a { } list
1116 To use a namespace itself in addition to some of its members, `self` may appear
1117 as part of a brace-enclosed list of imports:
1120 use std::fmt::{self, Debug};
1123 If you only want to import the namespace, do so directly:
1131 The `self` import appears more than once in the list.
1133 Erroneous code example:
1135 ```compile_fail,E0430
1136 use something::{self, self}; // error: `self` import can only appear once in
1140 Please verify you didn't misspell the import name or remove the duplicated
1141 `self` import. Example:
1146 use something::{self}; // ok!
1152 An invalid `self` import was made.
1154 Erroneous code example:
1156 ```compile_fail,E0431
1157 use {self}; // error: `self` import can only appear in an import list with a
1161 You cannot import the current module into itself, please remove this import
1162 or verify you didn't misspell it.
1166 An import was unresolved.
1168 Erroneous code example:
1170 ```compile_fail,E0432
1171 use something::Foo; // error: unresolved import `something::Foo`.
1174 Paths in `use` statements are relative to the crate root. To import items
1175 relative to the current and parent modules, use the `self::` and `super::`
1176 prefixes, respectively. Also verify that you didn't misspell the import
1177 name and that the import exists in the module from where you tried to
1181 use self::something::Foo; // ok!
1189 Or, if you tried to use a module from an external crate, you may have missed
1190 the `extern crate` declaration (which is usually placed in the crate root):
1193 extern crate core; // Required to use the `core` crate
1201 An undeclared type or module was used.
1203 Erroneous code example:
1205 ```compile_fail,E0433
1206 let map = HashMap::new();
1207 // error: failed to resolve. Use of undeclared type or module `HashMap`
1210 Please verify you didn't misspell the type/module's name or that you didn't
1211 forgot to import it:
1215 use std::collections::HashMap; // HashMap has been imported.
1216 let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1221 This error indicates that a variable usage inside an inner function is invalid
1222 because the variable comes from a dynamic environment. Inner functions do not
1223 have access to their containing environment.
1225 Erroneous code example:
1227 ```compile_fail,E0434
1231 y // error: can't capture dynamic environment in a fn item; use the
1232 // || { ... } closure form instead.
1237 Functions do not capture local variables. To fix this error, you can replace the
1238 function with a closure:
1249 or replace the captured variable with a constant or a static item:
1253 static mut X: u32 = 4;
1266 A non-constant value was used in a constant expression.
1268 Erroneous code example:
1270 ```compile_fail,E0435
1272 let a: [u8; foo]; // error: attempt to use a non-constant value in a constant
1275 To fix this error, please replace the value with a constant. Example:
1278 let a: [u8; 42]; // ok!
1284 const FOO: usize = 42;
1285 let a: [u8; FOO]; // ok!
1290 Trait implementations can only implement associated types that are members of
1291 the trait in question. This error indicates that you attempted to implement
1292 an associated type whose name does not match the name of any associated type
1295 Erroneous code example:
1297 ```compile_fail,E0437
1305 The solution to this problem is to remove the extraneous associated type:
1315 Trait implementations can only implement associated constants that are
1316 members of the trait in question. This error indicates that you
1317 attempted to implement an associated constant whose name does not
1318 match the name of any associated constant in the trait.
1320 Erroneous code example:
1322 ```compile_fail,E0438
1326 const BAR: bool = true;
1330 The solution to this problem is to remove the extraneous associated constant:
1340 Macro import declarations were malformed.
1342 Erroneous code examples:
1344 ```compile_fail,E0466
1345 #[macro_use(a_macro(another_macro))] // error: invalid import declaration
1346 extern crate core as some_crate;
1348 #[macro_use(i_want = "some_macros")] // error: invalid import declaration
1349 extern crate core as another_crate;
1352 This is a syntax error at the level of attribute declarations. The proper
1353 syntax for macro imports is the following:
1355 ```ignore (cannot-doctest-multicrate-project)
1358 macro_rules! get_tacos {
1363 macro_rules! get_pimientos {
1368 #[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
1369 extern crate some_crate; // `get_pimientos` macros from some_crate
1372 If you would like to import all exported macros, write `macro_use` with no
1377 Macro reexport declarations were empty or malformed.
1379 Erroneous code examples:
1381 ```compile_fail,E0467
1382 #[macro_reexport] // error: no macros listed for export
1383 extern crate core as macros_for_good;
1385 #[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
1386 extern crate core as other_macros_for_good;
1389 This is a syntax error at the level of attribute declarations.
1391 Currently, `macro_reexport` requires at least one macro name to be listed.
1392 Unlike `macro_use`, listing no names does not reexport all macros from the
1395 Decide which macros you would like to export and list them properly.
1397 These are proper reexport declarations:
1399 ```ignore (cannot-doctest-multicrate-project)
1400 #[macro_reexport(some_macro, another_macro)]
1401 extern crate macros_for_good;
1406 A non-root module attempts to import macros from another crate.
1408 Example of erroneous code:
1410 ```compile_fail,E0468
1412 #[macro_use(debug_assert)] // error: must be at crate root to import
1413 extern crate core; // macros from another crate
1414 fn run_macro() { debug_assert!(true); }
1418 Only `extern crate` imports at the crate root level are allowed to import
1421 Either move the macro import to crate root or do without the foreign macros.
1425 #[macro_use(debug_assert)]
1429 fn run_macro() { debug_assert!(true); }
1436 A macro listed for import was not found.
1438 Erroneous code example:
1440 ```compile_fail,E0469
1441 #[macro_use(drink, be_merry)] // error: imported macro not found
1449 Either the listed macro is not contained in the imported crate, or it is not
1450 exported from the given crate.
1452 This could be caused by a typo. Did you misspell the macro's name?
1454 Double-check the names of the macros listed for import, and that the crate
1455 in question exports them.
1457 A working version would be:
1459 ```ignore (cannot-doctest-multicrate-project)
1460 // In some_crate crate:
1467 macro_rules! drink {
1472 #[macro_use(eat, drink)]
1473 extern crate some_crate; //ok!
1478 A macro listed for reexport was not found.
1480 Erroneous code example:
1482 ```compile_fail,E0470
1483 #[macro_reexport(drink, be_merry)]
1491 Either the listed macro is not contained in the imported crate, or it is not
1492 exported from the given crate.
1494 This could be caused by a typo. Did you misspell the macro's name?
1496 Double-check the names of the macros listed for reexport, and that the crate
1497 in question exports them.
1501 ```ignore (cannot-doctest-multicrate-project)
1502 // In some_crate crate:
1509 macro_rules! drink {
1514 #[macro_reexport(eat, drink)]
1515 extern crate some_crate;
1520 A binding shadowed something it shouldn't.
1522 Erroneous code example:
1524 ```compile_fail,E0530
1525 static TEST: i32 = 0;
1527 let r: (i32, i32) = (0, 0);
1529 TEST => {} // error: match bindings cannot shadow statics
1533 To fix this error, just change the binding's name in order to avoid shadowing
1534 one of the following:
1537 * struct/enum variant
1545 static TEST: i32 = 0;
1547 let r: (i32, i32) = (0, 0);
1549 something => {} // ok!
1555 Pattern arm did not match expected kind.
1557 Erroneous code example:
1559 ```compile_fail,E0532
1565 fn print_on_failure(state: &State) {
1567 // error: expected unit struct/variant or constant, found tuple
1568 // variant `State::Failed`
1569 State::Failed => println!("Failed"),
1575 To fix this error, ensure the match arm kind is the same as the expression
1586 fn print_on_failure(state: &State) {
1588 State::Failed(ref msg) => println!("Failed with {}", msg),
1596 A private item was used outside its scope.
1598 Erroneous code example:
1600 ```compile_fail,E0603
1602 const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
1603 // can't use it outside of the
1604 // `SomeModule` module.
1607 println!("const value: {}", SomeModule::PRIVATE); // error: constant `CONSTANT`
1611 In order to fix this error, you need to make the item public by using the `pub`
1616 pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
1620 println!("const value: {}", SomeModule::PRIVATE); // ok!
1626 register_diagnostics
! {
1627 // E0153, unused error code
1628 // E0157, unused error code
1631 // E0402, // cannot use an outer type parameter in this context
1632 // E0406, merged into 420
1633 // E0410, merged into 408
1634 // E0413, merged into 530
1635 // E0414, merged into 530
1636 // E0417, merged into 532
1637 // E0418, merged into 532
1638 // E0419, merged into 531
1639 // E0420, merged into 532
1640 // E0421, merged into 531
1641 E0531
, // unresolved pattern path kind `name`
1642 // E0427, merged into 530