Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E0011 explanation #25903

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion 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 @@ -899,7 +931,6 @@ static mut BAR: Option<Vec<i32>> = None;


register_diagnostics! {
E0011,
E0014,
E0016,
E0017,
Expand Down