Skip to content

Commit

Permalink
Move Option<DirtyReason> into Freshness
Browse files Browse the repository at this point in the history
  • Loading branch information
dnbln committed Nov 26, 2022
1 parent e08848c commit 00b81c2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::job::{Freshness, Job, Work};
use super::job::{Job, Work};
use super::{fingerprint, Context, LinkType, Unit};
use crate::core::compiler::artifact;
use crate::core::compiler::context::Metadata;
Expand Down Expand Up @@ -488,7 +488,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
} else {
fingerprint::prepare_target(cx, unit, false)?
};
if job.freshness() == Freshness::Dirty {
if job.freshness().is_dirty() {
job.before(dirty);
} else {
job.before(fresh);
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/compiler/fingerprint/dirty_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::Shell;
use crate::Config;
use std::fmt;

#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum DirtyReason {
RustcChanged,
FeaturesChanged {
Expand Down Expand Up @@ -84,6 +84,7 @@ impl DirtyReason {
}
}

#[derive(Clone)]
#[allow(dead_code)] // fields to be used in the future
pub struct DirtyReasonDeps {
pub(super) old: Vec<DepFingerprint>,
Expand All @@ -96,6 +97,7 @@ impl fmt::Debug for DirtyReasonDeps {
}
}

#[derive(Clone)]
pub struct FsStatusOutdated(FsStatus);

impl FsStatusOutdated {
Expand Down
39 changes: 27 additions & 12 deletions src/cargo/core/compiler/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::util::CargoResult;
pub struct Job {
work: Work,
fresh: Freshness,

dirty_reason: Option<DirtyReason>,
}

/// Each proc should send its description before starting.
Expand Down Expand Up @@ -48,16 +46,14 @@ impl Job {
Job {
work: Work::noop(),
fresh: Freshness::Fresh,
dirty_reason: None,
}
}

/// Creates a new job representing a unit of work.
pub fn new_dirty(work: Work, dirty_reason: Option<DirtyReason>) -> Job {
Job {
work,
fresh: Freshness::Dirty,
dirty_reason,
fresh: Freshness::Dirty(dirty_reason),
}
}

Expand All @@ -70,18 +66,14 @@ impl Job {
/// Returns whether this job was fresh/dirty, where "fresh" means we're
/// likely to perform just some small bookkeeping where "dirty" means we'll
/// probably do something slow like invoke rustc.
pub fn freshness(&self) -> Freshness {
self.fresh
pub fn freshness(&self) -> &Freshness {
&self.fresh
}

pub fn before(&mut self, next: Work) {
let prev = mem::replace(&mut self.work, Work::noop());
self.work = next.then(prev);
}

pub fn dirty_reason(&self) -> Option<&DirtyReason> {
self.dirty_reason.as_ref()
}
}

impl fmt::Debug for Job {
Expand All @@ -94,8 +86,31 @@ impl fmt::Debug for Job {
///
/// A fresh package does not necessarily need to be rebuilt (unless a dependency
/// was also rebuilt), and a dirty package must always be rebuilt.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub enum Freshness {
Fresh,
Dirty(Option<DirtyReason>),
}

impl Freshness {
pub fn is_dirty(&self) -> bool {
matches!(self, Freshness::Dirty(_))
}

pub fn is_fresh(&self) -> bool {
matches!(self, Freshness::Fresh)
}

pub fn kind(&self) -> FreshnessKind {
match self {
Freshness::Fresh => FreshnessKind::Fresh,
Freshness::Dirty(_) => FreshnessKind::Dirty,
}
}
}

#[derive(Copy, Clone, Debug)]
pub enum FreshnessKind {
Fresh,
Dirty,
}
15 changes: 7 additions & 8 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ use super::job::{
};
use super::timings::Timings;
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
use crate::core::compiler::fingerprint::DirtyReason;
use crate::core::compiler::future_incompat::{
self, FutureBreakageItem, FutureIncompatReportPackage,
};
use crate::core::compiler::job::FreshnessKind;
use crate::core::resolver::ResolveBehavior;
use crate::core::{PackageId, Shell, TargetKind};
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
Expand Down Expand Up @@ -676,7 +676,7 @@ impl<'cfg> DrainState<'cfg> {
// NOTE: An error here will drop the job without starting it.
// That should be OK, since we want to exit as soon as
// possible during an error.
self.note_working_on(cx.bcx.config, &unit, job.freshness(), job.dirty_reason())?;
self.note_working_on(cx.bcx.config, &unit, job.freshness())?;
}
self.run(&unit, job, cx, scope);
}
Expand Down Expand Up @@ -1115,7 +1115,7 @@ impl<'cfg> DrainState<'cfg> {
assert!(self.active.insert(id, unit.clone()).is_none());

let messages = self.messages.clone();
let fresh = job.freshness();
let fresh = job.freshness().kind(); // closure below moves job, cannot keep `&Freshness`
let rmeta_required = cx.rmeta_required(unit);

let doit = move |state: JobState<'_, '_>| {
Expand Down Expand Up @@ -1167,7 +1167,7 @@ impl<'cfg> DrainState<'cfg> {
};

match fresh {
Freshness::Fresh => {
FreshnessKind::Fresh => {
self.timings.add_fresh();
// Running a fresh job on the same thread is often much faster than spawning a new
// thread to run the job.
Expand All @@ -1179,7 +1179,7 @@ impl<'cfg> DrainState<'cfg> {
_marker: marker::PhantomData,
});
}
Freshness::Dirty => {
FreshnessKind::Dirty => {
self.timings.add_dirty();
scope.spawn(move || {
doit(JobState {
Expand Down Expand Up @@ -1342,8 +1342,7 @@ impl<'cfg> DrainState<'cfg> {
&mut self,
config: &Config,
unit: &Unit,
fresh: Freshness,
dirty_reason: Option<&DirtyReason>,
fresh: &Freshness,
) -> CargoResult<()> {
if (self.compiled.contains(&unit.pkg.package_id())
&& !unit.mode.is_doc()
Expand All @@ -1357,7 +1356,7 @@ impl<'cfg> DrainState<'cfg> {
match fresh {
// Any dirty stage which runs at least one command gets printed as
// being a compiled package.
Dirty => {
Dirty(dirty_reason) => {
if unit.mode.is_doc() {
self.documented.insert(unit.pkg.package_id());
config.shell().status("Documenting", &unit.pkg)?;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn compile<'cfg>(
} else {
let force = exec.force_rebuild(unit) || force_rebuild;
let mut job = fingerprint::prepare_target(cx, unit, force)?;
job.before(if job.freshness() == Freshness::Dirty {
job.before(if job.freshness().is_dirty() {
let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
rustdoc(cx, unit)?
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{env, fs};

use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, Freshness, UnitOutput};
use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, UnitOutput};
use crate::core::{Dependency, Edition, Package, PackageId, Source, SourceId, Workspace};
use crate::ops::CompileFilter;
use crate::ops::{common_for_install_and_uninstall::*, FilterRule};
Expand Down Expand Up @@ -683,7 +683,7 @@ fn is_installed(
let tracker = InstallTracker::load(config, root)?;
let (freshness, _duplicates) =
tracker.check_upgrade(dst, pkg, force, opts, target, &rustc.verbose_version)?;
Ok(freshness == Freshness::Fresh)
Ok(freshness.is_fresh())
}

/// Checks if vers can only be satisfied by exactly one version of a package in a registry, and it's
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl InstallTracker {
// Check if any tracked exe's are already installed.
let duplicates = self.find_duplicates(dst, &exes);
if force || duplicates.is_empty() {
return Ok((Freshness::Dirty, duplicates));
return Ok((Freshness::Dirty(None), duplicates));
}
// Check if all duplicates come from packages of the same name. If
// there are duplicates from other packages, then --force will be
Expand Down Expand Up @@ -200,7 +200,7 @@ impl InstallTracker {
let source_id = pkg.package_id().source_id();
if source_id.is_path() {
// `cargo install --path ...` is always rebuilt.
return Ok((Freshness::Dirty, duplicates));
return Ok((Freshness::Dirty(None), duplicates));
}
let is_up_to_date = |dupe_pkg_id| {
let info = self
Expand All @@ -224,7 +224,7 @@ impl InstallTracker {
if matching_duplicates.iter().all(is_up_to_date) {
Ok((Freshness::Fresh, duplicates))
} else {
Ok((Freshness::Dirty, duplicates))
Ok((Freshness::Dirty(None), duplicates))
}
} else {
// Format the error message.
Expand Down

0 comments on commit 00b81c2

Please sign in to comment.