From 58955dee66a09a81db2dda83f49adf4436bfd6d4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 25 Jun 2016 14:21:45 +0200 Subject: [PATCH] Add new error codes and improve some explanations --- src/librustc_resolve/diagnostics.rs | 289 ++++++++++++++++++++-------- 1 file changed, 207 insertions(+), 82 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 208b5f11e20d1..7269a62b6063a 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -16,6 +16,8 @@ register_long_diagnostics! { E0154: r##" +## Note: this error code is no longer emitted by the compiler. + Imports (`use` statements) are not allowed after non-item statements, such as variable declarations and expression statements. @@ -50,6 +52,8 @@ https://doc.rust-lang.org/reference.html#statements "##, E0251: r##" +## Note: this error code is no longer emitted by the compiler. + Two items of the same name cannot be imported without rebinding one of the items under a new local name. @@ -75,9 +79,9 @@ E0252: r##" Two items of the same name cannot be imported without rebinding one of the items under a new local name. -An example of this error: +Erroneous code example: -```compile_fail +```compile_fail,E0252 use foo::baz; use bar::baz; // error, do `use bar::baz as quux` instead @@ -87,6 +91,41 @@ mod foo { pub struct baz; } +mod bar { + pub mod baz {} +} +``` + +You can use aliases in order to fix this error. Example: + +``` +use foo::baz as foo_baz; +use bar::baz; // ok! + +fn main() {} + +mod foo { + pub struct baz; +} + +mod bar { + pub mod baz {} +} +``` + +Or you can reference the item with its parent: + +``` +use bar::baz; + +fn main() { + let x = foo::baz; // ok! +} + +mod foo { + pub struct baz; +} + mod bar { pub mod baz {} } @@ -95,9 +134,11 @@ mod bar { E0253: r##" Attempt was made to import an unimportable value. This can happen when trying -to import a method from a trait. An example of this error: +to import a method from a trait. -```compile_fail +Erroneous code example: + +```compile_fail,E0253 mod foo { pub trait MyTrait { fn do_something(); @@ -105,6 +146,8 @@ mod foo { } use foo::MyTrait::do_something; + +fn main() {} ``` It's invalid to directly import methods belonging to a trait or concrete type. @@ -114,10 +157,10 @@ E0255: r##" You can't import a value whose name is the same as another value defined in the module. -An example of this error: +Erroneous code example: -```compile_fail -use bar::foo; // error, do `use bar::foo as baz` instead +```compile_fail,E0255 +use bar::foo; // error: an item named `foo` is already in scope fn foo() {} @@ -127,9 +170,39 @@ mod bar { fn main() {} ``` + +You can use aliases in order to fix this error. Example: + +``` +use bar::foo as bar_foo; // ok! + +fn foo() {} + +mod bar { + pub fn foo() {} +} + +fn main() {} +``` + +Or you can reference the item with its parent: + +``` +fn foo() {} + +mod bar { + pub fn foo() {} +} + +fn main() { + bar::foo(); // we get the item by referring to its parent +} +``` "##, E0256: r##" +## Note: this error code is no longer emitted by the compiler. + You can't import a type or module when the name of the item being imported is the same as another type or submodule defined in the module. @@ -154,9 +227,11 @@ that has been imported into the current module. Erroneous code example: -```compile_fail -extern crate a; -extern crate crate_a as a; +```compile_fail,E0259 +extern crate std; +extern crate libc as std; + +fn main() {} ``` The solution is to choose a different name that doesn't conflict with any @@ -165,17 +240,17 @@ external crate imported into the current module. Correct example: ```ignore -extern crate a; -extern crate crate_a as other_name; +extern crate std; +extern crate libc as other_name; ``` "##, E0260: r##" The name for an item declaration conflicts with an external crate's name. -For instance: +Erroneous code example: -```ignore +```ignore,E0260 extern crate abc; struct abc; @@ -206,10 +281,10 @@ https://doc.rust-lang.org/reference.html#statements "##, E0364: r##" -Private items cannot be publicly re-exported. This error indicates that you +Private items cannot be publicly re-exported. This error indicates that you attempted to `pub use` a type or value that was not itself public. -Here is an example that demonstrates the error: +Erroneous code example: ```compile_fail mod foo { @@ -217,17 +292,21 @@ mod foo { } pub use foo::X; + +fn main() {} ``` The solution to this problem is to ensure that the items that you are re-exporting are themselves marked with `pub`: -```ignore +``` mod foo { pub const X: u32 = 1; } pub use foo::X; + +fn main() {} ``` See the 'Use Declarations' section of the reference for more information on @@ -240,25 +319,29 @@ E0365: r##" Private modules cannot be publicly re-exported. This error indicates that you attempted to `pub use` a module that was not itself public. -Here is an example that demonstrates the error: +Erroneous code example: -```compile_fail +```compile_fail,E0365 mod foo { pub const X: u32 = 1; } pub use foo as foo2; + +fn main() {} ``` The solution to this problem is to ensure that the module that you are re-exporting is itself marked with `pub`: -```ignore +``` pub mod foo { pub const X: u32 = 1; } pub use foo as foo2; + +fn main() {} ``` See the 'Use Declarations' section of the reference for more information @@ -269,9 +352,11 @@ https://doc.rust-lang.org/reference.html#use-declarations E0401: r##" Inner items do not inherit type parameters from the functions they are embedded -in. For example, this will not compile: +in. -```compile_fail +Erroneous code example: + +```compile_fail,E0401 fn foo(x: T) { fn bar(y: T) { // T is defined in the "outer" function // .. @@ -282,7 +367,7 @@ fn foo(x: T) { Nor will this: -```compile_fail +```compile_fail,E0401 fn foo(x: T) { type MaybeT = Option; // ... @@ -291,7 +376,7 @@ fn foo(x: T) { Or this: -```compile_fail +```compile_fail,E0401 fn foo(x: T) { struct Foo { x: T, @@ -374,9 +459,11 @@ closures or copying the parameters should still work. "##, E0403: r##" -Some type parameters have the same name. Example of erroneous code: +Some type parameters have the same name. -```compile_fail +Erroneous code example: + +```compile_fail,E0403 fn foo(s: T, u: T) {} // error: the name `T` is already used for a type // parameter in this type parameter list ``` @@ -390,10 +477,11 @@ fn foo(s: T, u: Y) {} // ok! "##, E0404: r##" -You tried to implement something which was not a trait on an object. Example of -erroneous code: +You tried to implement something which was not a trait on an object. -```compile_fail +Erroneous code example: + +```compile_fail,E0404 struct Foo; struct Bar; @@ -416,9 +504,11 @@ impl Foo for Bar { // ok! "##, E0405: r##" -The code refers to a trait that is not in scope. Example of erroneous code: +The code refers to a trait that is not in scope. -```compile_fail +Erroneous code example: + +```compile_fail,E0405 struct Foo; impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope @@ -446,9 +536,11 @@ impl SomeTrait for Foo { // ok! E0407: r##" A definition of a method not in the implemented trait was given in a trait -implementation. Example of erroneous code: +implementation. -```compile_fail +Erroneous code example: + +```compile_fail,E0407 trait Foo { fn a(); } @@ -501,9 +593,9 @@ E0408: r##" An "or" pattern was used where the variable bindings are not consistently bound across patterns. -Example of erroneous code: +Erroneous code example: -```compile_fail +```compile_fail,E0408 match x { Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is // not bound in pattern #2 @@ -545,9 +637,9 @@ E0409: r##" An "or" pattern was used where the variable bindings are not consistently bound across patterns. -Example of erroneous code: +Erroneous code example: -```compile_fail +```compile_fail,E0409 let x = (0, 2); match x { (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with @@ -583,9 +675,11 @@ match x { "##, E0411: r##" -The `Self` keyword was used outside an impl or a trait. Erroneous code example: +The `Self` keyword was used outside an impl or a trait. -```compile_fail +Erroneous code example: + +```compile_fail,E0411 ::foo; // error: use of `Self` outside of an impl or trait ``` @@ -639,9 +733,11 @@ trait Baz : Foo + Foo2 { "##, E0412: r##" -The type name used is not in scope. Example of erroneous codes: +The type name used is not in scope. -```compile_fail +Erroneous code examples: + +```compile_fail,E0412 impl Something {} // error: type name `Something` is not in scope // or: @@ -678,9 +774,11 @@ fn foo(x: T) {} // ok! "##, E0415: r##" -More than one function parameter have the same name. Example of erroneous code: +More than one function parameter have the same name. -```compile_fail +Erroneous code example: + +```compile_fail,E0415 fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than // once in this parameter list ``` @@ -693,9 +791,11 @@ fn foo(f: i32, g: i32) {} // ok! "##, E0416: r##" -An identifier is bound more than once in a pattern. Example of erroneous code: +An identifier is bound more than once in a pattern. -```compile_fail +Erroneous code example: + +```compile_fail,E0416 match (1, 2) { (x, x) => {} // error: identifier `x` is bound more than once in the // same pattern @@ -722,9 +822,10 @@ match (A, B, C) { E0422: r##" You are trying to use an identifier that is either undefined or not a struct. -For instance: -``` compile_fail +Erroneous code example: + +``` compile_fail,E0422 fn main () { let x = Foo { x: 1, y: 2 }; } @@ -733,7 +834,7 @@ fn main () { In this case, `Foo` is undefined, so it inherently isn't anything, and definitely not a struct. -```compile_fail +```compile_fail,E0422 fn main () { let foo = 1; let x = foo { x: 1, y: 2 }; @@ -745,10 +846,11 @@ one. "##, E0423: r##" -A `struct` variant name was used like a function name. Example of erroneous -code: +A `struct` variant name was used like a function name. -```compile_fail +Erroneous code example: + +```compile_fail,E0423 struct Foo { a: bool}; let f = Foo(); @@ -767,9 +869,11 @@ let f = Foo(); // ok! "##, E0424: r##" -The `self` keyword was used in a static method. Example of erroneous code: +The `self` keyword was used in a static method. -```compile_fail +Erroneous code example: + +```compile_fail,E0424 struct Foo; impl Foo { @@ -799,9 +903,11 @@ impl Foo { "##, E0425: r##" -An unresolved name was used. Example of erroneous codes: +An unresolved name was used. -```compile_fail +Erroneous code examples: + +```compile_fail,E0425 something_that_doesnt_exist::foo; // error: unresolved name `something_that_doesnt_exist::foo` @@ -846,9 +952,11 @@ let x = unknown_variable; // ok! "##, E0426: r##" -An undeclared label was used. Example of erroneous code: +An undeclared label was used. -```compile_fail +Erroneous code example: + +```compile_fail,E0426 loop { break 'a; // error: use of undeclared label `'a` } @@ -864,10 +972,11 @@ Please verify you spelt or declare the label correctly. Example: "##, E0428: r##" -A type or module has been defined more than once. Example of erroneous -code: +A type or module has been defined more than once. -```compile_fail +Erroneous code example: + +```compile_fail,E0428 struct Bar; struct Bar; // error: duplicate definition of value `Bar` ``` @@ -885,9 +994,9 @@ E0429: r##" The `self` keyword cannot appear alone as the last segment in a `use` declaration. -Example of erroneous code: +Erroneous code example: -```compile_fail +```compile_fail,E0429 use std::fmt::self; // error: `self` imports are only allowed within a { } list ``` @@ -906,9 +1015,11 @@ use std::fmt; "##, E0430: r##" -The `self` import appears more than once in the list. Erroneous code example: +The `self` import appears more than once in the list. -```compile_fail +Erroneous code example: + +```compile_fail,E0430 use something::{self, self}; // error: `self` import can only appear once in // the list ``` @@ -922,9 +1033,11 @@ use something::self; // ok! "##, E0431: r##" -An invalid `self` import was made. Erroneous code example: +An invalid `self` import was made. -```compile_fail +Erroneous code example: + +```compile_fail,E0431 use {self}; // error: `self` import can only appear in an import list with a // non-empty prefix ``` @@ -934,9 +1047,11 @@ or verify you didn't misspell it. "##, E0432: r##" -An import was unresolved. Erroneous code example: +An import was unresolved. -```compile_fail +Erroneous code example: + +```compile_fail,E0432 use something::Foo; // error: unresolved import `something::Foo`. ``` @@ -965,14 +1080,23 @@ use homura::Madoka; "##, E0433: r##" -Invalid import. Example of erroneous code: +An undeclared type or module was used. -```compile_fail -use something_which_doesnt_exist; -// error: unresolved import `something_which_doesnt_exist` +Erroneous code example: + +```compile_fail,E0433 +let map = HashMap::new(); +// error: failed to resolve. Use of undeclared type or module `HashMap` ``` -Please verify you didn't misspell the import's name. +Please verify you didn't misspell the type/module's name or that you didn't +forgot to import it: + + +``` +use std::collections::HashMap; // HashMap has been imported. +let map: HashMap = HashMap::new(); // So it can be used! +``` "##, E0434: r##" @@ -980,9 +1104,9 @@ This error indicates that a variable usage inside an inner function is invalid because the variable comes from a dynamic environment. Inner functions do not have access to their containing environment. -Example of erroneous code: +Erroneous code example: -```compile_fail +```compile_fail,E0434 fn foo() { let y = 5; fn bar() -> u32 { @@ -1021,10 +1145,11 @@ fn foo() { "##, E0435: r##" -A non-constant value was used to initialise a constant. Example of erroneous -code: +A non-constant value was used to initialise a constant. -```compile_fail +Erroneous code example: + +```compile_fail,E0435 let foo = 42u32; const FOO : u32 = foo; // error: attempt to use a non-constant value in a // constant @@ -1050,9 +1175,9 @@ the trait in question. This error indicates that you attempted to implement an associated type whose name does not match the name of any associated type in the trait. -Here is an example that demonstrates the error: +Erroneous code example: -```compile_fail +```compile_fail,E0437 trait Foo {} impl Foo for i32 { @@ -1075,9 +1200,9 @@ members of the trait in question. This error indicates that you attempted to implement an associated constant whose name does not match the name of any associated constant in the trait. -Here is an example that demonstrates the error: +Erroneous code example: -```compile_fail +```compile_fail,E0438 #![feature(associated_consts)] trait Foo {}