Skip to content

Commit b5ac64b

Browse files
committed
Don't PrintOnPanic on fatal errors
1 parent 9ca82a9 commit b5ac64b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/base.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ pub(crate) fn codegen_and_compile_fn<'tcx>(
2929
module: &mut dyn Module,
3030
instance: Instance<'tcx>,
3131
) {
32-
let _inst_guard =
33-
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
32+
let _inst_guard = crate::PrintOnPanic(Some(tcx.sess), || {
33+
format!("{:?} {}", instance, tcx.symbol_name(instance).name)
34+
});
3435

3536
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
3637
let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance);
@@ -48,7 +49,7 @@ pub(crate) fn codegen_fn<'tcx>(
4849
debug_assert!(!instance.substs.needs_infer());
4950

5051
let mir = tcx.instance_mir(instance.def);
51-
let _mir_guard = crate::PrintOnPanic(|| {
52+
let _mir_guard = crate::PrintOnPanic(Some(tcx.sess), || {
5253
let mut buf = Vec::new();
5354
with_no_trimmed_paths!({
5455
rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf)
@@ -176,7 +177,7 @@ pub(crate) fn compile_fn(
176177
write!(clif, " {}", isa_flag).unwrap();
177178
}
178179
writeln!(clif, "\n").unwrap();
179-
crate::PrintOnPanic(move || {
180+
crate::PrintOnPanic(None, move || {
180181
let mut clif = clif.clone();
181182
::cranelift_codegen::write::decorate_function(
182183
&mut &clif_comments_clone,
@@ -497,7 +498,7 @@ fn codegen_stmt<'tcx>(
497498
#[allow(unused_variables)] cur_block: Block,
498499
stmt: &Statement<'tcx>,
499500
) {
500-
let _print_guard = crate::PrintOnPanic(|| format!("stmt {:?}", stmt));
501+
let _print_guard = crate::PrintOnPanic(Some(fx.tcx.sess), || format!("stmt {:?}", stmt));
501502

502503
fx.set_debug_loc(stmt.source_info);
503504

src/driver/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ fn predefine_mono_items<'tcx>(
2323
match mono_item {
2424
MonoItem::Fn(instance) => {
2525
let name = tcx.symbol_name(instance).name;
26-
let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name));
26+
let _inst_guard =
27+
crate::PrintOnPanic(Some(tcx.sess), || format!("{:?} {}", instance, name));
2728
let sig =
2829
get_function_sig(tcx, module.target_config().default_call_conv, instance);
2930
let linkage = crate::linkage::get_clif_linkage(

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ mod prelude {
113113
pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
114114
}
115115

116-
struct PrintOnPanic<F: Fn() -> String>(F);
117-
impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
116+
struct PrintOnPanic<'a, F: Fn() -> String>(Option<&'a Session>, F);
117+
impl<'a, F: Fn() -> String> Drop for PrintOnPanic<'a, F> {
118118
fn drop(&mut self) {
119-
if ::std::thread::panicking() {
120-
println!("{}", (self.0)());
119+
if ::std::thread::panicking() && self.0.map_or(true, |sess| sess.has_errors().is_none()) {
120+
println!("{}", (self.1)());
121121
}
122122
}
123123
}

0 commit comments

Comments
 (0)