Don't PrintOnPanic on fatal errors

This commit is contained in:
bjorn3 2022-12-16 09:44:28 +00:00
parent 9ca82a9a3d
commit b5ac64b4cf
3 changed files with 12 additions and 10 deletions

View File

@ -29,8 +29,9 @@ pub(crate) fn codegen_and_compile_fn<'tcx>(
module: &mut dyn Module,
instance: Instance<'tcx>,
) {
let _inst_guard =
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
let _inst_guard = crate::PrintOnPanic(Some(tcx.sess), || {
format!("{:?} {}", instance, tcx.symbol_name(instance).name)
});
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
@ -48,7 +49,7 @@ pub(crate) fn codegen_fn<'tcx>(
debug_assert!(!instance.substs.needs_infer());
let mir = tcx.instance_mir(instance.def);
let _mir_guard = crate::PrintOnPanic(|| {
let _mir_guard = crate::PrintOnPanic(Some(tcx.sess), || {
let mut buf = Vec::new();
with_no_trimmed_paths!({
rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf)
@ -176,7 +177,7 @@ pub(crate) fn compile_fn(
write!(clif, " {}", isa_flag).unwrap();
}
writeln!(clif, "\n").unwrap();
crate::PrintOnPanic(move || {
crate::PrintOnPanic(None, move || {
let mut clif = clif.clone();
::cranelift_codegen::write::decorate_function(
&mut &clif_comments_clone,
@ -497,7 +498,7 @@ fn codegen_stmt<'tcx>(
#[allow(unused_variables)] cur_block: Block,
stmt: &Statement<'tcx>,
) {
let _print_guard = crate::PrintOnPanic(|| format!("stmt {:?}", stmt));
let _print_guard = crate::PrintOnPanic(Some(fx.tcx.sess), || format!("stmt {:?}", stmt));
fx.set_debug_loc(stmt.source_info);

View File

@ -23,7 +23,8 @@ fn predefine_mono_items<'tcx>(
match mono_item {
MonoItem::Fn(instance) => {
let name = tcx.symbol_name(instance).name;
let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name));
let _inst_guard =
crate::PrintOnPanic(Some(tcx.sess), || format!("{:?} {}", instance, name));
let sig =
get_function_sig(tcx, module.target_config().default_call_conv, instance);
let linkage = crate::linkage::get_clif_linkage(

View File

@ -113,11 +113,11 @@ mod prelude {
pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
}
struct PrintOnPanic<F: Fn() -> String>(F);
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
struct PrintOnPanic<'a, F: Fn() -> String>(Option<&'a Session>, F);
impl<'a, F: Fn() -> String> Drop for PrintOnPanic<'a, F> {
fn drop(&mut self) {
if ::std::thread::panicking() {
println!("{}", (self.0)());
if ::std::thread::panicking() && self.0.map_or(true, |sess| sess.has_errors().is_none()) {
println!("{}", (self.1)());
}
}
}