Skip to content

Commit ee8eade

Browse files
committed
only have one place we push to backtrack_stack
1 parent b9f44ce commit ee8eade

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

src/cargo/core/resolver/mod.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,12 @@ impl RemainingCandidates {
595595
///
596596
/// If all dependencies can be activated and resolved to a version in the
597597
/// dependency graph, cx.resolve is returned.
598-
fn activate_deps_loop<'a>(mut cx: Context<'a>,
599-
registry: &mut Registry,
600-
summaries: &[(Summary, Method)],
601-
config: Option<&Config>)
602-
-> CargoResult<Context<'a>> {
598+
fn activate_deps_loop<'a>(
599+
mut cx: Context<'a>,
600+
registry: &mut Registry,
601+
summaries: &[(Summary, Method)],
602+
config: Option<&Config>,
603+
) -> CargoResult<Context<'a>> {
603604
// Note that a `BinaryHeap` is used for the remaining dependencies that need
604605
// activation. This heap is sorted such that the "largest value" is the most
605606
// constrained dependency, or the one with the least candidates.
@@ -611,7 +612,10 @@ fn activate_deps_loop<'a>(mut cx: Context<'a>,
611612
let mut remaining_deps = BinaryHeap::new();
612613
for &(ref summary, ref method) in summaries {
613614
debug!("initial activation: {}", summary.package_id());
614-
let candidate = Candidate { summary: summary.clone(), replace: None };
615+
let candidate = Candidate {
616+
summary: summary.clone(),
617+
replace: None,
618+
};
615619
let res = activate(&mut cx, registry, None, candidate, method)?;
616620
if let Some((frame, _)) = res {
617621
remaining_deps.push(frame);
@@ -638,7 +642,6 @@ fn activate_deps_loop<'a>(mut cx: Context<'a>,
638642
// backtracking states where if we hit an error we can return to in order to
639643
// attempt to continue resolving.
640644
while let Some(mut deps_frame) = remaining_deps.pop() {
641-
642645
// If we spend a lot of time here (we shouldn't in most cases) then give
643646
// a bit of a visual indicator as to what we're doing. Only enable this
644647
// when stderr is a tty (a human is likely to be watching) to ensure we
@@ -650,10 +653,8 @@ fn activate_deps_loop<'a>(mut cx: Context<'a>,
650653
// to amortize the cost of the current time lookup.
651654
ticks += 1;
652655
if let Some(config) = config {
653-
if config.shell().is_err_tty() &&
654-
!printed &&
655-
ticks % 1000 == 0 &&
656-
start.elapsed() - deps_time > time_to_print
656+
if config.shell().is_err_tty() && !printed && ticks % 1000 == 0
657+
&& start.elapsed() - deps_time > time_to_print
657658
{
658659
printed = true;
659660
config.shell().status("Resolving", "dependency graph...")?;
@@ -688,54 +689,55 @@ fn activate_deps_loop<'a>(mut cx: Context<'a>,
688689
// This means that we're going to attempt to activate each candidate in
689690
// turn. We could possibly fail to activate each candidate, so we try
690691
// each one in turn.
691-
let candidate = match next {
692-
Ok((candidate, has_another)) => {
693-
// We have a candidate. Add an entry to the `backtrack_stack` so
694-
// we can try the next one if this one fails.
695-
if has_another {
696-
backtrack_stack.push(BacktrackFrame {
697-
cur,
698-
context_backup: Context::clone(&cx),
699-
deps_backup: <BinaryHeap<DepsFrame>>::clone(&remaining_deps),
700-
remaining_candidates,
701-
parent: Summary::clone(&parent),
702-
dep: Dependency::clone(&dep),
703-
features: Rc::clone(&features),
704-
});
705-
}
706-
candidate
707-
}
708-
Err(mut conflicting) => {
709-
// This dependency has no valid candidate. Backtrack until we
710-
// find a dependency that does have a candidate to try, and try
711-
// to activate that one. This resets the `remaining_deps` to
712-
// their state at the found level of the `backtrack_stack`.
713-
trace!("{}[{}]>{} -- no candidates", parent.name(), cur,
714-
dep.name());
715-
match find_candidate(&mut backtrack_stack,
716-
&mut cx,
717-
&mut remaining_deps,
718-
&mut parent,
719-
&mut cur,
720-
&mut dep,
721-
&mut features,
722-
&mut conflicting) {
723-
None => return Err(activation_error(&cx, registry, &parent,
724-
&dep,
725-
conflicting,
726-
&candidates, config)),
727-
Some(candidate) => candidate,
728-
}
729-
}
730-
};
692+
let (candidate, has_another) = next.or_else(|mut conflicting| {
693+
// This dependency has no valid candidate. Backtrack until we
694+
// find a dependency that does have a candidate to try, and try
695+
// to activate that one. This resets the `remaining_deps` to
696+
// their state at the found level of the `backtrack_stack`.
697+
trace!("{}[{}]>{} -- no candidates", parent.name(), cur, dep.name());
698+
find_candidate(
699+
&mut backtrack_stack,
700+
&mut cx,
701+
&mut remaining_deps,
702+
&mut parent,
703+
&mut cur,
704+
&mut dep,
705+
&mut features,
706+
&mut remaining_candidates,
707+
&mut conflicting,
708+
).ok_or_else(|| {
709+
activation_error(
710+
&cx,
711+
registry,
712+
&parent,
713+
&dep,
714+
conflicting,
715+
&candidates,
716+
config,
717+
)
718+
})
719+
})?;
720+
721+
// We have a candidate. Add an entry to the `backtrack_stack` so
722+
// we can try the next one if this one fails.
723+
if has_another {
724+
backtrack_stack.push(BacktrackFrame {
725+
cur,
726+
context_backup: Context::clone(&cx),
727+
deps_backup: <BinaryHeap<DepsFrame>>::clone(&remaining_deps),
728+
remaining_candidates,
729+
parent: Summary::clone(&parent),
730+
dep: Dependency::clone(&dep),
731+
features: Rc::clone(&features),
732+
});
733+
}
731734

732735
let method = Method::Required {
733736
dev_deps: false,
734737
features: &features,
735738
uses_default_features: dep.uses_default_features(),
736739
};
737-
trace!("{}[{}]>{} trying {}", parent.name(), cur, dep.name(),
738-
candidate.summary.version());
740+
trace!("{}[{}]>{} trying {}", parent.name(), cur, dep.name(), candidate.summary.version());
739741
let res = activate(&mut cx, registry, Some(&parent), candidate, &method)?;
740742
if let Some((frame, dur)) = res {
741743
remaining_deps.push(frame);
@@ -765,8 +767,9 @@ fn find_candidate<'a>(
765767
cur: &mut usize,
766768
dep: &mut Dependency,
767769
features: &mut Rc<Vec<String>>,
770+
remaining_candidates: &mut RemainingCandidates,
768771
conflicting_activations: &mut HashSet<PackageId>,
769-
) -> Option<Candidate> {
772+
) -> Option<(Candidate, bool)> {
770773
while let Some(mut frame) = backtrack_stack.pop() {
771774
let next= frame.remaining_candidates.next(frame.context_backup.prev_active(&frame.dep));
772775
if frame.context_backup.is_active(parent.package_id())
@@ -778,16 +781,14 @@ fn find_candidate<'a>(
778781
continue;
779782
}
780783
if let Ok((candidate, has_another)) = next {
781-
if has_another {
782-
backtrack_stack.push(frame.clone());
783-
}
784784
*cur = frame.cur;
785785
*cx = frame.context_backup;
786786
*remaining_deps = frame.deps_backup;
787787
*parent = frame.parent;
788788
*dep = frame.dep;
789789
*features = frame.features;
790-
return Some(candidate);
790+
*remaining_candidates = frame.remaining_candidates;
791+
return Some((candidate, has_another));
791792
}
792793
}
793794
None

0 commit comments

Comments
 (0)