Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions compiler/rustc_incremental/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
41 changes: 12 additions & 29 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Svh>) {
assert!(sess.dcx().has_errors_or_delayed_bugs().is_none());

if sess.opts.incremental.is_none() {
return;
}
Expand All @@ -307,24 +308,6 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {

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
Expand All @@ -350,21 +333,19 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
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);
Comment thread
adwinwhite marked this conversation as resolved.
}

pub(crate) fn delete_all_session_dir_contents(sess: &Session) -> io::Result<()> {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
40 changes: 10 additions & 30 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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.
Expand Down
Loading