Skip to content

Commit 24011e3

Browse files
Adding E0011 explanation
1 parent 59ba55d commit 24011e3

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/librustc/diagnostics.rs

+32-1
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 user-defined operator on const/static variable is restricted to what can be
181+
evaluated at compile-time. Using an user-defined operator could call a user-defined
182+
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`
@@ -800,7 +832,6 @@ struct Foo<T: 'static> {
800832

801833

802834
register_diagnostics! {
803-
E0011,
804835
E0014,
805836
E0016,
806837
E0017,

0 commit comments

Comments
 (0)