Skip to content

Commit 2c7e3b5

Browse files
committed
WIP
1 parent fc23a81 commit 2c7e3b5

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/librustc/mir/interpret/error.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ use crate::hir::map::definitions::DefPathData;
44
use crate::mir;
55
use crate::ty::layout::{Align, LayoutError, Size};
66
use crate::ty::query::TyCtxtAt;
7+
use crate::ty::tls;
78
use crate::ty::{self, layout, Ty};
89

910
use backtrace::Backtrace;
1011
use hir::GeneratorKind;
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::symbol::Symbol;
1518
use rustc_span::{Pos, Span};
1619
use rustc_target::spec::abi::Abi;
17-
use std::{any::Any, env, fmt};
20+
use std::{any::Any, fmt};
1821

1922
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
2023
pub enum ErrorHandled {
@@ -241,21 +244,26 @@ impl From<ErrorHandled> for InterpErrorInfo<'tcx> {
241244

242245
impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
243246
fn from(kind: InterpError<'tcx>) -> Self {
244-
let backtrace = match env::var("RUSTC_CTFE_BACKTRACE") {
245-
// Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present".
246-
Ok(ref val) if val != "0" => {
247-
let mut backtrace = Backtrace::new_unresolved();
247+
let capture_backtrace = tls::with_context_opt(|ctxt| {
248+
if let Some(ctxt) = ctxt {
249+
let l = Lock::borrow(&ctxt.tcx.sess.ctfe_backtrace);
250+
*l
251+
} else {
252+
CtfeBacktrace::Disabled
253+
}
254+
});
248255

249-
if val == "immediate" {
250-
// Print it now.
251-
print_backtrace(&mut backtrace);
252-
None
253-
} else {
254-
Some(Box::new(backtrace))
255-
}
256+
let backtrace = match capture_backtrace {
257+
CtfeBacktrace::Disabled => None,
258+
CtfeBacktrace::Capture => Some(Box::new(Backtrace::new_unresolved())),
259+
CtfeBacktrace::Immediate => {
260+
// Print it now.
261+
let mut backtrace = Backtrace::new_unresolved();
262+
print_backtrace(&mut backtrace);
263+
None
256264
}
257-
_ => None,
258265
};
266+
259267
InterpErrorInfo { kind, backtrace }
260268
}
261269
}

src/librustc_session/session.rs

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

52+
#[derive(Clone, Copy)]
53+
pub enum CtfeBacktrace {
54+
Disabled,
55+
Capture,
56+
Immediate,
57+
}
58+
5259
/// Represents the data associated with a compilation
5360
/// session for a single crate.
5461
pub struct Session {
@@ -136,6 +143,8 @@ pub struct Session {
136143
/// Path for libraries that will take preference over libraries shipped by Rust.
137144
/// Used by windows-gnu targets to priortize system mingw-w64 libraries.
138145
pub system_library_path: OneThread<RefCell<Option<Option<PathBuf>>>>,
146+
147+
pub ctfe_backtrace: Lock<CtfeBacktrace>,
139148
}
140149

141150
pub struct PerfStats {
@@ -1036,6 +1045,12 @@ fn build_session_(
10361045
sopts.debugging_opts.time_passes,
10371046
);
10381047

1048+
let ctfe_backtrace = Lock::new(match env::var("RUSTC_CTFE_BACKTRACE") {
1049+
Ok(ref val) if val == "immediate" => CtfeBacktrace::Immediate,
1050+
Ok(ref val) if val != "0" => CtfeBacktrace::Capture,
1051+
_ => CtfeBacktrace::Disabled,
1052+
});
1053+
10391054
let sess = Session {
10401055
target: target_cfg,
10411056
host,
@@ -1073,6 +1088,7 @@ fn build_session_(
10731088
trait_methods_not_found: Lock::new(Default::default()),
10741089
confused_type_with_std_module: Lock::new(Default::default()),
10751090
system_library_path: OneThread::new(RefCell::new(Default::default())),
1091+
ctfe_backtrace,
10761092
};
10771093

10781094
validate_commandline_args_with_session_available(&sess);

0 commit comments

Comments
 (0)