From 61b26e323ca3468f69d84ca4b6f960ea4baedb3e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 30 May 2015 00:44:22 +0200 Subject: [PATCH] Add E0011 explanation --- src/librustc/diagnostics.rs | 97 ++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 8 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index f5cb1bd25d60d..5c27c09b84298 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -176,6 +176,38 @@ for the entire lifetime of a program. Creating a boxed value allocates memory on the heap at runtime, and therefore cannot be done at compile time. "##, +E0011: r##" +Using a user-defined operator on const/static variable is restricted to what +can be evaluated at compile-time. Using an user-defined operator could call a +user-defined function, which is not allowed. + +Bad example: + +``` +use std::ops::Index; + +struct Foo { a: u8 } + +impl ::std::ops::Index for Foo { + type Output = u8; + + fn index<'a>(&'a self, idx: u8) -> &'a u8 { &self.a } +} + +const a: Foo = Foo { a: 0u8 }; +const b: u8 = a[0]; // Index trait is defined by the user, bad! +``` + +The only traits which can be used have to be already implemented, not user-defined. + +Example: + +``` +const a: &'static [i32] = &[1, 2, 3]; +const b: i32 = a[0]; // Good! +``` +"##, + E0013: r##" Static and const variables can refer to other const variables. But a const variable cannot refer to a static variable. For example, `Y` cannot refer to `X` @@ -218,9 +250,9 @@ Therefore, casting one of these non-constant pointers to an integer results in a non-constant integer which lead to this error. Example: ``` -const X: u32 = 50; -const Y: *const u32 = &X; -println!("{:?}", Y); +const X: u32 = 1; +const Y: usize = &X as *const u32 as usize; +println!("{}", Y); ``` "##, @@ -396,6 +428,54 @@ enum Method { GET, POST } ``` "##, +E0261: r##" +When using a lifetime like `'a` in a type, it must be declared before being +used. + +These two examples illustrate the problem: + +``` +// error, use of undeclared lifetime name `'a` +fn foo(x: &'a str) { } + +struct Foo { + // error, use of undeclared lifetime name `'a` + x: &'a str, +} +``` + +These can be fixed by declaring lifetime parameters: + +``` +fn foo<'a>(x: &'a str) { } + +struct Foo<'a> { + x: &'a str, +} +``` +"##, + +E0262: r##" +Declaring certain lifetime names in parameters is disallowed. For example, +because the `'static` lifetime is a special built-in lifetime name denoting +the lifetime of the entire program, this is an error: + +``` +// error, illegal lifetime parameter name `'static` +fn foo<'static>(x: &'static str) { } +``` +"##, + +E0263: r##" +A lifetime name cannot be declared more than once in the same scope. For +example: + +``` +// error, lifetime name `'a` declared twice in the same scope +fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { } +``` +"##, + E0265: r##" This error indicates that a static or constant references itself. All statics and constants need to resolve to a value in an acyclic manner. @@ -800,7 +880,6 @@ struct Foo { register_diagnostics! { - E0011, E0014, E0016, E0017, @@ -814,9 +893,6 @@ register_diagnostics! { E0136, E0138, E0139, - E0261, // use of undeclared lifetime name - E0262, // illegal lifetime parameter name - E0263, // lifetime name declared twice in same scope E0264, // unknown external lang item E0266, // expected item E0269, // not all control paths return a value @@ -845,5 +921,10 @@ register_diagnostics! { E0314, // closure outlives stack frame E0315, // cannot invoke closure outside of its lifetime E0316, // nested quantification of lifetimes - E0370 // discriminant overflow + E0370, // discriminant overflow + E0378, // method calls limited to constant inherent methods + E0394, // cannot refer to other statics by value, use the address-of + // operator or a constant instead + E0395, // pointer comparison in const-expr + E0396 // pointer dereference in const-expr }