Skip to content

Commit 61b26e3

Browse files
Add E0011 explanation
1 parent 86a821e commit 61b26e3

File tree

1 file changed

+89
-8
lines changed

1 file changed

+89
-8
lines changed

src/librustc/diagnostics.rs

+89-8
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,38 @@ for the entire lifetime of a program. Creating a boxed value allocates memory on
176176
the heap at runtime, and therefore cannot be done at compile time.
177177
"##,
178178

179+
E0011: r##"
180+
Using a user-defined operator on const/static variable is restricted to what
181+
can be evaluated at compile-time. Using an user-defined operator could call a
182+
user-defined function, which is not allowed.
183+
184+
Bad example:
185+
186+
```
187+
use std::ops::Index;
188+
189+
struct Foo { a: u8 }
190+
191+
impl ::std::ops::Index<u8> for Foo {
192+
type Output = u8;
193+
194+
fn index<'a>(&'a self, idx: u8) -> &'a u8 { &self.a }
195+
}
196+
197+
const a: Foo = Foo { a: 0u8 };
198+
const b: u8 = a[0]; // Index trait is defined by the user, bad!
199+
```
200+
201+
The only traits which can be used have to be already implemented, not user-defined.
202+
203+
Example:
204+
205+
```
206+
const a: &'static [i32] = &[1, 2, 3];
207+
const b: i32 = a[0]; // Good!
208+
```
209+
"##,
210+
179211
E0013: r##"
180212
Static and const variables can refer to other const variables. But a const
181213
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
218250
in a non-constant integer which lead to this error. Example:
219251
220252
```
221-
const X: u32 = 50;
222-
const Y: *const u32 = &X;
223-
println!("{:?}", Y);
253+
const X: u32 = 1;
254+
const Y: usize = &X as *const u32 as usize;
255+
println!("{}", Y);
224256
```
225257
"##,
226258

@@ -396,6 +428,54 @@ enum Method { GET, POST }
396428
```
397429
"##,
398430

431+
E0261: r##"
432+
When using a lifetime like `'a` in a type, it must be declared before being
433+
used.
434+
435+
These two examples illustrate the problem:
436+
437+
```
438+
// error, use of undeclared lifetime name `'a`
439+
fn foo(x: &'a str) { }
440+
441+
struct Foo {
442+
// error, use of undeclared lifetime name `'a`
443+
x: &'a str,
444+
}
445+
```
446+
447+
These can be fixed by declaring lifetime parameters:
448+
449+
```
450+
fn foo<'a>(x: &'a str) { }
451+
452+
struct Foo<'a> {
453+
x: &'a str,
454+
}
455+
```
456+
"##,
457+
458+
E0262: r##"
459+
Declaring certain lifetime names in parameters is disallowed. For example,
460+
because the `'static` lifetime is a special built-in lifetime name denoting
461+
the lifetime of the entire program, this is an error:
462+
463+
```
464+
// error, illegal lifetime parameter name `'static`
465+
fn foo<'static>(x: &'static str) { }
466+
```
467+
"##,
468+
469+
E0263: r##"
470+
A lifetime name cannot be declared more than once in the same scope. For
471+
example:
472+
473+
```
474+
// error, lifetime name `'a` declared twice in the same scope
475+
fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
476+
```
477+
"##,
478+
399479
E0265: r##"
400480
This error indicates that a static or constant references itself.
401481
All statics and constants need to resolve to a value in an acyclic manner.
@@ -800,7 +880,6 @@ struct Foo<T: 'static> {
800880

801881

802882
register_diagnostics! {
803-
E0011,
804883
E0014,
805884
E0016,
806885
E0017,
@@ -814,9 +893,6 @@ register_diagnostics! {
814893
E0136,
815894
E0138,
816895
E0139,
817-
E0261, // use of undeclared lifetime name
818-
E0262, // illegal lifetime parameter name
819-
E0263, // lifetime name declared twice in same scope
820896
E0264, // unknown external lang item
821897
E0266, // expected item
822898
E0269, // not all control paths return a value
@@ -845,5 +921,10 @@ register_diagnostics! {
845921
E0314, // closure outlives stack frame
846922
E0315, // cannot invoke closure outside of its lifetime
847923
E0316, // nested quantification of lifetimes
848-
E0370 // discriminant overflow
924+
E0370, // discriminant overflow
925+
E0378, // method calls limited to constant inherent methods
926+
E0394, // cannot refer to other statics by value, use the address-of
927+
// operator or a constant instead
928+
E0395, // pointer comparison in const-expr
929+
E0396 // pointer dereference in const-expr
849930
}

0 commit comments

Comments
 (0)