From 3ef863bfdfb9173a0ad55d24e20202948dda6bbe Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 2 Jul 2018 19:00:07 +0200 Subject: [PATCH 1/9] Place unions, pointer casts and pointer derefs behind extra feature gates --- src/libcore/lib.rs | 1 + src/librustc_mir/diagnostics.rs | 55 ----------- src/librustc_mir/transform/qualify_consts.rs | 97 +++++++------------ src/libsyntax/feature_gate.rs | 9 ++ .../compile-fail/cast-ptr-to-int-const.rs | 4 +- src/test/ui/const-deref-ptr.rs | 3 +- src/test/ui/const-deref-ptr.stderr | 10 +- src/test/ui/const-eval/const_transmute.rs | 2 + .../ui/const-eval/promoted_const_fn_fail.rs | 2 +- .../ui/const-eval/ref_to_float_transmute.rs | 2 + src/test/ui/const-eval/ref_to_int_match.rs | 2 + .../ui/const-eval/ref_to_int_match.stderr | 2 +- .../ui/const-eval/union_promotion.nll.stderr | 11 +++ src/test/ui/error-codes/E0396-fixed.rs | 19 ++++ src/test/ui/error-codes/E0396-fixed.stderr | 12 +++ src/test/ui/error-codes/E0396.rs | 3 +- src/test/ui/error-codes/E0396.stderr | 10 +- src/test/ui/issue-17458.rs | 2 +- src/test/ui/issue-18294.rs | 2 +- src/test/ui/union/union-const-eval.rs | 1 + 20 files changed, 118 insertions(+), 131 deletions(-) create mode 100644 src/test/ui/const-eval/union_promotion.nll.stderr create mode 100644 src/test/ui/error-codes/E0396-fixed.rs create mode 100644 src/test/ui/error-codes/E0396-fixed.stderr diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index a4a01990c22a5..dc4a2d7c0d7b1 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -82,6 +82,7 @@ #![feature(concat_idents)] #![feature(const_fn)] #![feature(const_int_ops)] +#![feature(const_fn_union)] #![feature(custom_attribute)] #![feature(doc_cfg)] #![feature(doc_spotlight)] diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs index 3c751d52b0664..e38b8633108c8 100644 --- a/src/librustc_mir/diagnostics.rs +++ b/src/librustc_mir/diagnostics.rs @@ -619,38 +619,6 @@ If you really want global mutable state, try using `static mut` or a global `UnsafeCell`. "##, -E0018: r##" - -The value of static and constant integers must be known at compile time. You -can't cast a pointer to an integer because the address of a pointer can -vary. - -For example, if you write: - -```compile_fail,E0018 -static MY_STATIC: u32 = 42; -static MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize; -static WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR; -``` - -Then `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However, -the address can change when the program is linked, as well as change -between different executions due to ASLR, and many linkers would -not be able to calculate the value of `WHAT`. - -On the other hand, static and constant pointers can point either to -a known numeric address or to the address of a symbol. - -``` -static MY_STATIC: u32 = 42; -static MY_STATIC_ADDR: &'static u32 = &MY_STATIC; -const CONST_ADDR: *const u8 = 0x5f3759df as *const u8; -``` - -This does not pose a problem by itself because they can't be -accessed directly. -"##, - E0019: r##" A function call isn't allowed in the const's initialization expression because the expression's value must be known at compile-time. Erroneous code @@ -1208,29 +1176,6 @@ fn main() { ``` "##, -E0396: r##" -The value behind a raw pointer can't be determined at compile-time -(or even link-time), which means it can't be used in a constant -expression. Erroneous code example: - -```compile_fail,E0396 -const REG_ADDR: *const u8 = 0x5f3759df as *const u8; - -const VALUE: u8 = unsafe { *REG_ADDR }; -// error: raw pointers cannot be dereferenced in constants -``` - -A possible fix is to dereference your pointer at some point in run-time. - -For example: - -``` -const REG_ADDR: *const u8 = 0x5f3759df as *const u8; - -let reg_value = unsafe { *REG_ADDR }; -``` -"##, - E0492: r##" A borrow of a constant containing interior mutability was attempted. Erroneous code example: diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 208679d2aa08a..025e639d9282b 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -495,37 +495,41 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx); if let ty::TyRawPtr(_) = base_ty.sty { - if this.mode != Mode::Fn { - let mut err = struct_span_err!( - this.tcx.sess, - this.span, - E0396, - "raw pointers cannot be dereferenced in {}s", - this.mode + if this.mode != Mode::Fn && + !this.tcx.sess.features_untracked().const_raw_ptr_deref { + emit_feature_err( + &this.tcx.sess.parse_sess, "const_raw_ptr_deref", + this.span, GateIssue::Language, + &format!( + "dereferencing raw pointers in {}s is unstable", + this.mode, + ), ); - err.span_label(this.span, - "dereference of raw pointer in constant"); - if this.tcx.sess.teach(&err.get_code().unwrap()) { - err.note( - "The value behind a raw pointer can't be determined \ - at compile-time (or even link-time), which means it \ - can't be used in a constant expression." - ); - err.help("A possible fix is to dereference your pointer \ - at some point in run-time."); - } - err.emit(); } } } ProjectionElem::Field(..) | ProjectionElem::Index(_) => { - if this.mode == Mode::Fn { - let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx); - if let Some(def) = base_ty.ty_adt_def() { - if def.is_union() { - this.not_const(); + let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx); + if let Some(def) = base_ty.ty_adt_def() { + if def.is_union() { + match this.mode { + Mode::Fn => {}, + Mode::ConstFn => { + if !this.tcx.sess.features_untracked().const_fn_union { + emit_feature_err( + &this.tcx.sess.parse_sess, "const_fn_union", + this.span, GateIssue::Language, + "unions in const fn are unstable", + ); + } + }, + + | Mode::Static + | Mode::StaticMut + | Mode::Const + => {}, } } } @@ -723,43 +727,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { (CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) => { self.add(Qualif::NOT_CONST); - if self.mode != Mode::Fn { - let mut err = struct_span_err!( - self.tcx.sess, - self.span, - E0018, - "raw pointers cannot be cast to integers in {}s", - self.mode + if self.mode != Mode::Fn && + !self.tcx.sess.features_untracked().const_raw_ptr_deref { + emit_feature_err( + &self.tcx.sess.parse_sess, "const_raw_ptr_deref", + self.span, GateIssue::Language, + &format!( + "casting pointers to integers in {}s is unstable", + self.mode, + ), ); - if self.tcx.sess.teach(&err.get_code().unwrap()) { - err.note("\ -The value of static and constant integers must be known at compile time. You can't cast a pointer \ -to an integer because the address of a pointer can vary. - -For example, if you write: - -``` -static MY_STATIC: u32 = 42; -static MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize; -static WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR; -``` - -Then `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However, the address can change \ -when the program is linked, as well as change between different executions due to ASLR, and many \ -linkers would not be able to calculate the value of `WHAT`. - -On the other hand, static and constant pointers can point either to a known numeric address or to \ -the address of a symbol. - -``` -static MY_STATIC: u32 = 42; -static MY_STATIC_ADDR: &'static u32 = &MY_STATIC; -const CONST_ADDR: *const u8 = 0x5f3759df as *const u8; -``` - -This does not pose a problem by itself because they can't be accessed directly."); - } - err.emit(); } } _ => {} diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index a3822a4a1f93b..5ae9ae7f404a3 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -216,6 +216,15 @@ declare_features! ( // Allows let bindings and destructuring in `const fn` functions and constants. (active, const_let, "1.22.1", Some(48821), None), + // Allows accessing fields of unions inside const fn + (active, const_fn_union, "1.27.0", Some(51909), None), + + // Allows casting raw pointers to `usize` during const eval + (active, const_raw_ptr_to_usize_cast, "1.27.0", Some(51910), None), + + // Allows dereferencing raw pointers during const eval + (active, const_raw_ptr_deref, "1.27.0", Some(51911), None), + // Allows using #[prelude_import] on glob `use` items. // // rustc internal diff --git a/src/test/compile-fail/cast-ptr-to-int-const.rs b/src/test/compile-fail/cast-ptr-to-int-const.rs index 7c32e31d23b24..30f94e1b5da03 100644 --- a/src/test/compile-fail/cast-ptr-to-int-const.rs +++ b/src/test/compile-fail/cast-ptr-to-int-const.rs @@ -9,7 +9,7 @@ // except according to those terms. fn main() { - const X: u32 = main as u32; //~ ERROR E0018 + const X: u32 = main as u32; //~ ERROR casting pointers to integers in constants is unstable const Y: u32 = 0; - const Z: u32 = &Y as *const u32 as u32; //~ ERROR E0018 + const Z: u32 = &Y as *const u32 as u32; //~ ERROR is unstable } diff --git a/src/test/ui/const-deref-ptr.rs b/src/test/ui/const-deref-ptr.rs index fa15f3e87c694..3d0477feb2060 100644 --- a/src/test/ui/const-deref-ptr.rs +++ b/src/test/ui/const-deref-ptr.rs @@ -11,6 +11,7 @@ // Check that you can't dereference raw pointers in constants. fn main() { - static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; //~ ERROR E0396 + static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; + //~^ ERROR dereferencing raw pointers in statics is unstable println!("{}", C); } diff --git a/src/test/ui/const-deref-ptr.stderr b/src/test/ui/const-deref-ptr.stderr index 61db58104d2d2..94a383bcf1620 100644 --- a/src/test/ui/const-deref-ptr.stderr +++ b/src/test/ui/const-deref-ptr.stderr @@ -1,9 +1,11 @@ -error[E0396]: raw pointers cannot be dereferenced in statics +error[E0658]: dereferencing raw pointers in statics is unstable (see issue #51911) --> $DIR/const-deref-ptr.rs:14:29 | -LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; //~ ERROR E0396 - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer in constant +LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0396`. +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/const-eval/const_transmute.rs b/src/test/ui/const-eval/const_transmute.rs index e661444a7b47d..8dd1476d25a77 100644 --- a/src/test/ui/const-eval/const_transmute.rs +++ b/src/test/ui/const-eval/const_transmute.rs @@ -11,6 +11,8 @@ // compile-pass // run-pass +#![feature(const_fn_union)] + union Transmute { t: T, u: U, diff --git a/src/test/ui/const-eval/promoted_const_fn_fail.rs b/src/test/ui/const-eval/promoted_const_fn_fail.rs index f0f35ce614e61..19db07dd330ae 100644 --- a/src/test/ui/const-eval/promoted_const_fn_fail.rs +++ b/src/test/ui/const-eval/promoted_const_fn_fail.rs @@ -10,7 +10,7 @@ // compile-pass -#![feature(const_fn)] +#![feature(const_fn, const_fn_union)] #![deny(const_err)] diff --git a/src/test/ui/const-eval/ref_to_float_transmute.rs b/src/test/ui/const-eval/ref_to_float_transmute.rs index 77d5222cb9c74..1758ac72b633b 100644 --- a/src/test/ui/const-eval/ref_to_float_transmute.rs +++ b/src/test/ui/const-eval/ref_to_float_transmute.rs @@ -10,6 +10,8 @@ //compile-pass +#![feature(const_fn_union)] + fn main() {} static FOO: u32 = 42; diff --git a/src/test/ui/const-eval/ref_to_int_match.rs b/src/test/ui/const-eval/ref_to_int_match.rs index 8ad7f11f0cee8..cb942f465e40f 100644 --- a/src/test/ui/const-eval/ref_to_int_match.rs +++ b/src/test/ui/const-eval/ref_to_int_match.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(const_fn_union)] + fn main() { let n: Int = 40; match n { diff --git a/src/test/ui/const-eval/ref_to_int_match.stderr b/src/test/ui/const-eval/ref_to_int_match.stderr index 64ea57702d706..e82a16c066fdb 100644 --- a/src/test/ui/const-eval/ref_to_int_match.stderr +++ b/src/test/ui/const-eval/ref_to_int_match.stderr @@ -1,5 +1,5 @@ error[E0030]: lower range bound must be less than or equal to upper - --> $DIR/ref_to_int_match.rs:15:9 + --> $DIR/ref_to_int_match.rs:17:9 | LL | 10..=BAR => {}, //~ ERROR lower range bound must be less than or equal to upper | ^^ lower bound larger than upper bound diff --git a/src/test/ui/const-eval/union_promotion.nll.stderr b/src/test/ui/const-eval/union_promotion.nll.stderr new file mode 100644 index 0000000000000..ea95a8b42a480 --- /dev/null +++ b/src/test/ui/const-eval/union_promotion.nll.stderr @@ -0,0 +1,11 @@ +error: internal compiler error: unexpected region for local data ReStatic + --> $DIR/union_promotion.rs:19:29 + | +LL | let x: &'static bool = &unsafe { //~ borrowed value does not live long enough + | _____________________________^ +LL | | Foo { a: &1 }.b == Foo { a: &2 }.b +LL | | }; + | |_____^ + +error: aborting due to previous error + diff --git a/src/test/ui/error-codes/E0396-fixed.rs b/src/test/ui/error-codes/E0396-fixed.rs new file mode 100644 index 0000000000000..08d20e7850d94 --- /dev/null +++ b/src/test/ui/error-codes/E0396-fixed.rs @@ -0,0 +1,19 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_raw_ptr_deref)] + +const REG_ADDR: *const u8 = 0x5f3759df as *const u8; + +const VALUE: u8 = unsafe { *REG_ADDR }; +//~^ ERROR this constant cannot be used + +fn main() { +} diff --git a/src/test/ui/error-codes/E0396-fixed.stderr b/src/test/ui/error-codes/E0396-fixed.stderr new file mode 100644 index 0000000000000..7d3c98c8ea808 --- /dev/null +++ b/src/test/ui/error-codes/E0396-fixed.stderr @@ -0,0 +1,12 @@ +error: this constant cannot be used + --> $DIR/E0396-fixed.rs:15:1 + | +LL | const VALUE: u8 = unsafe { *REG_ADDR }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^---------^^^ + | | + | a memory access tried to interpret some bytes as a pointer + | + = note: #[deny(const_err)] on by default + +error: aborting due to previous error + diff --git a/src/test/ui/error-codes/E0396.rs b/src/test/ui/error-codes/E0396.rs index 7f34acdfb9007..6434620d26ab2 100644 --- a/src/test/ui/error-codes/E0396.rs +++ b/src/test/ui/error-codes/E0396.rs @@ -10,7 +10,8 @@ const REG_ADDR: *const u8 = 0x5f3759df as *const u8; -const VALUE: u8 = unsafe { *REG_ADDR }; //~ ERROR E0396 +const VALUE: u8 = unsafe { *REG_ADDR }; +//~^ ERROR dereferencing raw pointers in constants is unstable fn main() { } diff --git a/src/test/ui/error-codes/E0396.stderr b/src/test/ui/error-codes/E0396.stderr index 87dfd50dc9735..70331acc0e9a6 100644 --- a/src/test/ui/error-codes/E0396.stderr +++ b/src/test/ui/error-codes/E0396.stderr @@ -1,9 +1,11 @@ -error[E0396]: raw pointers cannot be dereferenced in constants +error[E0658]: dereferencing raw pointers in constants is unstable (see issue #51911) --> $DIR/E0396.rs:13:28 | -LL | const VALUE: u8 = unsafe { *REG_ADDR }; //~ ERROR E0396 - | ^^^^^^^^^ dereference of raw pointer in constant +LL | const VALUE: u8 = unsafe { *REG_ADDR }; + | ^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0396`. +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/issue-17458.rs b/src/test/ui/issue-17458.rs index f5b7a0c13b728..354d389158d61 100644 --- a/src/test/ui/issue-17458.rs +++ b/src/test/ui/issue-17458.rs @@ -9,7 +9,7 @@ // except according to those terms. static X: usize = 0 as *const usize as usize; -//~^ ERROR: raw pointers cannot be cast to integers in statics +//~^ ERROR: casting pointers to integers in statics is unstable fn main() { assert_eq!(X, 0); diff --git a/src/test/ui/issue-18294.rs b/src/test/ui/issue-18294.rs index efc1ba1635c95..28dc6846f69e1 100644 --- a/src/test/ui/issue-18294.rs +++ b/src/test/ui/issue-18294.rs @@ -10,6 +10,6 @@ fn main() { const X: u32 = 1; - const Y: usize = &X as *const u32 as usize; //~ ERROR E0018 + const Y: usize = &X as *const u32 as usize; //~ ERROR is unstable println!("{}", Y); } diff --git a/src/test/ui/union/union-const-eval.rs b/src/test/ui/union/union-const-eval.rs index 3ae76e1a82a5c..c640acec05e80 100644 --- a/src/test/ui/union/union-const-eval.rs +++ b/src/test/ui/union/union-const-eval.rs @@ -9,6 +9,7 @@ // except according to those terms. // compile-pass +#![feature(const_fn_union)] union U { a: usize, From fee07534bbe192befadb03b1626c2bbf3d6e5cfb Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 2 Jul 2018 19:59:39 +0200 Subject: [PATCH 2/9] Add feature gate checks --- .../compile-fail/cast-ptr-to-int-const.rs | 2 ++ .../const-eval/feature-gate-const_fn_union.rs | 22 +++++++++++++++++++ .../feature-gate-const_fn_union.stderr | 11 ++++++++++ src/test/ui/error-codes/E0396.rs | 2 ++ src/test/ui/error-codes/E0396.stderr | 2 +- 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/const-eval/feature-gate-const_fn_union.rs create mode 100644 src/test/ui/const-eval/feature-gate-const_fn_union.stderr diff --git a/src/test/compile-fail/cast-ptr-to-int-const.rs b/src/test/compile-fail/cast-ptr-to-int-const.rs index 30f94e1b5da03..8764cb72b8cc1 100644 --- a/src/test/compile-fail/cast-ptr-to-int-const.rs +++ b/src/test/compile-fail/cast-ptr-to-int-const.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// gate-test-const_raw_ptr_to_usize_cast + fn main() { const X: u32 = main as u32; //~ ERROR casting pointers to integers in constants is unstable const Y: u32 = 0; diff --git a/src/test/ui/const-eval/feature-gate-const_fn_union.rs b/src/test/ui/const-eval/feature-gate-const_fn_union.rs new file mode 100644 index 0000000000000..113046b0689b2 --- /dev/null +++ b/src/test/ui/const-eval/feature-gate-const_fn_union.rs @@ -0,0 +1,22 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_fn)] + +fn main() {} + +union Foo { + u: u32, + i: i32, +} + +const unsafe fn foo(u: u32) -> i32 { + Foo { u }.i //~ ERROR unions in const fn are unstable +} diff --git a/src/test/ui/const-eval/feature-gate-const_fn_union.stderr b/src/test/ui/const-eval/feature-gate-const_fn_union.stderr new file mode 100644 index 0000000000000..1e28f14165fe9 --- /dev/null +++ b/src/test/ui/const-eval/feature-gate-const_fn_union.stderr @@ -0,0 +1,11 @@ +error[E0658]: unions in const fn are unstable (see issue #51909) + --> $DIR/feature-gate-const_fn_union.rs:21:5 + | +LL | Foo { u }.i //~ ERROR unions in const fn are unstable + | ^^^^^^^^^^^ + | + = help: add #![feature(const_fn_union)] to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/error-codes/E0396.rs b/src/test/ui/error-codes/E0396.rs index 6434620d26ab2..1ee8a74a46547 100644 --- a/src/test/ui/error-codes/E0396.rs +++ b/src/test/ui/error-codes/E0396.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// gate-test-const_raw_ptr_deref + const REG_ADDR: *const u8 = 0x5f3759df as *const u8; const VALUE: u8 = unsafe { *REG_ADDR }; diff --git a/src/test/ui/error-codes/E0396.stderr b/src/test/ui/error-codes/E0396.stderr index 70331acc0e9a6..a2a2e7243588f 100644 --- a/src/test/ui/error-codes/E0396.stderr +++ b/src/test/ui/error-codes/E0396.stderr @@ -1,5 +1,5 @@ error[E0658]: dereferencing raw pointers in constants is unstable (see issue #51911) - --> $DIR/E0396.rs:13:28 + --> $DIR/E0396.rs:15:28 | LL | const VALUE: u8 = unsafe { *REG_ADDR }; | ^^^^^^^^^ From c0aedc0b6a64c919bb618b7c7ffe2622338302dd Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 13 Jul 2018 10:53:46 +0200 Subject: [PATCH 3/9] Use the correct feature gate name --- src/librustc_mir/transform/qualify_consts.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 025e639d9282b..89d539f6bcaed 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -728,9 +728,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { (CastTy::FnPtr, CastTy::Int(_)) => { self.add(Qualif::NOT_CONST); if self.mode != Mode::Fn && - !self.tcx.sess.features_untracked().const_raw_ptr_deref { + !self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast { emit_feature_err( - &self.tcx.sess.parse_sess, "const_raw_ptr_deref", + &self.tcx.sess.parse_sess, "const_raw_ptr_to_usize_cast", self.span, GateIssue::Language, &format!( "casting pointers to integers in {}s is unstable", From 07e2dd7d96a7fb235ac8a4a56f5c436e381f573d Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 13 Jul 2018 16:55:15 +0200 Subject: [PATCH 4/9] Don't accidentally promote union access in MIR --- src/librustc_mir/transform/qualify_consts.rs | 2 +- src/test/ui/const-eval/union_promotion.nll.stderr | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 src/test/ui/const-eval/union_promotion.nll.stderr diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 89d539f6bcaed..7a724a2e39492 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -515,7 +515,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { if let Some(def) = base_ty.ty_adt_def() { if def.is_union() { match this.mode { - Mode::Fn => {}, + Mode::Fn => this.not_const(), Mode::ConstFn => { if !this.tcx.sess.features_untracked().const_fn_union { emit_feature_err( diff --git a/src/test/ui/const-eval/union_promotion.nll.stderr b/src/test/ui/const-eval/union_promotion.nll.stderr deleted file mode 100644 index ea95a8b42a480..0000000000000 --- a/src/test/ui/const-eval/union_promotion.nll.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: internal compiler error: unexpected region for local data ReStatic - --> $DIR/union_promotion.rs:19:29 - | -LL | let x: &'static bool = &unsafe { //~ borrowed value does not live long enough - | _____________________________^ -LL | | Foo { a: &1 }.b == Foo { a: &2 }.b -LL | | }; - | |_____^ - -error: aborting due to previous error - From 1fc7580a8e75020c486cd73ab3b149f0df2a6abd Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 17 Jul 2018 13:46:48 +0200 Subject: [PATCH 5/9] Rebase fallout: new tests need updated ui output --- src/test/ui/const-eval/match-test-ptr-null.rs | 2 +- src/test/ui/const-eval/match-test-ptr-null.stderr | 12 +++++++----- src/test/ui/issue-17458.stderr | 6 ++++-- src/test/ui/issue-18294.stderr | 8 +++++--- src/test/ui/issue-52023-array-size-pointer-cast.rs | 2 +- .../ui/issue-52023-array-size-pointer-cast.stderr | 8 +++++--- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/test/ui/const-eval/match-test-ptr-null.rs b/src/test/ui/const-eval/match-test-ptr-null.rs index 19b3dcc318154..81fcd23fb786d 100644 --- a/src/test/ui/const-eval/match-test-ptr-null.rs +++ b/src/test/ui/const-eval/match-test-ptr-null.rs @@ -13,7 +13,7 @@ fn main() { // that pointer comparison is disallowed, not that parts of a pointer are accessed as raw // bytes. let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length - match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers + match &1 as *const i32 as usize { //~ ERROR casting pointers to integers in constants 0 => 42, //~ ERROR constant contains unimplemented expression type //~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed n => n, diff --git a/src/test/ui/const-eval/match-test-ptr-null.stderr b/src/test/ui/const-eval/match-test-ptr-null.stderr index 726ada9b428aa..26577948faefa 100644 --- a/src/test/ui/const-eval/match-test-ptr-null.stderr +++ b/src/test/ui/const-eval/match-test-ptr-null.stderr @@ -1,8 +1,10 @@ -error[E0018]: raw pointers cannot be cast to integers in constants +error[E0658]: casting pointers to integers in constants is unstable (see issue #51910) --> $DIR/match-test-ptr-null.rs:16:15 | -LL | match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers +LL | match &1 as *const i32 as usize { //~ ERROR casting pointers to integers in constants | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_to_usize_cast)] to the crate attributes to enable error[E0019]: constant contains unimplemented expression type --> $DIR/match-test-ptr-null.rs:17:13 @@ -15,7 +17,7 @@ error[E0080]: could not evaluate repeat length | LL | let _: [u8; 0] = [4; { //~ ERROR could not evaluate repeat length | __________________________^ -LL | | match &1 as *const i32 as usize { //~ ERROR raw pointers cannot be cast to integers +LL | | match &1 as *const i32 as usize { //~ ERROR casting pointers to integers in constants LL | | 0 => 42, //~ ERROR constant contains unimplemented expression type | | - "pointer arithmetic or comparison" needs an rfc before being allowed inside constants LL | | //~^ NOTE "pointer arithmetic or comparison" needs an rfc before being allowed @@ -26,5 +28,5 @@ LL | | }]; error: aborting due to 3 previous errors -Some errors occurred: E0018, E0019, E0080. -For more information about an error, try `rustc --explain E0018`. +Some errors occurred: E0019, E0080, E0658. +For more information about an error, try `rustc --explain E0019`. diff --git a/src/test/ui/issue-17458.stderr b/src/test/ui/issue-17458.stderr index 7a43813fa6d38..0303e4bddb56f 100644 --- a/src/test/ui/issue-17458.stderr +++ b/src/test/ui/issue-17458.stderr @@ -1,9 +1,11 @@ -error[E0018]: raw pointers cannot be cast to integers in statics +error[E0658]: casting pointers to integers in statics is unstable (see issue #51910) --> $DIR/issue-17458.rs:11:19 | LL | static X: usize = 0 as *const usize as usize; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_to_usize_cast)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0018`. +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/issue-18294.stderr b/src/test/ui/issue-18294.stderr index 151deefb2b7c3..0b94e778d3771 100644 --- a/src/test/ui/issue-18294.stderr +++ b/src/test/ui/issue-18294.stderr @@ -1,9 +1,11 @@ -error[E0018]: raw pointers cannot be cast to integers in constants +error[E0658]: casting pointers to integers in constants is unstable (see issue #51910) --> $DIR/issue-18294.rs:13:22 | -LL | const Y: usize = &X as *const u32 as usize; //~ ERROR E0018 +LL | const Y: usize = &X as *const u32 as usize; //~ ERROR is unstable | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_to_usize_cast)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0018`. +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/issue-52023-array-size-pointer-cast.rs b/src/test/ui/issue-52023-array-size-pointer-cast.rs index f3bee1a631580..02bed69f0d40e 100644 --- a/src/test/ui/issue-52023-array-size-pointer-cast.rs +++ b/src/test/ui/issue-52023-array-size-pointer-cast.rs @@ -9,5 +9,5 @@ // except according to those terms. fn main() { - let _ = [0; (&0 as *const i32) as usize]; //~ ERROR raw pointers cannot be cast + let _ = [0; (&0 as *const i32) as usize]; //~ ERROR casting pointers to integers in constants } diff --git a/src/test/ui/issue-52023-array-size-pointer-cast.stderr b/src/test/ui/issue-52023-array-size-pointer-cast.stderr index 888de82e379c8..74270c2bef798 100644 --- a/src/test/ui/issue-52023-array-size-pointer-cast.stderr +++ b/src/test/ui/issue-52023-array-size-pointer-cast.stderr @@ -1,9 +1,11 @@ -error[E0018]: raw pointers cannot be cast to integers in constants +error[E0658]: casting pointers to integers in constants is unstable (see issue #51910) --> $DIR/issue-52023-array-size-pointer-cast.rs:12:17 | -LL | let _ = [0; (&0 as *const i32) as usize]; //~ ERROR raw pointers cannot be cast +LL | let _ = [0; (&0 as *const i32) as usize]; //~ ERROR casting pointers to integers in constants | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_raw_ptr_to_usize_cast)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0018`. +For more information about this error, try `rustc --explain E0658`. From 36907fc18d10b05d8b7a7383a4f0ecab9b431845 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 3 Aug 2018 11:12:10 +0200 Subject: [PATCH 6/9] Also put comparing raw pointers behind a feature gate --- src/librustc_mir/diagnostics.rs | 30 -------------------- src/librustc_mir/transform/qualify_consts.rs | 13 ++++----- src/libsyntax/feature_gate.rs | 3 ++ src/test/ui/error-codes/E0395.rs | 2 +- src/test/ui/error-codes/E0395.stderr | 10 ++++--- src/test/ui/issue-25826.rs | 2 +- src/test/ui/issue-25826.stderr | 8 ++++-- 7 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs index e38b8633108c8..3312cd78c547b 100644 --- a/src/librustc_mir/diagnostics.rs +++ b/src/librustc_mir/diagnostics.rs @@ -1113,36 +1113,6 @@ fn main() { ``` "##, -E0395: r##" -The value assigned to a constant scalar must be known at compile time, -which is not the case when comparing raw pointers. - -Erroneous code example: - -```compile_fail,E0395 -static FOO: i32 = 42; -static BAR: i32 = 42; - -static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; -// error: raw pointers cannot be compared in statics! -``` - -The address assigned by the linker to `FOO` and `BAR` may or may not -be identical, so the value of `BAZ` can't be determined. - -If you want to do the comparison, please do it at run-time. - -For example: - -``` -static FOO: i32 = 42; -static BAR: i32 = 42; - -let baz: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; -// baz isn't a constant expression so it's ok -``` -"##, - E0161: r##" A value was moved. However, its size was not known at compile time, and only values of a known size can be moved. diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 7a724a2e39492..1a66a1751ffa0 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -752,14 +752,13 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { self.add(Qualif::NOT_CONST); if self.mode != Mode::Fn { - struct_span_err!( - self.tcx.sess, self.span, E0395, - "raw pointers cannot be compared in {}s", - self.mode) - .span_label( + emit_feature_err( + &self.tcx.sess.parse_sess, + "const_compare_raw_pointers", self.span, - "comparing raw pointers in static") - .emit(); + GateIssue::Language, + &format!("comparing raw pointers inside {}", self.mode), + ); } } } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 5ae9ae7f404a3..a3090597096d9 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -225,6 +225,9 @@ declare_features! ( // Allows dereferencing raw pointers during const eval (active, const_raw_ptr_deref, "1.27.0", Some(51911), None), + // Allows comparing raw pointers during const eval + (active, const_compare_raw_pointers, "1.27.0", Some(53020), None), + // Allows using #[prelude_import] on glob `use` items. // // rustc internal diff --git a/src/test/ui/error-codes/E0395.rs b/src/test/ui/error-codes/E0395.rs index 00008ea6b6f37..e8d1b0f54c1c0 100644 --- a/src/test/ui/error-codes/E0395.rs +++ b/src/test/ui/error-codes/E0395.rs @@ -11,6 +11,6 @@ static FOO: i32 = 42; static BAR: i32 = 42; -static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR E0395 +static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020 fn main() { } diff --git a/src/test/ui/error-codes/E0395.stderr b/src/test/ui/error-codes/E0395.stderr index ee90df798706a..90f9ab9ec3641 100644 --- a/src/test/ui/error-codes/E0395.stderr +++ b/src/test/ui/error-codes/E0395.stderr @@ -1,9 +1,11 @@ -error[E0395]: raw pointers cannot be compared in statics +error[E0658]: comparing raw pointers inside static (see issue #53020) --> $DIR/E0395.rs:14:22 | -LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR E0395 - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comparing raw pointers in static +LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_compare_raw_pointers)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0395`. +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/issue-25826.rs b/src/test/ui/issue-25826.rs index 00e1279d58a0e..6b9caba0218f3 100644 --- a/src/test/ui/issue-25826.rs +++ b/src/test/ui/issue-25826.rs @@ -11,6 +11,6 @@ fn id(t: T) -> T { t } fn main() { const A: bool = id:: as *const () < id:: as *const (); - //~^ ERROR raw pointers cannot be compared in constants [E0395] + //~^ ERROR comparing raw pointers inside constant println!("{}", A); } diff --git a/src/test/ui/issue-25826.stderr b/src/test/ui/issue-25826.stderr index fed9e8efa8487..a5ab7cfa6d3c5 100644 --- a/src/test/ui/issue-25826.stderr +++ b/src/test/ui/issue-25826.stderr @@ -1,9 +1,11 @@ -error[E0395]: raw pointers cannot be compared in constants +error[E0658]: comparing raw pointers inside constant (see issue #53020) --> $DIR/issue-25826.rs:13:21 | LL | const A: bool = id:: as *const () < id:: as *const (); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comparing raw pointers in static + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(const_compare_raw_pointers)] to the crate attributes to enable error: aborting due to previous error -For more information about this error, try `rustc --explain E0395`. +For more information about this error, try `rustc --explain E0658`. From aa0884ecc11ec60a9fceeda8dc461798809ad1b8 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 3 Aug 2018 12:33:47 +0200 Subject: [PATCH 7/9] Add feature gate test --- src/test/ui/error-codes/E0395.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/ui/error-codes/E0395.rs b/src/test/ui/error-codes/E0395.rs index e8d1b0f54c1c0..617050732b7de 100644 --- a/src/test/ui/error-codes/E0395.rs +++ b/src/test/ui/error-codes/E0395.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// gate-test-const_compare_raw_pointers + static FOO: i32 = 42; static BAR: i32 = 42; From a091a6567c210686fdbc77cd1c5d5bca5e86cd3d Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 3 Aug 2018 13:27:35 +0200 Subject: [PATCH 8/9] Make sure the feature gate actually works and never allows promoting these operations --- src/librustc_mir/transform/qualify_consts.rs | 40 ++++++++++--------- src/test/ui/const-eval/const_raw_ptr_ops.rs | 27 +++++++++++++ .../ui/const-eval/const_raw_ptr_ops.stderr | 36 +++++++++++++++++ .../ui/const-eval/promoted_raw_ptr_ops.rs | 18 +++++++++ .../ui/const-eval/promoted_raw_ptr_ops.stderr | 35 ++++++++++++++++ src/test/ui/error-codes/E0395.stderr | 2 +- 6 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 src/test/ui/const-eval/const_raw_ptr_ops.rs create mode 100644 src/test/ui/const-eval/const_raw_ptr_ops.stderr create mode 100644 src/test/ui/const-eval/promoted_raw_ptr_ops.rs create mode 100644 src/test/ui/const-eval/promoted_raw_ptr_ops.stderr diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 1a66a1751ffa0..d876ee77e76cf 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -491,20 +491,21 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { this.super_place(place, context, location); match proj.elem { ProjectionElem::Deref => { - this.add(Qualif::NOT_CONST); - - let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx); - if let ty::TyRawPtr(_) = base_ty.sty { - if this.mode != Mode::Fn && - !this.tcx.sess.features_untracked().const_raw_ptr_deref { - emit_feature_err( - &this.tcx.sess.parse_sess, "const_raw_ptr_deref", - this.span, GateIssue::Language, - &format!( - "dereferencing raw pointers in {}s is unstable", - this.mode, - ), - ); + if let Mode::Fn = this.mode { + this.add(Qualif::NOT_CONST); + } else { + let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx); + if let ty::TyRawPtr(_) = base_ty.sty { + if !this.tcx.sess.features_untracked().const_raw_ptr_deref { + emit_feature_err( + &this.tcx.sess.parse_sess, "const_raw_ptr_deref", + this.span, GateIssue::Language, + &format!( + "dereferencing raw pointers in {}s is unstable", + this.mode, + ), + ); + } } } } @@ -726,9 +727,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { match (cast_in, cast_out) { (CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) => { - self.add(Qualif::NOT_CONST); - if self.mode != Mode::Fn && - !self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast { + if let Mode::Fn = self.mode { + self.add(Qualif::NOT_CONST); + } else if !self.tcx.sess.features_untracked().const_raw_ptr_to_usize_cast { emit_feature_err( &self.tcx.sess.parse_sess, "const_raw_ptr_to_usize_cast", self.span, GateIssue::Language, @@ -750,8 +751,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { op == BinOp::Ge || op == BinOp::Gt || op == BinOp::Offset); - self.add(Qualif::NOT_CONST); - if self.mode != Mode::Fn { + if let Mode::Fn = self.mode { + self.add(Qualif::NOT_CONST); + } else if !self.tcx.sess.features_untracked().const_compare_raw_pointers { emit_feature_err( &self.tcx.sess.parse_sess, "const_compare_raw_pointers", diff --git a/src/test/ui/const-eval/const_raw_ptr_ops.rs b/src/test/ui/const-eval/const_raw_ptr_ops.rs new file mode 100644 index 0000000000000..9121c67b8b9ba --- /dev/null +++ b/src/test/ui/const-eval/const_raw_ptr_ops.rs @@ -0,0 +1,27 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_raw_ptr_to_usize_cast, const_compare_raw_pointers, const_raw_ptr_deref)] + +fn main() {} + +// unconst and bad, will thus error in miri +const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used +// unconst and fine +const X2: bool = 42 as *const i32 == 43 as *const i32; +// unconst and fine +const Y: usize = 42usize as *const i32 as usize + 1; +// unconst and bad, will thus error in miri +const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used +// unconst and fine +const Z: i32 = unsafe { *(&1 as *const i32) }; +// unconst and bad, will thus error in miri +const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used +const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used \ No newline at end of file diff --git a/src/test/ui/const-eval/const_raw_ptr_ops.stderr b/src/test/ui/const-eval/const_raw_ptr_ops.stderr new file mode 100644 index 0000000000000..a9442be081d94 --- /dev/null +++ b/src/test/ui/const-eval/const_raw_ptr_ops.stderr @@ -0,0 +1,36 @@ +error: this constant cannot be used + --> $DIR/const_raw_ptr_ops.rs:16:1 + | +LL | const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used + | ^^^^^^^^^^^^^^^^------------------------------------^ + | | + | "pointer arithmetic or comparison" needs an rfc before being allowed inside constants + | + = note: #[deny(const_err)] on by default + +error: this constant cannot be used + --> $DIR/const_raw_ptr_ops.rs:22:1 + | +LL | const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used + | ^^^^^^^^^^^^^^^^^^-----------------------------^ + | | + | "pointer arithmetic or comparison" needs an rfc before being allowed inside constants + +error: this constant cannot be used + --> $DIR/const_raw_ptr_ops.rs:26:1 + | +LL | const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used + | ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^ + | | + | tried to access memory with alignment 2, but alignment 4 is required + +error: this constant cannot be used + --> $DIR/const_raw_ptr_ops.rs:27:1 + | +LL | const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used + | ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^ + | | + | a memory access tried to interpret some bytes as a pointer + +error: aborting due to 4 previous errors + diff --git a/src/test/ui/const-eval/promoted_raw_ptr_ops.rs b/src/test/ui/const-eval/promoted_raw_ptr_ops.rs new file mode 100644 index 0000000000000..30e7648f04d9a --- /dev/null +++ b/src/test/ui/const-eval/promoted_raw_ptr_ops.rs @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(const_raw_ptr_to_usize_cast, const_compare_raw_pointers, const_raw_ptr_deref)] + +fn main() { + let x: &'static bool = &(42 as *const i32 == 43 as *const i32); + //~^ ERROR does not live long enough + let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough + let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough +} \ No newline at end of file diff --git a/src/test/ui/const-eval/promoted_raw_ptr_ops.stderr b/src/test/ui/const-eval/promoted_raw_ptr_ops.stderr new file mode 100644 index 0000000000000..90c73c095fba1 --- /dev/null +++ b/src/test/ui/const-eval/promoted_raw_ptr_ops.stderr @@ -0,0 +1,35 @@ +error[E0597]: borrowed value does not live long enough + --> $DIR/promoted_raw_ptr_ops.rs:14:29 + | +LL | let x: &'static bool = &(42 as *const i32 == 43 as *const i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough +... +LL | } + | - temporary value only lives until here + | + = note: borrowed value must be valid for the static lifetime... + +error[E0597]: borrowed value does not live long enough + --> $DIR/promoted_raw_ptr_ops.rs:16:30 + | +LL | let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough +LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough +LL | } + | - temporary value only lives until here + | + = note: borrowed value must be valid for the static lifetime... + +error[E0597]: borrowed value does not live long enough + --> $DIR/promoted_raw_ptr_ops.rs:17:28 + | +LL | let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough +LL | } + | - temporary value only lives until here + | + = note: borrowed value must be valid for the static lifetime... + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/error-codes/E0395.stderr b/src/test/ui/error-codes/E0395.stderr index 90f9ab9ec3641..0fb9a9e854ddb 100644 --- a/src/test/ui/error-codes/E0395.stderr +++ b/src/test/ui/error-codes/E0395.stderr @@ -1,5 +1,5 @@ error[E0658]: comparing raw pointers inside static (see issue #53020) - --> $DIR/E0395.rs:14:22 + --> $DIR/E0395.rs:16:22 | LL | static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) }; //~ ERROR issue #53020 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 4b731a908b3df2a926eae5fb5233dbc73b098276 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 3 Aug 2018 14:22:22 +0200 Subject: [PATCH 9/9] Fix tidy --- src/test/ui/const-eval/const_raw_ptr_ops.rs | 2 +- src/test/ui/const-eval/promoted_raw_ptr_ops.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/const-eval/const_raw_ptr_ops.rs b/src/test/ui/const-eval/const_raw_ptr_ops.rs index 9121c67b8b9ba..2aff6a7c55c20 100644 --- a/src/test/ui/const-eval/const_raw_ptr_ops.rs +++ b/src/test/ui/const-eval/const_raw_ptr_ops.rs @@ -24,4 +24,4 @@ const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used const Z: i32 = unsafe { *(&1 as *const i32) }; // unconst and bad, will thus error in miri const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used -const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used \ No newline at end of file +const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used diff --git a/src/test/ui/const-eval/promoted_raw_ptr_ops.rs b/src/test/ui/const-eval/promoted_raw_ptr_ops.rs index 30e7648f04d9a..3b437f69d8d63 100644 --- a/src/test/ui/const-eval/promoted_raw_ptr_ops.rs +++ b/src/test/ui/const-eval/promoted_raw_ptr_ops.rs @@ -15,4 +15,4 @@ fn main() { //~^ ERROR does not live long enough let y: &'static usize = &(&1 as *const i32 as usize + 1); //~ ERROR does not live long enough let z: &'static i32 = &(unsafe { *(42 as *const i32) }); //~ ERROR does not live long enough -} \ No newline at end of file +}