Skip to content

Commit a67fa85

Browse files
committed
Emit a warning when Miri is used with optimizations
1 parent d332c06 commit a67fa85

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

ci.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function run_tests {
4343
# optimizations up all the way, too).
4444
# Optimizations change diagnostics (mostly backtraces), so we don't check
4545
# them. Also error locations change so we don't run the failing tests.
46-
MIRIFLAGS="${MIRIFLAGS:-} -O -Zmir-opt-level=4" MIRI_SKIP_UI_CHECKS=1 ./miri test -- tests/{pass,panic}
46+
# We explicitly enable debug-assertions here, they are disabled by -O but we have tests
47+
# which exist to check that we panic on debug assertion failures.
48+
MIRIFLAGS="${MIRIFLAGS:-} -O -Zmir-opt-level=4 -Cdebug-assertions=yes" MIRI_SKIP_UI_CHECKS=1 ./miri test -- tests/{pass,panic}
4749

4850
# Also run some many-seeds tests. 64 seeds means this takes around a minute per test.
4951
for FILE in tests/many-seeds/*.rs; do

src/bin/miri.rs

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use rustc_middle::{
3030
},
3131
ty::{query::ExternProviders, TyCtxt},
3232
};
33+
use rustc_session::config::OptLevel;
34+
3335
use rustc_session::{config::CrateType, search_paths::PathKind, CtfeBacktrace};
3436

3537
use miri::{BacktraceStyle, BorrowTrackerMethod, ProvenanceMode, RetagFields};
@@ -82,6 +84,21 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
8284
env::set_current_dir(cwd).unwrap();
8385
}
8486

87+
if tcx.sess.opts.optimize != OptLevel::No {
88+
tcx.sess.warn("Miri does not support optimizations. If you have enabled optimizations \
89+
by selecting a Cargo profile (such as --release) which changes other profile settings \
90+
such as whether debug assertions and overflow checks are enabled, those settings are \
91+
still applied.");
92+
}
93+
if tcx.sess.mir_opt_level() > 0 {
94+
tcx.sess.warn("You have explicitly enabled MIR optimizations, overriding Miri's default \
95+
which is to completely disable them. Any optimizations may hide UB that Miri would \
96+
otherwise detect, and it is not necessarily possible to predict what kind of UB will \
97+
be missed. If you are enabling optimizations to make Miri run faster, we advise using \
98+
cfg(miri) to shrink your workload instead. The impact of enabling MIR optimizations is \
99+
usually marginal at best.");
100+
}
101+
85102
if let Some(return_code) = miri::eval_entry(tcx, entry_def_id, entry_type, config) {
86103
std::process::exit(
87104
i32::try_from(return_code).expect("Return value was too large!"),

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,5 @@ pub const MIRI_DEFAULT_ARGS: &[&str] = &[
130130
"-Zmir-emit-retag",
131131
"-Zmir-opt-level=0",
132132
"--cfg=miri",
133-
"-Cdebug-assertions=on",
134133
"-Zextra-const-ub-checks",
135134
];

0 commit comments

Comments
 (0)