Skip to content

Commit 1ae512d

Browse files
committed
Auto merge of #6946 - Eh2406:remove_candidate, r=alexcrichton
Remove Candidate `Candidate` was a type to record the possibility that a package was replaced using the replacements feature. However there were only two places that cared about this possibility. One was switched to used a lookup table in #6860. This PR switches the other to use that table as well. Making the entire `Candidate` type unnecessary. The main benefit of this change is a reduction in the cognitive complexity.
2 parents 1b4fab3 + c1b22c5 commit 1ae512d

File tree

4 files changed

+41
-55
lines changed

4 files changed

+41
-55
lines changed

src/cargo/core/resolver/dep_cache.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::core::interning::InternedString;
1919
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
2020
use crate::util::errors::CargoResult;
2121

22-
use crate::core::resolver::types::{Candidate, ConflictReason, DepInfo, FeaturesSet};
22+
use crate::core::resolver::types::{ConflictReason, DepInfo, FeaturesSet};
2323
use crate::core::resolver::{ActivateResult, Method};
2424

2525
pub struct RegistryQueryer<'a> {
@@ -31,14 +31,14 @@ pub struct RegistryQueryer<'a> {
3131
/// specify minimum dependency versions to be used.
3232
minimal_versions: bool,
3333
/// a cache of `Candidate`s that fulfil a `Dependency`
34-
registry_cache: HashMap<Dependency, Rc<Vec<Candidate>>>,
34+
registry_cache: HashMap<Dependency, Rc<Vec<Summary>>>,
3535
/// a cache of `Dependency`s that are required for a `Summary`
3636
summary_cache: HashMap<
3737
(Option<PackageId>, Summary, Method),
3838
Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>,
3939
>,
4040
/// all the cases we ended up using a supplied replacement
41-
used_replacements: HashMap<PackageId, PackageId>,
41+
used_replacements: HashMap<PackageId, Summary>,
4242
}
4343

