Skip to content

Commit 00b81c2

Browse files
committed
Move Option<DirtyReason> into Freshness
1 parent e08848c commit 00b81c2

File tree

7 files changed

+45
-29
lines changed

7 files changed

+45
-29
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::job::{Freshness, Job, Work};
1+
use super::job::{Job, Work};
22
use super::{fingerprint, Context, LinkType, Unit};
33
use crate::core::compiler::artifact;
44
use crate::core::compiler::context::Metadata;
@@ -488,7 +488,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
488488
} else {
489489
fingerprint::prepare_target(cx, unit, false)?
490490
};
491-
if job.freshness() == Freshness::Dirty {
491+
if job.freshness().is_dirty() {
492492
job.before(dirty);
493493
} else {
494494
job.before(fresh);

src/cargo/core/compiler/fingerprint/dirty_reason.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::core::Shell;
44
use crate::Config;
55
use std::fmt;
66

7-
#[derive(Debug)]
7+
#[derive(Clone, Debug)]
88
pub enum DirtyReason {
99
RustcChanged,
1010
FeaturesChanged {
@@ -84,6 +84,7 @@ impl DirtyReason {
8484
}
8585
}
8686

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

100+
#[derive(Clone)]
99101
pub struct FsStatusOutdated(FsStatus);
100102

101103
impl FsStatusOutdated {

src/cargo/core/compiler/job.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use crate::util::CargoResult;
88
pub struct Job {
99
work: Work,
1010
fresh: Freshness,
11-
12-
dirty_reason: Option<DirtyReason>,
1311
}
1412

1513
/// Each proc should send its description before starting.
@@ -48,16 +46,14 @@ impl Job {
4846
Job {
4947
work: Work::noop(),
5048
fresh: Freshness::Fresh,
51-
dirty_reason: None,
5249
}
5350
}
5451

5552
/// Creates a new job representing a unit of work.
5653
pub fn new_dirty(work: Work, dirty_reason: Option<DirtyReason>) -> Job {
5754
Job {
5855
work,
59-
fresh: Freshness::Dirty,
60-
dirty_reason,
56+
fresh: Freshness::Dirty(dirty_reason),
6157
}
6258
}
6359

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

7773
pub fn before(&mut self, next: Work) {
7874
let prev = mem::replace(&mut self.work, Work::noop());
7975
self.work = next.then(prev);
8076
}
81-
82-
pub fn dirty_reason(&self) -> Option<&DirtyReason> {
83-
self.dirty_reason.as_ref()
84-
}
8577
}
8678

8779
impl fmt::Debug for Job {
@@ -94,8 +86,31 @@ impl fmt::Debug for Job {
9486
///
9587
/// A fresh package does not necessarily need to be rebuilt (unless a dependency
9688
/// was also rebuilt), and a dirty package must always be rebuilt.
97-
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
89+
#[derive(Debug, Clone)]
9890
pub enum Freshness {
91+
Fresh,
92+
Dirty(Option<DirtyReason>),
93+
}
94+
95+
impl Freshness {
96+
pub fn is_dirty(&self) -> bool {
97+
matches!(self, Freshness::Dirty(_))
98+
}
99+
100+
pub fn is_fresh(&self) -> bool {
101+
matches!(self, Freshness::Fresh)
102+
}
103+
104+
pub fn kind(&self) -> FreshnessKind {
105+
match self {
106+
Freshness::Fresh => FreshnessKind::Fresh,
107+
Freshness::Dirty(_) => FreshnessKind::Dirty,
108+
}
109+
}
110+
}
111+
112+
#[derive(Copy, Clone, Debug)]
113+
pub enum FreshnessKind {
99114
Fresh,
100115
Dirty,
101116
}

src/cargo/core/compiler/job_queue.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ use super::job::{
7171
};
7272
use super::timings::Timings;
7373
use super::{BuildContext, BuildPlan, CompileMode, Context, Unit};
74-
use crate::core::compiler::fingerprint::DirtyReason;
7574
use crate::core::compiler::future_incompat::{
7675
self, FutureBreakageItem, FutureIncompatReportPackage,
7776
};
77+
use crate::core::compiler::job::FreshnessKind;
7878
use crate::core::resolver::ResolveBehavior;
7979
use crate::core::{PackageId, Shell, TargetKind};
8080
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
@@ -676,7 +676,7 @@ impl<'cfg> DrainState<'cfg> {
676676
// NOTE: An error here will drop the job without starting it.
677677
// That should be OK, since we want to exit as soon as
678678
// possible during an error.
679-
self.note_working_on(cx.bcx.config, &unit, job.freshness(), job.dirty_reason())?;
679+
self.note_working_on(cx.bcx.config, &unit, job.freshness())?;
680680
}
681681
self.run(&unit, job, cx, scope);
682682
}
@@ -1115,7 +1115,7 @@ impl<'cfg> DrainState<'cfg> {
11151115
assert!(self.active.insert(id, unit.clone()).is_none());
11161116

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

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

11691169
match fresh {
1170-
Freshness::Fresh => {
1170+
FreshnessKind::Fresh => {
11711171
self.timings.add_fresh();
11721172
// Running a fresh job on the same thread is often much faster than spawning a new
11731173
// thread to run the job.
@@ -1179,7 +1179,7 @@ impl<'cfg> DrainState<'cfg> {
11791179
_marker: marker::PhantomData,
11801180
});
11811181
}
1182-
Freshness::Dirty => {
1182+
FreshnessKind::Dirty => {
11831183
self.timings.add_dirty();
11841184
scope.spawn(move || {
11851185
doit(JobState {
@@ -1342,8 +1342,7 @@ impl<'cfg> DrainState<'cfg> {
13421342
&mut self,
13431343
config: &Config,
13441344
unit: &Unit,
1345-
fresh: Freshness,
1346-
dirty_reason: Option<&DirtyReason>,
1345+
fresh: &Freshness,
13471346
) -> CargoResult<()> {
13481347
if (self.compiled.contains(&unit.pkg.package_id())
13491348
&& !unit.mode.is_doc()
@@ -1357,7 +1356,7 @@ impl<'cfg> DrainState<'cfg> {
13571356
match fresh {
13581357
// Any dirty stage which runs at least one command gets printed as
13591358
// being a compiled package.
1360-
Dirty => {
1359+
Dirty(dirty_reason) => {
13611360
if unit.mode.is_doc() {
13621361
self.documented.insert(unit.pkg.package_id());
13631362
config.shell().status("Documenting", &unit.pkg)?;

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn compile<'cfg>(
167167
} else {
168168
let force = exec.force_rebuild(unit) || force_rebuild;
169169
let mut job = fingerprint::prepare_target(cx, unit, force)?;
170-
job.before(if job.freshness() == Freshness::Dirty {
170+
job.before(if job.freshness().is_dirty() {
171171
let work = if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
172172
rustdoc(cx, unit)?
173173
} else {

src/cargo/ops/cargo_install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33
use std::sync::Arc;
44
use std::{env, fs};
55

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

689689
/// Checks if vers can only be satisfied by exactly one version of a package in a registry, and it's

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl InstallTracker {
170170
// Check if any tracked exe's are already installed.
171171
let duplicates = self.find_duplicates(dst, &exes);
172172
if force || duplicates.is_empty() {
173-
return Ok((Freshness::Dirty, duplicates));
173+
return Ok((Freshness::Dirty(None), duplicates));
174174
}
175175
// Check if all duplicates come from packages of the same name. If
176176
// there are duplicates from other packages, then --force will be
@@ -200,7 +200,7 @@ impl InstallTracker {
200200
let source_id = pkg.package_id().source_id();
201201
if source_id.is_path() {
202202
// `cargo install --path ...` is always rebuilt.
203-
return Ok((Freshness::Dirty, duplicates));
203+
return Ok((Freshness::Dirty(None), duplicates));
204204
}
205205
let is_up_to_date = |dupe_pkg_id| {
206206
let info = self
@@ -224,7 +224,7 @@ impl InstallTracker {
224224
if matching_duplicates.iter().all(is_up_to_date) {
225225
Ok((Freshness::Fresh, duplicates))
226226
} else {
227-
Ok((Freshness::Dirty, duplicates))
227+
Ok((Freshness::Dirty(None), duplicates))
228228
}
229229
} else {
230230
// Format the error message.

0 commit comments

Comments
 (0)