From 2022fac4df5830a1a346f0174f189cc2894f7229 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sun, 22 Dec 2019 21:40:00 +0100 Subject: [PATCH] Constants reading or referencing statics is illegal and some uses of it will be illegal forever (e.g. mutable or interior mutable statics) --- src/librustc_mir/const_eval.rs | 7 ++++--- src/test/ui/consts/const-points-to-static.stderr | 2 +- src/test/ui/consts/const-prop-read-static-in-const.stderr | 2 +- .../ui/consts/miri_unleashed/const_refers_to_static.stderr | 4 ++-- src/test/ui/issues/issue-52060.stderr | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 3ea76fab6dd..4fa4e87e0ff 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -182,6 +182,7 @@ fn eval_body_using_ecx<'mir, 'tcx>( #[derive(Clone, Debug)] pub enum ConstEvalError { NeedsRfc(String), + ConstAccessesStatic, } impl<'tcx> Into> for ConstEvalError { @@ -201,6 +202,7 @@ impl fmt::Display for ConstEvalError { msg ) } + ConstAccessesStatic => write!(f, "constant accesses static"), } } } @@ -210,6 +212,7 @@ impl Error for ConstEvalError { use self::ConstEvalError::*; match *self { NeedsRfc(_) => "this feature needs an rfc before being allowed inside constants", + ConstAccessesStatic => "constant accesses static", } } @@ -547,9 +550,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, if memory_extra.can_access_statics { Ok(()) } else { - Err(ConstEvalError::NeedsRfc( - "constants accessing static items".to_string(), - ).into()) + Err(ConstEvalError::ConstAccessesStatic.into()) } } } diff --git a/src/test/ui/consts/const-points-to-static.stderr b/src/test/ui/consts/const-points-to-static.stderr index 5ebd643b39d..8949358e293 100644 --- a/src/test/ui/consts/const-points-to-static.stderr +++ b/src/test/ui/consts/const-points-to-static.stderr @@ -8,7 +8,7 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/const-points-to-static.rs:5:1 | LL | const TEST: &u8 = &MY_STATIC; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "constants accessing static items" needs an rfc before being allowed inside constants + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. diff --git a/src/test/ui/consts/const-prop-read-static-in-const.stderr b/src/test/ui/consts/const-prop-read-static-in-const.stderr index 993cf455aba..bfaa0f934ad 100644 --- a/src/test/ui/consts/const-prop-read-static-in-const.stderr +++ b/src/test/ui/consts/const-prop-read-static-in-const.stderr @@ -10,7 +10,7 @@ error: any use of this value will cause an error LL | const TEST: u8 = MY_STATIC; | -----------------^^^^^^^^^- | | - | "constants accessing static items" needs an rfc before being allowed inside constants + | constant accesses static | = note: `#[deny(const_err)]` on by default diff --git a/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr b/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr index 044a5232e5a..7a8c9d44e5a 100644 --- a/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr +++ b/src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr @@ -42,7 +42,7 @@ LL | | static FOO: AtomicUsize = AtomicUsize::new(0); LL | | unsafe { &*(&FOO as *const _ as *const usize) } LL | | LL | | }; - | |__^ "constants accessing static items" needs an rfc before being allowed inside constants + | |__^ constant accesses static | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. @@ -54,7 +54,7 @@ LL | | static FOO: usize = 0; LL | | &FOO LL | | LL | | }; - | |__^ "constants accessing static items" needs an rfc before being allowed inside constants + | |__^ constant accesses static | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. diff --git a/src/test/ui/issues/issue-52060.stderr b/src/test/ui/issues/issue-52060.stderr index 0b54b31abec..c69145c1fe8 100644 --- a/src/test/ui/issues/issue-52060.stderr +++ b/src/test/ui/issues/issue-52060.stderr @@ -8,7 +8,7 @@ error[E0080]: evaluation of constant value failed --> $DIR/issue-52060.rs:4:26 | LL | static B: [u32; 1] = [0; A.len()]; - | ^ "constants accessing static items" needs an rfc before being allowed inside constants + | ^ constant accesses static error: aborting due to 2 previous errors