4444
impl<'a> RegistryQueryer<'a> {
@@ -60,7 +60,11 @@ impl<'a> RegistryQueryer<'a> {
6060
}
6161

6262
pub fn used_replacement_for(&self, p: PackageId) -> Option<(PackageId, PackageId)> {
63-
self.used_replacements.get(&p).map(|&r| (p, r))
63+
self.used_replacements.get(&p).map(|r| (p, r.package_id()))
64+
}
65+
66+
pub fn replacement_summary(&self, p: PackageId) -> Option<&Summary> {
67+
self.used_replacements.get(&p)
6468
}
6569

6670
/// Queries the `registry` to return a list of candidates for `dep`.
@@ -69,7 +73,7 @@ impl<'a> RegistryQueryer<'a> {
6973
/// any candidates are returned which match an override then the override is
7074
/// applied by performing a second query for what the override should
7175
/// return.
72-
pub fn query(&mut self, dep: &Dependency) -> CargoResult<Rc<Vec<Candidate>>> {
76+
pub fn query(&mut self, dep: &Dependency) -> CargoResult<Rc<Vec<Summary>>> {
7377
if let Some(out) = self.registry_cache.get(dep).cloned() {
7478
return Ok(out);
7579
}
@@ -78,16 +82,11 @@ impl<'a> RegistryQueryer<'a> {
7882
self.registry.query(
7983
dep,
8084
&mut |s| {
81-
ret.push(Candidate {
82-
summary: s,
83-
replace: None,
84-
});
85+
ret.push(s);
8586
},
8687
false,
8788
)?;
88-
for candidate in ret.iter_mut() {
89-
let summary = &candidate.summary;
90-
89+
for summary in ret.iter_mut() {
9190
let mut potential_matches = self
9291
.replacements
9392
.iter()
@@ -157,25 +156,22 @@ impl<'a> RegistryQueryer<'a> {
157156
for dep in summary.dependencies() {
158157
debug!("\t{} => {}", dep.package_name(), dep.version_req());
159158
}
160-
if let Some(r) = &replace {
161-
self.used_replacements
162-
.insert(summary.package_id(), r.package_id());
159+
if let Some(r) = replace {
160+
self.used_replacements.insert(summary.package_id(), r);
163161
}
164-
165-
candidate.replace = replace;
166162
}
167163

168164
// When we attempt versions for a package we'll want to do so in a
169165
// sorted fashion to pick the "best candidates" first. Currently we try
170166
// prioritized summaries (those in `try_to_use`) and failing that we
171167
// list everything from the maximum version to the lowest version.
172168
ret.sort_unstable_by(|a, b| {
173-
let a_in_previous = self.try_to_use.contains(&a.summary.package_id());
174-
let b_in_previous = self.try_to_use.contains(&b.summary.package_id());
169+
let a_in_previous = self.try_to_use.contains(&a.package_id());
170+
let b_in_previous = self.try_to_use.contains(&b.package_id());
175171
let previous_cmp = a_in_previous.cmp(&b_in_previous).reverse();
176172
match previous_cmp {
177173
Ordering::Equal => {
178-
let cmp = a.summary.version().cmp(b.summary.version());
174+
let cmp = a.version().cmp(b.version());
179175
if self.minimal_versions {
180176
// Lower version ordered first.
181177
cmp

src/cargo/core/resolver/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use failure::{Error, Fail};
77
use semver;
88

99
use super::context::Context;
10-
use super::types::{Candidate, ConflictMap, ConflictReason};
10+
use super::types::{ConflictMap, ConflictReason};
1111

1212
/// Error during resolution providing a path of `PackageId`s.
1313
pub struct ResolveError {
@@ -74,7 +74,7 @@ pub(super) fn activation_error(
7474
parent: &Summary,
7575
dep: &Dependency,
7676
conflicting_activations: &ConflictMap,
77-
candidates: &[Candidate],
77+
candidates: &[Summary],
7878
config: Option<&Config>,
7979
) -> ResolveError {
8080
let to_resolve_err = |err| {
@@ -101,7 +101,7 @@ pub(super) fn activation_error(
101101
msg.push_str(
102102
&candidates
103103
.iter()
104-
.map(|v| v.summary.version())
104+
.map(|v| v.version())
105105
.map(|v| v.to_string())
106106
.collect::<Vec<_>>()
107107
.join(", "),

src/cargo/core/resolver/mod.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::util::profile;
6262

6363
use self::context::{Activations, Context};
6464
use self::dep_cache::RegistryQueryer;
65-
use self::types::{Candidate, ConflictMap, ConflictReason, DepsFrame};
65+
use self::types::{ConflictMap, ConflictReason, DepsFrame};
6666
use self::types::{FeaturesSet, RcVecIter, RemainingDeps, ResolverProgress};
6767

6868
pub use self::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
@@ -181,11 +181,7 @@ fn activate_deps_loop(
181181
// Activate all the initial summaries to kick off some work.
182182
for &(ref summary, ref method) in summaries {
183183
debug!("initial activation: {}", summary.package_id());
184-
let candidate = Candidate {
185-
summary: summary.clone(),
186-
replace: None,
187-
};
188-
let res = activate(&mut cx, registry, None, candidate, method.clone());
184+
let res = activate(&mut cx, registry, None, summary.clone(), method.clone());
189185
match res {
190186
Ok(Some((frame, _))) => remaining_deps.push(frame),
191187
Ok(None) => (),
@@ -370,7 +366,7 @@ fn activate_deps_loop(
370366
None
371367
};
372368

373-
let pid = candidate.summary.package_id();
369+
let pid = candidate.package_id();
374370
let method = Method::Required {
375371
dev_deps: false,
376372
features: Rc::clone(&features),
@@ -382,7 +378,7 @@ fn activate_deps_loop(
382378
parent.name(),
383379
cur,
384380
dep.package_name(),
385-
candidate.summary.version()
381+
candidate.version()
386382
);
387383
let res = activate(&mut cx, registry, Some((&parent, &dep)), candidate, method);
388384

@@ -595,10 +591,10 @@ fn activate(
595591
cx: &mut Context,
596592
registry: &mut RegistryQueryer<'_>,
597593
parent: Option<(&Summary, &Dependency)>,
598-
candidate: Candidate,
594+
candidate: Summary,
599595
method: Method,
600596
) -> ActivateResult<Option<(DepsFrame, Duration)>> {
601-
let candidate_pid = candidate.summary.package_id();
597+
let candidate_pid = candidate.package_id();
602598
if let Some((parent, dep)) = parent {
603599
let parent_pid = parent.package_id();
604600
Rc::make_mut(
@@ -657,9 +653,9 @@ fn activate(
657653
}
658654
}
659655

660-
let activated = cx.flag_activated(&candidate.summary, &method)?;
656+
let activated = cx.flag_activated(&candidate, &method)?;
661657

662-
let candidate = match candidate.replace {
658+
let candidate = match registry.replacement_summary(candidate_pid) {
663659
Some(replace) => {
664660
if cx.flag_activated(&replace, &method)? && activated {
665661
return Ok(None);
@@ -669,14 +665,14 @@ fn activate(
669665
replace.package_id(),
670666
candidate_pid
671667
);
672-
replace
668+
replace.clone()
673669
}
674670
None => {
675671
if activated {
676672
return Ok(None);
677673
}
678674
trace!("activating {}", candidate_pid);
679-
candidate.summary
675+
candidate
680676
}
681677
};
682678

@@ -727,13 +723,13 @@ struct BacktrackFrame {
727723
/// filtered out, and as they are filtered the causes will be added to `conflicting_prev_active`.
728724
#[derive(Clone)]
729725
struct RemainingCandidates {
730-
remaining: RcVecIter<Candidate>,
726+
remaining: RcVecIter<Summary>,
731727
// This is a inlined peekable generator
732-
has_another: Option<Candidate>,
728+
has_another: Option<Summary>,
733729
}
734730

735731
impl RemainingCandidates {
736-
fn new(candidates: &Rc<Vec<Candidate>>) -> RemainingCandidates {
732+
fn new(candidates: &Rc<Vec<Summary>>) -> RemainingCandidates {
737733
RemainingCandidates {
738734
remaining: RcVecIter::new(Rc::clone(candidates)),
739735
has_another: None,
@@ -762,14 +758,14 @@ impl RemainingCandidates {
762758
cx: &Context,
763759
dep: &Dependency,
764760
parent: PackageId,
765-
) -> Option<(Candidate, bool)> {
761+
) -> Option<(Summary, bool)> {
766762
'main: for (_, b) in self.remaining.by_ref() {
767-
let b_id = b.summary.package_id();
763+
let b_id = b.package_id();
768764
// The `links` key in the manifest dictates that there's only one
769765
// package in a dependency graph, globally, with that particular
770766
// `links` key. If this candidate links to something that's already
771767
// linked to by a different package then we've gotta skip this.
772-
if let Some(link) = b.summary.links() {
768+
if let Some(link) = b.links() {
773769
if let Some(&a) = cx.links.get(&link) {
774770
if a != b_id {
775771
conflicting_prev_active
@@ -789,7 +785,7 @@ impl RemainingCandidates {
789785
// Here we throw out our candidate if it's *compatible*, yet not
790786
// equal, to all previously activated versions.
791787
if let Some((a, _)) = cx.activations.get(&b_id.as_activations_key()) {
792-
if *a != b.summary {
788+
if *a != b {
793789
conflicting_prev_active
794790
.entry(a.package_id())
795791
.or_insert(ConflictReason::Semver);
@@ -905,7 +901,7 @@ fn generalize_conflicting(
905901
.find(
906902
dep,
907903
&|id| {
908-
if id == other.summary.package_id() {
904+
if id == other.package_id() {
909905
// we are imagining that we used other instead
910906
Some(backtrack_critical_age)
911907
} else {
@@ -914,9 +910,9 @@ fn generalize_conflicting(
914910
age < backtrack_critical_age)
915911
}
916912
},
917-
Some(other.summary.package_id()),
913+
Some(other.package_id()),
918914
)
919-
.map(|con| (other.summary.package_id(), con))
915+
.map(|con| (other.package_id(), con))
920916
})
921917
.collect::<Option<Vec<(PackageId, &ConflictMap)>>>()
922918
{
@@ -973,7 +969,7 @@ fn find_candidate(
973969
parent: &Summary,
974970
backtracked: bool,
975971
conflicting_activations: &ConflictMap,
976-
) -> Option<(Candidate, bool, BacktrackFrame)> {
972+
) -> Option<(Summary, bool, BacktrackFrame)> {
977973
// When we're calling this method we know that `parent` failed to
978974
// activate. That means that some dependency failed to get resolved for
979975
// whatever reason. Normally, that means that all of those reasons

src/cargo/core/resolver/types.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ impl Method {
122122
}
123123
}
124124

125-
#[derive(Clone)]
126-
pub struct Candidate {
127-
pub summary: Summary,
128-
pub replace: Option<Summary>,
129-
}
130-
131125
#[derive(Clone)]
132126
pub struct DepsFrame {
133127
pub parent: Summary,
@@ -231,7 +225,7 @@ impl RemainingDeps {
231225
/// Information about the dependencies for a crate, a tuple of:
232226
///
233227
/// (dependency info, candidates, features activated)
234-
pub type DepInfo = (Dependency, Rc<Vec<Candidate>>, FeaturesSet);
228+
pub type DepInfo = (Dependency, Rc<Vec<Summary>>, FeaturesSet);
235229

236230
/// All possible reasons that a package might fail to activate.
237231
///

0 commit comments

Comments
 (0)