Skip to content

Commit c1ca055

Browse files
committed
add some comments
1 parent 9b8b12c commit c1ca055

File tree

5 files changed

+57
-33
lines changed

5 files changed

+57
-33
lines changed

src/cargo/core/resolver/conflict_cache.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use std::collections::{BTreeMap, HashMap, HashSet};
22

33
use log::trace;
44

5-
use super::types::{Conflict, ConflictReason};
5+
use super::types::{ConflictMap, ConflictReason};
66
use crate::core::resolver::Context;
77
use crate::core::{Dependency, PackageId};
88

99
/// This is a trie for storing a large number of sets designed to
1010
/// efficiently see if any of the stored sets are a subset of a search set.
1111
enum ConflictStoreTrie {
1212
/// One of the stored sets.
13-
Leaf(Conflict),
13+
Leaf(ConflictMap),
1414
/// A map from an element to a subtrie where
1515
/// all the sets in the subtrie contains that element.
1616
Node(BTreeMap<PackageId, ConflictStoreTrie>),
@@ -19,7 +19,11 @@ enum ConflictStoreTrie {
1919
impl ConflictStoreTrie {
2020
/// Finds any known set of conflicts, if any,
2121
/// which are activated in `cx` and pass the `filter` specified?
22-
fn find_conflicting(&self, cx: &Context, must_contain: Option<PackageId>) -> Option<&Conflict> {
22+
fn find_conflicting(
23+
&self,
24+
cx: &Context,
25+
must_contain: Option<PackageId>,
26+
) -> Option<&ConflictMap> {
2327
match self {
2428
ConflictStoreTrie::Leaf(c) => {
2529
if must_contain.is_none() {
@@ -53,7 +57,7 @@ impl ConflictStoreTrie {
5357
}
5458
}
5559

56-
fn insert(&mut self, mut iter: impl Iterator<Item = PackageId>, con: Conflict) {
60+
fn insert(&mut self, mut iter: impl Iterator<Item = PackageId>, con: ConflictMap) {
5761
if let Some(pid) = iter.next() {
5862
if let ConflictStoreTrie::Node(p) = self {
5963
p.entry(pid)
@@ -139,7 +143,7 @@ impl ConflictCache {
139143
cx: &Context,
140144
dep: &Dependency,
141145
must_contain: Option<PackageId>,
142-
) -> Option<&Conflict> {
146+
) -> Option<&ConflictMap> {
143147
let out = self
144148
.con_from_dep
145149
.get(dep)?
@@ -153,14 +157,14 @@ impl ConflictCache {
153157
}
154158
out
155159
}
156-
pub fn conflicting(&self, cx: &Context, dep: &Dependency) -> Option<&Conflict> {
160+
pub fn conflicting(&self, cx: &Context, dep: &Dependency) -> Option<&ConflictMap> {
157161
self.find_conflicting(cx, dep, None)
158162
}
159163

160164
/// Adds to the cache a conflict of the form:
161165
/// `dep` is known to be unresolvable if
162166
/// all the `PackageId` entries are activated.
163-
pub fn insert(&mut self, dep: &Dependency, con: &Conflict) {
167+
pub fn insert(&mut self, dep: &Dependency, con: &ConflictMap) {
164168
if con.values().any(|c| *c == ConflictReason::PublicDependency) {
165169
// TODO: needs more info for back jumping
166170
// for now refuse to cache it.

src/cargo/core/resolver/context.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use crate::util::CargoResult;
1212
use crate::util::Graph;
1313

1414
use super::errors::ActivateResult;
15-
use super::types::{Conflict, ConflictReason, DepInfo, GraphNode, Method, RcList, RegistryQueryer};
15+
use super::types::{
16+
ConflictMap, ConflictReason, DepInfo, GraphNode, Method, RcList, RegistryQueryer,
17+
};
1618

1719
pub use super::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
1820
pub use super::encode::{Metadata, WorkspaceResolve};
@@ -25,13 +27,19 @@ pub use super::resolve::Resolve;
2527
#[derive(Clone)]
2628
pub struct Context {
2729
pub activations: Activations,
30+
/// list the features that are activated for each package
2831
pub resolve_features: im_rc::HashMap<PackageId, Rc<HashSet<InternedString>>>,
32+
/// get the package that will be linking to a native library by its links attribute
2933
pub links: im_rc::HashMap<InternedString, PackageId>,
34+
/// for each package the list of names it can see,
35+
/// then for each name the exact version that name represents and weather the name is public.
3036
pub public_dependency:
3137
im_rc::HashMap<PackageId, im_rc::HashMap<InternedString, (PackageId, bool)>>,
3238

3339
// This is somewhat redundant with the `resolve_graph` that stores the same data,
3440
// but for querying in the opposite order.
41+
/// a way to look up for a package in activations what packages required it
42+
/// and all of the exact deps that it fulfilled.
3543
pub parents: Graph<PackageId, Rc<Vec<Dependency>>>,
3644

3745
// These are two cheaply-cloneable lists (O(1) clone) which are effectively
@@ -44,6 +52,7 @@ pub struct Context {
4452
pub warnings: RcList<String>,
4553
}
4654

55+
/// list all the activated versions of a particular crate name from a source
4756
pub type Activations = im_rc::HashMap<(InternedString, SourceId), Rc<Vec<Summary>>>;
4857

4958
impl Context {
@@ -155,7 +164,7 @@ impl Context {
155164
pub fn is_conflicting(
156165
&self,
157166
parent: Option<PackageId>,
158-
conflicting_activations: &Conflict,
167+
conflicting_activations: &ConflictMap,
159168
) -> bool {
160169
conflicting_activations
161170
.keys()

src/cargo/core/resolver/errors.rs

Lines changed: 2 additions & 2 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, Conflict, ConflictReason};
10+
use super::types::{Candidate, ConflictMap, ConflictReason};
1111

1212
/// Error during resolution providing a path of `PackageId`s.
1313
pub struct ResolveError {
@@ -72,7 +72,7 @@ pub(super) fn activation_error(
7272
registry: &mut dyn Registry,
7373
parent: &Summary,
7474
dep: &Dependency,
75-
conflicting_activations: &Conflict,
75+
conflicting_activations: &ConflictMap,
7676
candidates: &[Candidate],
7777
config: Option<&Config>,
7878
) -> ResolveError {

src/cargo/core/resolver/mod.rs

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

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

6868
pub use self::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
@@ -247,7 +247,7 @@ fn activate_deps_loop(
247247
//
248248
// This is a map of package ID to a reason why that packaged caused a
249249
// conflict for us.
250-
let mut conflicting_activations = Conflict::new();
250+
let mut conflicting_activations = ConflictMap::new();
251251

252252
// When backtracking we don't fully update `conflicting_activations`
253253
// especially for the cases that we didn't make a backtrack frame in the
@@ -590,28 +590,28 @@ fn activate(
590590
candidate: Candidate,
591591
method: &Method<'_>,
592592
) -> ActivateResult<Option<(DepsFrame, Duration)>> {
593+
let candidate_pid = candidate.summary.package_id();
593594
if let Some((parent, dep)) = parent {
594-
cx.resolve_graph.push(GraphNode::Link(
595-
parent.package_id(),
596-
candidate.summary.package_id(),
597-
dep.clone(),
598-
));
595+
let parent_pid = parent.package_id();
596+
cx.resolve_graph
597+
.push(GraphNode::Link(parent_pid, candidate_pid, dep.clone()));
599598
Rc::make_mut(
600-
cx.parents
601-
.link(candidate.summary.package_id(), parent.package_id()),
599+
// add a edge from candidate to parent in the parents graph
600+
cx.parents.link(candidate_pid, parent_pid),
602601
)
602+
// and associate dep with that edge
603603
.push(dep.clone());
604604
let cs: Vec<PackageId> = cx
605605
.public_dependency
606-
.get(&candidate.summary.package_id())
606+
.get(&candidate_pid)
607607
.iter()
608608
.flat_map(|x| x.values())
609609
.filter_map(|x| if x.1 { Some(&x.0) } else { None })
610-
.chain(Some(candidate.summary.package_id()).iter())
610+
.chain(&Some(candidate_pid))
611611
.cloned()
612612
.collect();
613613
for c in cs {
614-
let mut stack = vec![(parent.package_id(), dep.is_public())];
614+
let mut stack = vec![(parent_pid, dep.is_public())];
615615
while let Some((p, public)) = stack.pop() {
616616
match cx.public_dependency.entry(p).or_default().entry(c.name()) {
617617
im_rc::hashmap::Entry::Occupied(mut o) => {
@@ -641,22 +641,22 @@ fn activate(
641641
let candidate = match candidate.replace {
642642
Some(replace) => {
643643
cx.resolve_replacements
644-
.push((candidate.summary.package_id(), replace.package_id()));
644+
.push((candidate_pid, replace.package_id()));
645645
if cx.flag_activated(&replace, method)? && activated {
646646
return Ok(None);
647647
}
648648
trace!(
649649
"activating {} (replacing {})",
650650
replace.package_id(),
651-
candidate.summary.package_id()
651+
candidate_pid
652652
);
653653
replace
654654
}
655655
None => {
656656
if activated {
657657
return Ok(None);
658658
}
659-
trace!("activating {}", candidate.summary.package_id());
659+
trace!("activating {}", candidate_pid);
660660
candidate.summary
661661
}
662662
};
@@ -680,7 +680,7 @@ struct BacktrackFrame {
680680
parent: Summary,
681681
dep: Dependency,
682682
features: Rc<Vec<InternedString>>,
683-
conflicting_activations: Conflict,
683+
conflicting_activations: ConflictMap,
684684
}
685685

686686
/// A helper "iterator" used to extract candidates within a current `Context` of
@@ -727,7 +727,7 @@ impl RemainingCandidates {
727727
/// original list for the reason listed.
728728
fn next(
729729
&mut self,
730-
conflicting_prev_active: &mut Conflict,
730+
conflicting_prev_active: &mut ConflictMap,
731731
cx: &Context,
732732
dep: &Dependency,
733733
parent: PackageId,
@@ -769,13 +769,18 @@ impl RemainingCandidates {
769769
continue;
770770
}
771771
}
772+
// We may still have to reject do to a public dependency conflict. If one of any of our
773+
// ancestors that can see us already knows about a different crate with this name then
774+
// we have to reject this candidate. Additionally this candidate may already have been
775+
// activated and have public dependants of its own,
776+
// all of witch also need to be checked the same way.
772777
for &t in cx
773778
.public_dependency
774779
.get(&b.summary.package_id())
775780
.iter()
776781
.flat_map(|x| x.values())
777782
.filter_map(|x| if x.1 { Some(&x.0) } else { None })
778-
.chain(Some(b.summary.package_id()).iter())
783+
.chain(&Some(b.summary.package_id()))
779784
{
780785
let mut stack = vec![(parent, dep.is_public())];
781786
while let Some((p, public)) = stack.pop() {
@@ -845,7 +850,7 @@ fn find_candidate(
845850
backtrack_stack: &mut Vec<BacktrackFrame>,
846851
parent: &Summary,
847852
backtracked: bool,
848-
conflicting_activations: &Conflict,
853+
conflicting_activations: &ConflictMap,
849854
) -> Option<(Candidate, bool, BacktrackFrame)> {
850855
while let Some(mut frame) = backtrack_stack.pop() {
851856
let next = frame.remaining_candidates.next(
@@ -860,13 +865,19 @@ fn find_candidate(
860865
};
861866
// When we're calling this method we know that `parent` failed to
862867
// activate. That means that some dependency failed to get resolved for
863-
// whatever reason, and all of those reasons (plus maybe some extras)
864-
// are listed in `conflicting_activations`.
868+
// whatever reason. Normally, that means that all of those reasons
869+
// (plus maybe some extras) are listed in `conflicting_activations`.
865870
//
866871
// This means that if all members of `conflicting_activations` are still
867872
// active in this back up we know that we're guaranteed to not actually
868873
// make any progress. As a result if we hit this condition we can
869874
// completely skip this backtrack frame and move on to the next.
875+
//
876+
// The abnormal situations are things that do not put all of the reasons in `conflicting_activations`:
877+
// If we backtracked we do not know how our `conflicting_activations` related to
878+
// the cause of that backtrack, so we do not update it.
879+
// If we had a PublicDependency conflict, then we do not yet have a compact way to
880+
// represent all the parts of the problem, so `conflicting_activations` is incomplete.
870881
if !backtracked
871882
&& !conflicting_activations
872883
.values()

src/cargo/core/resolver/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl ConflictReason {
425425
}
426426
}
427427

428-
pub type Conflict = BTreeMap<PackageId, ConflictReason>;
428+
pub type ConflictMap = BTreeMap<PackageId, ConflictReason>;
429429

430430
pub struct RcVecIter<T> {
431431
vec: Rc<Vec<T>>,

0 commit comments

Comments
 (0)