diff --git a/src/base.rs b/src/base.rs index 89d955e8bf2..7df11cf4aed 100644 --- a/src/base.rs +++ b/src/base.rs @@ -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); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 6e925cea277..1ab4fdf6155 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -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( diff --git a/src/lib.rs b/src/lib.rs index 629d79d5012..49b5dc32144 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,11 +113,11 @@ mod prelude { pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue}; } -struct PrintOnPanic String>(F); -impl String> Drop for PrintOnPanic { +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)()); } } }