From fa3621b468263828b5e1a1b1563e0b9cb7209e96 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sun, 14 Feb 2021 18:27:08 +0200 Subject: [PATCH] Don't fail to remove files if they are missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- compiler/rustc_codegen_llvm/src/back/write.rs | 5 ++--- compiler/rustc_codegen_ssa/src/back/link.rs | 11 +++++++---- compiler/rustc_codegen_ssa/src/back/write.rs | 14 +++++++------- .../debuginfo-emit-llvm-ir-and-split-debuginfo.rs | 7 +++++++ 4 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 src/test/ui/debuginfo-emit-llvm-ir-and-split-debuginfo.rs diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 8b737c9a2e557..5f8a11ab94e66 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -11,6 +11,7 @@ use crate::llvm_util; use crate::type_::Type; use crate::LlvmCodegenBackend; use crate::ModuleLlvm; +use rustc_codegen_ssa::back::link::ensure_removed; use rustc_codegen_ssa::back::write::{ BitcodeSection, CodegenContext, EmitObj, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn, @@ -879,9 +880,7 @@ pub(crate) unsafe fn codegen( if !config.emit_bc { debug!("removing_bitcode {:?}", bc_out); - if let Err(e) = fs::remove_file(&bc_out) { - diag_handler.err(&format!("failed to remove bitcode: {}", e)); - } + ensure_removed(diag_handler, &bc_out); } } diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 6c58417590e69..972b9bbfe1caf 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1,5 +1,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::temp_dir::MaybeTempDir; +use rustc_errors::Handler; use rustc_fs_util::fix_windows_verbatim_for_gcc; use rustc_hir::def_id::CrateNum; use rustc_middle::middle::cstore::{EncodedMetadata, LibSource}; @@ -34,9 +35,11 @@ use std::path::{Path, PathBuf}; use std::process::{ExitStatus, Output, Stdio}; use std::{ascii, char, env, fmt, fs, io, mem, str}; -pub fn remove(sess: &Session, path: &Path) { +pub fn ensure_removed(diag_handler: &Handler, path: &Path) { if let Err(e) = fs::remove_file(path) { - sess.err(&format!("failed to remove {}: {}", path.display(), e)); + if e.kind() != io::ErrorKind::NotFound { + diag_handler.err(&format!("failed to remove {}: {}", path.display(), e)); + } } } @@ -112,11 +115,11 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>( if !sess.opts.cg.save_temps { let remove_temps_from_module = |module: &CompiledModule| { if let Some(ref obj) = module.object { - remove(sess, obj); + ensure_removed(sess.diagnostic(), obj); } if let Some(ref obj) = module.dwarf_object { - remove(sess, obj); + ensure_removed(sess.diagnostic(), obj); } }; diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 6aef5cb535a1f..b0aed81246007 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1,4 +1,4 @@ -use super::link::{self, remove}; +use super::link::{self, ensure_removed}; use super::linker::LinkerInfo; use super::lto::{self, SerializedModule}; use super::symbol_export::symbol_name_for_instance_in_crate; @@ -543,7 +543,7 @@ fn produce_final_output_artifacts( copy_gracefully(&path, &crate_output.path(output_type)); if !sess.opts.cg.save_temps && !keep_numbered { // The user just wants `foo.x`, not `foo.#module-name#.x`. - remove(sess, &path); + ensure_removed(sess.diagnostic(), &path); } } else { let ext = crate_output @@ -642,19 +642,19 @@ fn produce_final_output_artifacts( for module in compiled_modules.modules.iter() { if let Some(ref path) = module.object { if !keep_numbered_objects { - remove(sess, path); + ensure_removed(sess.diagnostic(), path); } } if let Some(ref path) = module.dwarf_object { if !keep_numbered_objects { - remove(sess, path); + ensure_removed(sess.diagnostic(), path); } } if let Some(ref path) = module.bytecode { if !keep_numbered_bitcode { - remove(sess, path); + ensure_removed(sess.diagnostic(), path); } } } @@ -662,13 +662,13 @@ fn produce_final_output_artifacts( if !user_wants_bitcode { if let Some(ref metadata_module) = compiled_modules.metadata_module { if let Some(ref path) = metadata_module.bytecode { - remove(sess, &path); + ensure_removed(sess.diagnostic(), &path); } } if let Some(ref allocator_module) = compiled_modules.allocator_module { if let Some(ref path) = allocator_module.bytecode { - remove(sess, path); + ensure_removed(sess.diagnostic(), path); } } } diff --git a/src/test/ui/debuginfo-emit-llvm-ir-and-split-debuginfo.rs b/src/test/ui/debuginfo-emit-llvm-ir-and-split-debuginfo.rs new file mode 100644 index 0000000000000..043011b3316a7 --- /dev/null +++ b/src/test/ui/debuginfo-emit-llvm-ir-and-split-debuginfo.rs @@ -0,0 +1,7 @@ +// build-pass +// +// compile-flags: -g --emit=llvm-ir -Zunstable-options -Csplit-debuginfo=unpacked +// +// Make sure that we don't explode with an error if we don't actually end up emitting any `dwo`s, +// as would be the case if we don't actually codegen anything. +#![crate_type="rlib"]