Skip to content

Commit 7292d5f

Browse files
Rollup merge of #82105 - nagisa:nagisa/ensure-removed, r=petrochenkov
Don't fail to remove files if they are missing In the backend we may want to remove certain temporary files, but in certain other situations these files might not be produced in the first place. We don't exactly care about that, and the intent is really that these files are gone after a certain point in the backend. Here we unify the backend file removing calls to use `ensure_removed` which will attempt to delete a file, but will not fail if it does not exist (anymore). The tradeoff to this approach is, of course, that we may miss instances were we are attempting to remove files at wrong paths due to some bug – compilation would silently succeed but the temporary files would remain there somewhere.
2 parents 253631d + fa3621b commit 7292d5f

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::llvm_util;
1111
use crate::type_::Type;
1212
use crate::LlvmCodegenBackend;
1313
use crate::ModuleLlvm;
14+
use rustc_codegen_ssa::back::link::ensure_removed;
1415
use rustc_codegen_ssa::back::write::{
1516
BitcodeSection, CodegenContext, EmitObj, ModuleConfig, TargetMachineFactoryConfig,
1617
TargetMachineFactoryFn,
@@ -879,9 +880,7 @@ pub(crate) unsafe fn codegen(
879880

880881
if !config.emit_bc {
881882
debug!("removing_bitcode {:?}", bc_out);
882-
if let Err(e) = fs::remove_file(&bc_out) {
883-
diag_handler.err(&format!("failed to remove bitcode: {}", e));
884-
}
883+
ensure_removed(diag_handler, &bc_out);
885884
}
886885
}
887886

compiler/rustc_codegen_ssa/src/back/link.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_data_structures::fx::FxHashSet;
22
use rustc_data_structures::temp_dir::MaybeTempDir;
3+
use rustc_errors::Handler;
34
use rustc_fs_util::fix_windows_verbatim_for_gcc;
45
use rustc_hir::def_id::CrateNum;
56
use rustc_middle::middle::cstore::{EncodedMetadata, LibSource};
@@ -34,9 +35,11 @@ use std::path::{Path, PathBuf};
3435
use std::process::{ExitStatus, Output, Stdio};
3536
use std::{ascii, char, env, fmt, fs, io, mem, str};
3637

37-
pub fn remove(sess: &Session, path: &Path) {
38+
pub fn ensure_removed(diag_handler: &Handler, path: &Path) {
3839
if let Err(e) = fs::remove_file(path) {
39-
sess.err(&format!("failed to remove {}: {}", path.display(), e));
40+
if e.kind() != io::ErrorKind::NotFound {
41+
diag_handler.err(&format!("failed to remove {}: {}", path.display(), e));
42+
}
4043
}
4144
}
4245

@@ -112,11 +115,11 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
112115
if !sess.opts.cg.save_temps {
113116
let remove_temps_from_module = |module: &CompiledModule| {
114117
if let Some(ref obj) = module.object {
115-
remove(sess, obj);
118+
ensure_removed(sess.diagnostic(), obj);
116119
}
117120

118121
if let Some(ref obj) = module.dwarf_object {
119-
remove(sess, obj);
122+
ensure_removed(sess.diagnostic(), obj);
120123
}
121124
};
122125

compiler/rustc_codegen_ssa/src/back/write.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::link::{self, remove};
1+
use super::link::{self, ensure_removed};
22
use super::linker::LinkerInfo;
33
use super::lto::{self, SerializedModule};
44
use super::symbol_export::symbol_name_for_instance_in_crate;
@@ -543,7 +543,7 @@ fn produce_final_output_artifacts(
543543
copy_gracefully(&path, &crate_output.path(output_type));
544544
if !sess.opts.cg.save_temps && !keep_numbered {
545545
// The user just wants `foo.x`, not `foo.#module-name#.x`.
546-
remove(sess, &path);
546+
ensure_removed(sess.diagnostic(), &path);
547547
}
548548
} else {
549549
let ext = crate_output
@@ -642,33 +642,33 @@ fn produce_final_output_artifacts(
642642
for module in compiled_modules.modules.iter() {
643643
if let Some(ref path) = module.object {
644644
if !keep_numbered_objects {
645-
remove(sess, path);
645+
ensure_removed(sess.diagnostic(), path);
646646
}
647647
}
648648

649649
if let Some(ref path) = module.dwarf_object {
650650
if !keep_numbered_objects {
651-
remove(sess, path);
651+
ensure_removed(sess.diagnostic(), path);
652652
}
653653
}
654654

655655
if let Some(ref path) = module.bytecode {
656656
if !keep_numbered_bitcode {
657-
remove(sess, path);
657+
ensure_removed(sess.diagnostic(), path);
658658
}
659659
}
660660
}
661661

662662
if !user_wants_bitcode {
663663
if let Some(ref metadata_module) = compiled_modules.metadata_module {
664664
if let Some(ref path) = metadata_module.bytecode {
665-
remove(sess, &path);
665+
ensure_removed(sess.diagnostic(), &path);
666666
}
667667
}
668668

669669
if let Some(ref allocator_module) = compiled_modules.allocator_module {
670670
if let Some(ref path) = allocator_module.bytecode {
671-
remove(sess, path);
671+
ensure_removed(sess.diagnostic(), path);
672672
}
673673
}
674674
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// build-pass
2+
//
3+
// compile-flags: -g --emit=llvm-ir -Zunstable-options -Csplit-debuginfo=unpacked
4+
//
5+
// Make sure that we don't explode with an error if we don't actually end up emitting any `dwo`s,
6+
// as would be the case if we don't actually codegen anything.
7+
#![crate_type="rlib"]

0 commit comments

Comments
 (0)