diff --git a/compiler/rustc_incremental/src/diagnostics.rs b/compiler/rustc_incremental/src/diagnostics.rs index a1f4464c76592..6e291b7ea3abb 100644 --- a/compiler/rustc_incremental/src/diagnostics.rs +++ b/compiler/rustc_incremental/src/diagnostics.rs @@ -184,13 +184,6 @@ pub(crate) struct DeletePartial<'a> { pub err: std::io::Error, } -#[derive(Diagnostic)] -#[diag("error deleting incremental compilation session directory `{$path}`: {$err}")] -pub(crate) struct DeleteFull<'a> { - pub path: &'a Path, - pub err: std::io::Error, -} - #[derive(Diagnostic)] #[diag("did not finalize incremental compilation session directory `{$path}`: {$err}")] #[help("the next build will not be able to reuse work from this compilation")] diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index 562481e51188b..c40aa49c29d11 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -294,9 +294,10 @@ pub(crate) fn prepare_session_directory( /// This function finalizes and thus 'publishes' the session directory by /// renaming it to `s-{timestamp}-{svh}` and releasing the file lock. -/// If there have been compilation errors, however, this function will just -/// delete the presumably invalid session directory. +/// This must not be called if there have been any compilation errors. pub fn finalize_session_directory(sess: &Session, svh: Option) { + assert!(sess.dcx().has_errors_or_delayed_bugs().is_none()); + if sess.opts.incremental.is_none() { return; } @@ -307,24 +308,6 @@ pub fn finalize_session_directory(sess: &Session, svh: Option) { let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone(); - if sess.dcx().has_errors_or_delayed_bugs().is_some() { - // If there have been any errors during compilation, we don't want to - // publish this session directory. Rather, we'll just delete it. - - debug!( - "finalize_session_directory() - invalidating session directory: {}", - incr_comp_session_dir.display() - ); - - if let Err(err) = std_fs::remove_dir_all(&*incr_comp_session_dir) { - sess.dcx().emit_warn(diagnostics::DeleteFull { path: &incr_comp_session_dir, err }); - } - - let lock_file_path = lock_file_path(&*incr_comp_session_dir); - delete_session_dir_lock_file(sess, &lock_file_path); - sess.mark_incr_comp_session_as_invalid(); - } - debug!("finalize_session_directory() - session directory: {}", incr_comp_session_dir.display()); let mut sub_dir_name = incr_comp_session_dir @@ -350,21 +333,19 @@ pub fn finalize_session_directory(sess: &Session, svh: Option) { match rename_path_with_retry(&*incr_comp_session_dir, &new_path, 3) { Ok(_) => { debug!("finalize_session_directory() - directory renamed successfully"); - - // This unlocks the directory - sess.finalize_incr_comp_session(new_path); } Err(e) => { // Warn about the error. However, no need to abort compilation now. sess.dcx().emit_note(diagnostics::Finalize { path: &incr_comp_session_dir, err: e }); - debug!("finalize_session_directory() - error, marking as invalid"); - // Drop the file lock, so we can garage collect - sess.mark_incr_comp_session_as_invalid(); + debug!("finalize_session_directory() - error"); } } - let _ = garbage_collect_session_directories(sess); + // This unlocks the directory + sess.finalize_incr_comp_session(); + + let _ = garbage_collect_session_directories(sess, &new_path); } pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> { @@ -602,10 +583,12 @@ fn is_old_enough_to_be_collected(timestamp: SystemTime) -> bool { } /// Runs garbage collection for the current session. -pub(crate) fn garbage_collect_session_directories(sess: &Session) -> io::Result<()> { +pub(crate) fn garbage_collect_session_directories( + sess: &Session, + session_directory: &Path, +) -> io::Result<()> { debug!("garbage_collect_session_directories() - begin"); - let session_directory = sess.incr_comp_session_dir(); debug!( "garbage_collect_session_directories() - session directory: {}", session_directory.display() diff --git a/compiler/rustc_incremental/src/persist/load.rs b/compiler/rustc_incremental/src/persist/load.rs index c3b9f433417a6..352ee59aaa0d4 100644 --- a/compiler/rustc_incremental/src/persist/load.rs +++ b/compiler/rustc_incremental/src/persist/load.rs @@ -192,7 +192,7 @@ pub fn setup_dep_graph( let load_result = load_dep_graph(sess); sess.time("incr_comp_garbage_collect_session_directories", || { - if let Err(e) = garbage_collect_session_directories(sess) { + if let Err(e) = garbage_collect_session_directories(sess, &sess.incr_comp_session_dir()) { warn!( "Error while trying to garbage collect incremental compilation \ cache directory: {e}", diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 025a5a55d0abd..24e033bdee088 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -99,7 +99,9 @@ impl Linker { work_products.insert(id, product); } - sess.dcx().abort_if_errors(); + if let Some(guar) = sess.dcx().has_errors_or_delayed_bugs() { + guar.raise_fatal(); + } let _timer = sess.timer("link"); diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 986dababf770a..ba5512566e8e2 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -459,7 +459,7 @@ impl Session { IncrCompSession::Active { session_directory: session_dir, _lock_file: lock_file }; } - pub fn finalize_incr_comp_session(&self, new_directory_path: PathBuf) { + pub fn finalize_incr_comp_session(&self) { let mut incr_comp_session = self.incr_comp_session.borrow_mut(); if let IncrCompSession::Active { .. } = *incr_comp_session { @@ -468,34 +468,17 @@ impl Session { } // Note: this will also drop the lock file, thus unlocking the directory. - *incr_comp_session = IncrCompSession::Finalized { session_directory: new_directory_path }; - } - - pub fn mark_incr_comp_session_as_invalid(&self) { - let mut incr_comp_session = self.incr_comp_session.borrow_mut(); - - let session_directory = match *incr_comp_session { - IncrCompSession::Active { ref session_directory, .. } => session_directory.clone(), - IncrCompSession::InvalidBecauseOfErrors { .. } => return, - _ => panic!("trying to invalidate `IncrCompSession` `{:?}`", *incr_comp_session), - }; - - // Note: this will also drop the lock file, thus unlocking the directory. - *incr_comp_session = IncrCompSession::InvalidBecauseOfErrors { session_directory }; + *incr_comp_session = IncrCompSession::FinalizedOrRemoved; } pub fn incr_comp_session_dir(&self) -> MappedReadGuard<'_, PathBuf> { let incr_comp_session = self.incr_comp_session.borrow(); - ReadGuard::map(incr_comp_session, |incr_comp_session| match *incr_comp_session { - IncrCompSession::NotInitialized => panic!( + ReadGuard::map(incr_comp_session, |incr_comp_session| match incr_comp_session { + IncrCompSession::NotInitialized | IncrCompSession::FinalizedOrRemoved => panic!( "trying to get session directory from `IncrCompSession`: {:?}", - *incr_comp_session, + incr_comp_session, ), - IncrCompSession::Active { ref session_directory, .. } - | IncrCompSession::Finalized { ref session_directory } - | IncrCompSession::InvalidBecauseOfErrors { ref session_directory } => { - session_directory - } + IncrCompSession::Active { session_directory, .. } => session_directory, }) } @@ -1424,13 +1407,10 @@ enum IncrCompSession { /// alone has an effect, because the file will unlock when the session is /// dropped. Active { session_directory: PathBuf, _lock_file: flock::Lock }, - /// This is the state after the session directory has been finalized. In this - /// state, the contents of the directory must not be modified any more. - Finalized { session_directory: PathBuf }, - /// This is an error state that is reached when some compilation error has - /// occurred. It indicates that the contents of the session directory must - /// not be used, since they might be invalid. - InvalidBecauseOfErrors { session_directory: PathBuf }, + /// This is the state after the session directory has been finalized or + /// removed after errors. In this state, the contents of the directory must + /// not be modified any more. + FinalizedOrRemoved, } /// A wrapper around an [`DiagCtxt`] that is used for early error emissions.