From b7e41d9aeaade8a42ee9e71de34fdf2d982c664f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 24 Jun 2015 20:48:12 +0200 Subject: [PATCH] Add E0396 error explanation --- src/librustc/diagnostics.rs | 55 ++++++++++++++++++++++++++++-- src/librustc_typeck/diagnostics.rs | 51 --------------------------- 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 0f83cdff537..c6b465b47f0 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -984,6 +984,57 @@ From [RFC 246]: [RFC 246]: https://github.com/rust-lang/rfcs/pull/246 "##, +E0395: r##" +The value assigned to a constant expression must be known at compile time, +which is not the case when comparing raw pointers. Erroneous code example: + +``` +static foo: i32 = 42; +static bar: i32 = 43; + +static baz: bool = { (&foo as *const i32) == (&bar as *const i32) }; +// error: raw pointers cannot be compared in statics! +``` + +Please check that the result of the comparison can be determined at compile time +or isn't assigned to a constant expression. Example: + +``` +static foo: i32 = 42; +static bar: i32 = 43; + +let baz: bool = { (&foo as *const i32) == (&bar as *const i32) }; +// baz isn't a constant expression so it's ok +``` +"##, + +E0396: r##" +The value assigned to a constant expression must be known at compile time, +which is not the case when dereferencing raw pointers. Erroneous code +example: + +``` +const foo: i32 = 42; +const baz: *const i32 = (&foo as *const i32); + +const deref: i32 = *baz; +// error: raw pointers cannot be dereferenced in constants! +``` + +To fix this error, please do not assign this value to a constant expression. +Example: + +``` +const foo: i32 = 42; +const baz: *const i32 = (&foo as *const i32); + +unsafe { let deref: i32 = *baz; } +// baz isn't a constant expression so it's ok +``` + +You'll also note that this assignment must be done in an unsafe block! +"##, + E0397: r##" It is not allowed for a mutable static to allocate or have destructors. For example: @@ -1039,7 +1090,5 @@ register_diagnostics! { E0314, // closure outlives stack frame E0315, // cannot invoke closure outside of its lifetime E0316, // nested quantification of lifetimes - E0370, // discriminant overflow - E0395, // pointer comparison in const-expr - E0396 // pointer dereference in const-expr + E0370 // discriminant overflow } diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 7228501c800..d32e194dbd4 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1538,57 +1538,6 @@ For more information see the [opt-in builtin traits RFC](https://github.com/rust -lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md). "## -E0395: r##" -The value assigned to a constant expression must be known at compile time, -which is not the case when comparing raw pointers. Erroneous code example: - -``` -static foo: i32 = 42; -static bar: i32 = 43; - -static baz: bool = { (&foo as *const i32) == (&bar as *const i32) }; -// error: raw pointers cannot be compared in statics! -``` - -To fix this error, please not assign this value to a constant expression. -Example: - -``` -static foo: i32 = 42; -static bar: i32 = 43; - -let baz: bool = { (&foo as *const i32) == (&bar as *const i32) }; -// baz isn't a constant expression so it's ok -``` -"##, - -E0396: r##" -The value assigned to a constant expression must be known at compile time, -which is not the case when dereferencing raw pointers. Erroneous code -example: - -``` -const foo: i32 = 42; -const baz: *const i32 = (&foo as *const i32); - -const deref: i32 = *baz; -// error: raw pointers cannot be dereferenced in constants! -``` - -To fix this error, please not assign this value to a constant expression. -Example: - -``` -const foo: i32 = 42; -const baz: *const i32 = (&foo as *const i32); - -unsafe { let deref: i32 = *baz; } -// baz isn't a constant expression so it's ok -``` - -You'll also note that this assignation must be done in an unsafe block! -"## - }