From 9eb416b8a44478b69102189ff6ff8fdce8d4d116 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 30 May 2015 00:44:22 +0200 Subject: [PATCH 1/3] Add E0011 explanation --- src/librustc/diagnostics.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index a3577981c1e..4597ae40161 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -176,6 +176,38 @@ struct X { x: (), } 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 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! +``` + +Only operators on builtin types are allowed. + +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` @@ -899,7 +931,6 @@ fn bar(&self) -> i32 { self.0 } register_diagnostics! { - E0011, E0014, E0016, E0017, From 42c5c982c4589d4e1f480407d29769f176f4e104 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 1 Jun 2015 10:46:28 +0200 Subject: [PATCH 2/3] Remove full path --- src/librustc/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 4597ae40161..e1034976589 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -188,7 +188,7 @@ struct X { x: (), } struct Foo { a: u8 } -impl ::std::ops::Index for Foo { +impl Index for Foo { type Output = u8; fn index<'a>(&'a self, idx: u8) -> &'a u8 { &self.a } From dbfc8c5a0711a0746d0af8fdf798f3fc631c83b6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 1 Jun 2015 12:33:41 +0200 Subject: [PATCH 3/3] Update diagnostics.rs --- src/librustc/diagnostics.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index e1034976589..5e79e5a5a4e 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -177,9 +177,9 @@ struct X { x: (), } "##, 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. +Initializers for constants and statics are evaluated at compile time. +User-defined operators rely on user-defined functions, which cannot be evaluated +at compile time. Bad example: