Skip to content

Commit 08ba014

Browse files
committed
make sure the miri-unleash-flag is not used to circumvent feature gates
1 parent c7eb916 commit 08ba014

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

src/librustc_mir/transform/check_consts/validation.rs

+3
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ impl Validator<'mir, 'tcx> {
254254

255255
if is_unleashable && self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
256256
self.tcx.sess.span_warn(self.tcx.def_span(self.def_id), "skipping const checks");
257+
if let Some(feature) = O::feature_gate() {
258+
self.tcx.sess.miri_unleashed_feature(feature);
259+
}
257260
return;
258261
}
259262

src/librustc_session/session.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ use rustc_errors::json::JsonEmitter;
2121
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorReported};
2222
use rustc_span::edition::Edition;
2323
use rustc_span::source_map::{self, FileLoader, MultiSpan, RealFileLoader, SourceMap, Span};
24-
use rustc_span::SourceFileHashAlgorithm;
24+
use rustc_span::{SourceFileHashAlgorithm, Symbol};
2525
use rustc_target::spec::{PanicStrategy, RelocModel, RelroLevel, Target, TargetTriple, TlsModel};
2626

2727
use std::cell::{self, RefCell};
2828
use std::env;
29+
use std::fmt::Write as _;
2930
use std::io::Write;
3031
use std::num::NonZeroU32;
3132
use std::path::PathBuf;
@@ -142,6 +143,10 @@ pub struct Session {
142143
/// and immediately printing the backtrace to stderr.
143144
pub ctfe_backtrace: Lock<CtfeBacktrace>,
144145

146+
/// This tracks whether `-Zunleash-the-miri-inside-of-you` was used to get around a
147+
/// feature gate. If yes, this file must fail to compile.
148+
miri_unleashed_features: Lock<FxHashSet<Symbol>>,
149+
145150
/// Base directory containing the `src/` for the Rust standard library, and
146151
/// potentially `rustc` as well, if we can can find it. Right now it's always
147152
/// `$sysroot/lib/rustlib/src/rust` (i.e. the `rustup` `rust-src` component).
@@ -188,7 +193,36 @@ impl From<&'static lint::Lint> for DiagnosticMessageId {
188193
}
189194
}
190195

196+
impl Drop for Session {
197+
fn drop(&mut self) {
198+
if !self.has_errors_or_delayed_span_bugs() {
199+
let unleashed_features = self.miri_unleashed_features.get_mut();
200+
if !unleashed_features.is_empty() {
201+
// Join the strings (itertools has it but libstd does not...)
202+
let mut list = String::new();
203+
for feature in unleashed_features.iter() {
204+
if !list.is_empty() {
205+
list.push_str(", ");
206+
}
207+
write!(&mut list, "{}", feature).unwrap();
208+
}
209+
// We have skipped a feature gate, and not run into other errors... reject.
210+
panic!(
211+
"`-Zunleash-the-miri-inside-of-you` may not be used to circumvent feature \
212+
gates, except when testing error paths in the CTFE engine.\n\
213+
The following feature flags are missing from this crate: {}",
214+
list,
215+
);
216+
}
217+
}
218+
}
219+
}
220+
191221
impl Session {
222+
pub fn miri_unleashed_feature(&self, s: Symbol) {
223+
self.miri_unleashed_features.lock().insert(s);
224+
}
225+
192226
pub fn local_crate_disambiguator(&self) -> CrateDisambiguator {
193227
*self.crate_disambiguator.get()
194228
}
@@ -1139,6 +1173,7 @@ pub fn build_session_with_source_map(
11391173
confused_type_with_std_module: Lock::new(Default::default()),
11401174
system_library_path: OneThread::new(RefCell::new(Default::default())),
11411175
ctfe_backtrace,
1176+
miri_unleashed_features: Lock::new(Default::default()),
11421177
real_rust_source_base_dir,
11431178
};
11441179

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// build-fail
22
// compile-flags: -Zunleash-the-miri-inside-of-you -Zdeduplicate-diagnostics
33
#![allow(const_err)]
4+
#![feature(const_raw_ptr_deref)] // FIXME: cannot remove because then rustc thinks there is no error
5+
#![crate_type = "lib"]
46

57
use std::sync::atomic::AtomicUsize;
68
use std::sync::atomic::Ordering;
@@ -24,7 +26,7 @@ static mut MUTABLE: u32 = 0;
2426
const READ_MUT: u32 = unsafe { MUTABLE };
2527
//~^ WARN skipping const checks
2628

27-
fn main() {
29+
pub fn main() {
2830
MUTATE_INTERIOR_MUT;
2931
//~^ ERROR: erroneous constant used
3032
READ_INTERIOR_MUT;

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: skipping const checks
2-
--> $DIR/const_refers_to_static.rs:11:1
2+
--> $DIR/const_refers_to_static.rs:13:1
33
|
44
LL | / const MUTATE_INTERIOR_MUT: usize = {
55
LL | |
@@ -9,7 +9,7 @@ LL | | };
99
| |__^
1010

1111
warning: skipping const checks
12-
--> $DIR/const_refers_to_static.rs:17:1
12+
--> $DIR/const_refers_to_static.rs:19:1
1313
|
1414
LL | / const READ_INTERIOR_MUT: usize = {
1515
LL | |
@@ -19,25 +19,25 @@ LL | | };
1919
| |__^
2020

2121
warning: skipping const checks
22-
--> $DIR/const_refers_to_static.rs:24:1
22+
--> $DIR/const_refers_to_static.rs:26:1
2323
|
2424
LL | const READ_MUT: u32 = unsafe { MUTABLE };
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error[E0080]: erroneous constant used
28-
--> $DIR/const_refers_to_static.rs:28:5
28+
--> $DIR/const_refers_to_static.rs:30:5
2929
|
3030
LL | MUTATE_INTERIOR_MUT;
3131
| ^^^^^^^^^^^^^^^^^^^ referenced constant has errors
3232

3333
error[E0080]: erroneous constant used
34-
--> $DIR/const_refers_to_static.rs:30:5
34+
--> $DIR/const_refers_to_static.rs:32:5
3535
|
3636
LL | READ_INTERIOR_MUT;
3737
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
3838

3939
error[E0080]: erroneous constant used
40-
--> $DIR/const_refers_to_static.rs:32:5
40+
--> $DIR/const_refers_to_static.rs:34:5
4141
|
4242
LL | READ_MUT;
4343
| ^^^^^^^^ referenced constant has errors

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

-8
This file was deleted.

src/test/ui/consts/miri_unleashed/read_from_static.rs src/test/ui/consts/read_from_static_mut_ref.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// run-pass
2-
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
#![feature(const_mut_refs)]
33
#![allow(const_err)]
44

55
static OH_YES: &mut i32 = &mut 42;
6-
//~^ WARN skipping const checks
76

87
fn main() {
98
// Make sure `OH_YES` can be read.

0 commit comments

Comments
 (0)