From 47c3dc2e7eda06c5637685597359bb23939cbfe4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 24 Jun 2015 20:45:22 +0200 Subject: [PATCH 1/3] Add E0395 error explanation --- src/librustc_typeck/diagnostics.rs | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 18341af7ea4b..7228501c800c 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1538,8 +1538,60 @@ 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! +"## + } + register_diagnostics! { E0068, E0074, From b7e41d9aeaade8a42ee9e71de34fdf2d982c664f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 24 Jun 2015 20:48:12 +0200 Subject: [PATCH 2/3] 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 0f83cdff5372..c6b465b47f02 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 7228501c800c..d32e194dbd43 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! -"## - } From 7b4eb1aa90e49f6d068052b6e2baa5f59336ee13 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 24 Jun 2015 22:32:12 +0200 Subject: [PATCH 3/3] Add E0327 error code. Thanks @nagisa for his help! --- src/librustc/diagnostics.rs | 2 +- src/librustc_typeck/diagnostics.rs | 38 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index c6b465b47f02..034d3ee1604a 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -1018,7 +1018,7 @@ const foo: i32 = 42; const baz: *const i32 = (&foo as *const i32); const deref: i32 = *baz; -// error: raw pointers cannot be dereferenced in constants! +// error: raw pointers cannot be dereferenced in constants ``` To fix this error, please do not assign this value to a constant expression. diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index d32e194dbd43..2154aee320f7 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1457,6 +1457,42 @@ impl Foo for Bar { ``` "##, +E0327: r##" +You cannot use associated items other than constant items as patterns. This +includes method items. Example of erroneous code: + +``` +enum B {} + +impl B { + fn bb() -> i32 { 0 } +} + +fn main() { + match 0 { + B::bb => {} // error: associated items in match patterns must + // be constants + } +} +``` + +Please check that you're not using a method as a pattern. Example: + +``` +enum B { + ba, + bb +} + +fn main() { + match B::ba { + B::bb => {} // ok! + _ => {} + } +} +``` +"##, + E0368: r##" This error indicates that a binary assignment operator like `+=` or `^=` was applied to the wrong types. For example: @@ -1540,7 +1576,6 @@ For more information see the [opt-in builtin traits RFC](https://github.com/rust } - register_diagnostics! { E0068, E0074, @@ -1640,7 +1675,6 @@ register_diagnostics! { E0323, // implemented an associated const when another trait item expected E0324, // implemented a method when another trait item expected E0325, // implemented an associated type when another trait item expected - E0327, // referred to method instead of constant in match pattern E0328, // cannot implement Unsize explicitly E0329, // associated const depends on type parameter or Self. E0366, // dropck forbid specialization to concrete type or region