Skip to content

Commit

Permalink
Add E0011 explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 29, 2015
1 parent 86a821e commit 61b26e3
Showing 1 changed file with 89 additions and 8 deletions.
97 changes: 89 additions & 8 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> 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`
Expand Down Expand Up @@ -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);
```
"##,

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -800,7 +880,6 @@ struct Foo<T: 'static> {


register_diagnostics! {
E0011,
E0014,
E0016,
E0017,
Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 61b26e3

Please sign in to comment.