Skip to content

Commit 9eb416b

Browse files
Add E0011 explanation
1 parent 474c6e0 commit 9eb416b

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 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+
Only operators on builtin types are allowed.
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`
@@ -899,7 +931,6 @@ static mut BAR: Option<Vec<i32>> = None;
899931

900932

901933
register_diagnostics! {
902-
E0011,
903934
E0014,
904935
E0016,
905936
E0017,

0 commit comments

Comments
 (0)