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
This commit is contained in:
Mazdak Farrokhzad 2020-03-11 14:03:53 +01:00 committed by GitHub
commit b1471e0a26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 14 deletions

View File

@ -5,15 +5,18 @@ use crate::mir;
use crate::mir::interpret::ConstValue; use crate::mir::interpret::ConstValue;
use crate::ty::layout::{Align, LayoutError, Size}; use crate::ty::layout::{Align, LayoutError, Size};
use crate::ty::query::TyCtxtAt; use crate::ty::query::TyCtxtAt;
use crate::ty::tls;
use crate::ty::{self, layout, Ty}; use crate::ty::{self, layout, Ty};
use backtrace::Backtrace; use backtrace::Backtrace;
use rustc_data_structures::sync::Lock;
use rustc_errors::{struct_span_err, DiagnosticBuilder}; use rustc_errors::{struct_span_err, DiagnosticBuilder};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_macros::HashStable; use rustc_macros::HashStable;
use rustc_session::CtfeBacktrace;
use rustc_span::{Pos, Span}; use rustc_span::{Pos, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use std::{any::Any, env, fmt}; use std::{any::Any, fmt};
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)] #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
pub enum ErrorHandled { pub enum ErrorHandled {
@ -257,21 +260,25 @@ impl From<ErrorHandled> for InterpErrorInfo<'_> {
impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> { impl<'tcx> From<InterpError<'tcx>> for InterpErrorInfo<'tcx> {
fn from(kind: InterpError<'tcx>) -> Self { fn from(kind: InterpError<'tcx>) -> Self {
let backtrace = match env::var("RUSTC_CTFE_BACKTRACE") { let capture_backtrace = tls::with_context_opt(|ctxt| {
// Matching `RUST_BACKTRACE` -- we treat "0" the same as "not present". if let Some(ctxt) = ctxt {
Ok(ref val) if val != "0" => { *Lock::borrow(&ctxt.tcx.sess.ctfe_backtrace)
let mut backtrace = Backtrace::new_unresolved(); } else {
CtfeBacktrace::Disabled
}
});
if val == "immediate" { let backtrace = match capture_backtrace {
CtfeBacktrace::Disabled => None,
CtfeBacktrace::Capture => Some(Box::new(Backtrace::new_unresolved())),
CtfeBacktrace::Immediate => {
// Print it now. // Print it now.
let mut backtrace = Backtrace::new_unresolved();
print_backtrace(&mut backtrace); print_backtrace(&mut backtrace);
None None
} else {
Some(Box::new(backtrace))
} }
}
_ => None,
}; };
InterpErrorInfo { kind, backtrace } InterpErrorInfo { kind, backtrace }
} }
} }

View File

@ -49,6 +49,18 @@ pub struct OptimizationFuel {
out_of_fuel: bool, out_of_fuel: bool,
} }
/// The behavior of the CTFE engine when an error occurs with regards to backtraces.
#[derive(Clone, Copy)]
pub enum CtfeBacktrace {
/// Do nothing special, return the error as usual without a backtrace.
Disabled,
/// Capture a backtrace at the point the error is created and return it in the error
/// (to be printed later if/when the error ever actually gets shown to the user).
Capture,
/// Capture a backtrace at the point the error is created and immediately print it out.
Immediate,
}
/// Represents the data associated with a compilation /// Represents the data associated with a compilation
/// session for a single crate. /// session for a single crate.
pub struct Session { pub struct Session {
@ -139,6 +151,11 @@ pub struct Session {
/// Path for libraries that will take preference over libraries shipped by Rust. /// Path for libraries that will take preference over libraries shipped by Rust.
/// Used by windows-gnu targets to priortize system mingw-w64 libraries. /// Used by windows-gnu targets to priortize system mingw-w64 libraries.
pub system_library_path: OneThread<RefCell<Option<Option<PathBuf>>>>, pub system_library_path: OneThread<RefCell<Option<Option<PathBuf>>>>,
/// Tracks the current behavior of the CTFE engine when an error occurs.
/// Options range from returning the error without a backtrace to returning an error
/// and immediately printing the backtrace to stderr.
pub ctfe_backtrace: Lock<CtfeBacktrace>,
} }
pub struct PerfStats { pub struct PerfStats {
@ -1040,6 +1057,12 @@ fn build_session_(
sopts.debugging_opts.time_passes, sopts.debugging_opts.time_passes,
); );
let ctfe_backtrace = Lock::new(match env::var("RUSTC_CTFE_BACKTRACE") {
Ok(ref val) if val == "immediate" => CtfeBacktrace::Immediate,
Ok(ref val) if val != "0" => CtfeBacktrace::Capture,
_ => CtfeBacktrace::Disabled,
});
let sess = Session { let sess = Session {
target: target_cfg, target: target_cfg,
host, host,
@ -1078,6 +1101,7 @@ fn build_session_(
trait_methods_not_found: Lock::new(Default::default()), trait_methods_not_found: Lock::new(Default::default()),
confused_type_with_std_module: Lock::new(Default::default()), confused_type_with_std_module: Lock::new(Default::default()),
system_library_path: OneThread::new(RefCell::new(Default::default())), system_library_path: OneThread::new(RefCell::new(Default::default())),
ctfe_backtrace,
}; };
validate_commandline_args_with_session_available(&sess); validate_commandline_args_with_session_available(&sess);