Skip to content

Commit b1471e0

Browse files
authored
Rollup merge of #69888 - wesleywiser:miri_exception_env_var_to_session_var, r=RalfJung
[Miri] Use a session variable instead of checking for an env var always In CTFE heavy code, checking the env var everytime is inefficient. We can do a lot better by using a `Session` variable instead. r? @RalfJung Part of #69297
2 parents e838383 + 5357f83 commit b1471e0

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/librustc/mir/interpret/error.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ use crate::mir;
55
use crate::mir::interpret::ConstValue;
66
use crate::ty::layout::{Align, LayoutError, Size};
77
use crate::ty::query::TyCtxtAt;
8+
use crate::ty::tls;
89
use crate::ty::{self, layout, Ty};
910

1011
use backtrace::Backtrace;
12+
use rustc_data_structures::sync::Lock;
1113
use rustc_errors::{struct_span_err, DiagnosticBuilder};
1214
use rustc_hir as hir;
1315
use rustc_macros::HashStable;
16+
use rustc_session::CtfeBacktrace;
1417
use rustc_span::{Pos, Span};
1518
use rustc_target::spec::abi::Abi;
16-
use std::{any::Any, env, fmt};
19+
use std::{any::Any, fmt};
1720

1821
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
1922
pub enum ErrorHandled {
@@ -257,21 +260,25 @@ impl From<ErrorHandled> for InterpErrorInfo<'_> {
257260

258261
impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
259262
fn from(kind: InterpError<'tcx>) -> Self {
260-
let backtrace = match env::var("RUSTC_CTFE_BACKTRACE") {
261-
// Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present".
262-
Ok(ref val) if val != "0" => {
263-
let mut backtrace = Backtrace::new_unresolved();
263+
let capture_backtrace = tls::with_context_opt(|ctxt| {
264+
if let Some(ctxt) = ctxt {
265+
*Lock::borrow(&ctxt.tcx.sess.ctfe_backtrace)
266+
} else {
267+
CtfeBacktrace::Disabled
268+
}
269+
});
264270

265-
if val == "immediate" {
266-
// Print it now.
267-
print_backtrace(&mut backtrace);
268-
None
269-
} else {
270-
Some(Box::new(backtrace))
271-
}
271+
let backtrace = match capture_backtrace {
272+
CtfeBacktrace::Disabled => None,
273+
CtfeBacktrace::Capture => Some(Box::new(Backtrace::new_unresolved())),
274+
CtfeBacktrace::Immediate => {
275+
// Print it now.
276+
let mut backtrace = Backtrace::new_unresolved();
277+
print_backtrace(&mut backtrace);
278+
None
272279
}
273-
_ => None,
274280
};
281+
275282
InterpErrorInfo { kind, backtrace }
276283
}
277284
}

src/librustc_session/session.rs

+24
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ pub struct OptimizationFuel {
4949
out_of_fuel: bool,
5050
}
5151

52+
/// The behavior of the CTFE engine when an error occurs with regards to backtraces.
53+
#[derive(Clone, Copy)]
54+
pub enum CtfeBacktrace {
55+
/// Do nothing special, return the error as usual without a backtrace.
56+
Disabled,
57+
/// Capture a backtrace at the point the error is created and return it in the error
58+
/// (to be printed later if/when the error ever actually gets shown to the user).
59+
Capture,
60+
/// Capture a backtrace at the point the error is created and immediately print it out.
61+
Immediate,
62+
}
63+
5264
/// Represents the data associated with a compilation
5365
/// session for a single crate.
5466
pub struct Session {
@@ -139,6 +151,11 @@ pub struct Session {
139151
/// Path for libraries that will take preference over libraries shipped by Rust.
140152
/// Used by windows-gnu targets to priortize system mingw-w64 libraries.
141153
pub system_library_path: OneThread<RefCell<Option<Option<PathBuf>>>>,
154+
155+
/// Tracks the current behavior of the CTFE engine when an error occurs.
156+
/// Options range from returning the error without a backtrace to returning an error
157+
/// and immediately printing the backtrace to stderr.
158+
pub ctfe_backtrace: Lock<CtfeBacktrace>,
142159
}
143160

144161
pub struct PerfStats {
@@ -1040,6 +1057,12 @@ fn build_session_(
10401057
sopts.debugging_opts.time_passes,
10411058
);
10421059

1060+
let ctfe_backtrace = Lock::new(match env::var("RUSTC_CTFE_BACKTRACE") {
1061+
Ok(ref val) if val == "immediate" => CtfeBacktrace::Immediate,
1062+
Ok(ref val) if val != "0" => CtfeBacktrace::Capture,
1063+
_ => CtfeBacktrace::Disabled,
1064+
});
1065+
10431066
let sess = Session {
10441067
target: target_cfg,
10451068
host,
@@ -1078,6 +1101,7 @@ fn build_session_(
10781101
trait_methods_not_found: Lock::new(Default::default()),
10791102
confused_type_with_std_module: Lock::new(Default::default()),
10801103
system_library_path: OneThread::new(RefCell::new(Default::default())),
1104+
ctfe_backtrace,
10811105
};
10821106

10831107
validate_commandline_args_with_session_available(&sess);

0 commit comments

Comments
 (0)