Skip to content

Commit 2022fac

Browse files
committed
Constants reading or referencing statics is illegal
and some uses of it will be illegal forever (e.g. mutable or interior mutable statics)
1 parent ad6b9c7 commit 2022fac

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

src/librustc_mir/const_eval.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
182182
#[derive(Clone, Debug)]
183183
pub enum ConstEvalError {
184184
NeedsRfc(String),
185+
ConstAccessesStatic,
185186
}
186187

187188
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalError {
@@ -201,6 +202,7 @@ impl fmt::Display for ConstEvalError {
201202
msg
202203
)
203204
}
205+
ConstAccessesStatic => write!(f, "constant accesses static"),
204206
}
205207
}
206208
}
@@ -210,6 +212,7 @@ impl Error for ConstEvalError {
210212
use self::ConstEvalError::*;
211213
match *self {
212214
NeedsRfc(_) => "this feature needs an rfc before being allowed inside constants",
215+
ConstAccessesStatic => "constant accesses static",
213216
}
214217
}
215218

@@ -547,9 +550,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
547550
if memory_extra.can_access_statics {
548551
Ok(())
549552
} else {
550-
Err(ConstEvalError::NeedsRfc(
551-
"constants accessing static items".to_string(),
552-
).into())
553+
Err(ConstEvalError::ConstAccessesStatic.into())
553554
}
554555
}
555556
}

src/test/ui/consts/const-points-to-static.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0080]: it is undefined behavior to use this value
88
--> $DIR/const-points-to-static.rs:5:1
99
|
1010
LL | const TEST: &u8 = &MY_STATIC;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "constants accessing static items" needs an rfc before being allowed inside constants
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static
1212
|
1313
= 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.
1414

src/test/ui/consts/const-prop-read-static-in-const.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: any use of this value will cause an error
1010
LL | const TEST: u8 = MY_STATIC;
1111
| -----------------^^^^^^^^^-
1212
| |
13-
| "constants accessing static items" needs an rfc before being allowed inside constants
13+
| constant accesses static
1414
|
1515
= note: `#[deny(const_err)]` on by default
1616

src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
4242
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
4343
LL | |
4444
LL | | };
45-
| |__^ "constants accessing static items" needs an rfc before being allowed inside constants
45+
| |__^ constant accesses static
4646
|
4747
= 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.
4848

@@ -54,7 +54,7 @@ LL | | static FOO: usize = 0;
5454
LL | | &FOO
5555
LL | |
5656
LL | | };
57-
| |__^ "constants accessing static items" needs an rfc before being allowed inside constants
57+
| |__^ constant accesses static
5858
|
5959
= 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.
6060

src/test/ui/issues/issue-52060.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0080]: evaluation of constant value failed
88
--> $DIR/issue-52060.rs:4:26
99
|
1010
LL | static B: [u32; 1] = [0; A.len()];
11-
| ^ "constants accessing static items" needs an rfc before being allowed inside constants
11+
| ^ constant accesses static
1212

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)