Constants reading or referencing statics is illegal

and some uses of it will be illegal forever
(e.g. mutable or interior mutable statics)
This commit is contained in:
Oliver Scherer 2019-12-22 21:40:00 +01:00
parent ad6b9c79d6
commit 2022fac4df
5 changed files with 9 additions and 8 deletions

View File

@ -182,6 +182,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
#[derive(Clone, Debug)]
pub enum ConstEvalError {
NeedsRfc(String),
ConstAccessesStatic,
}
impl<'tcx> Into<InterpErrorInfo<'tcx>> 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())
}
}
}

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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