Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return incompatibility from conflict & Allow backtracking to before a specific package #36

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions src/internal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::collections::HashSet as Set;
use std::sync::Arc;

use crate::internal::{
Arena, DecisionLevel, HashArena, Id, IncompDpId, Incompatibility, PartialSolution, Relation,
SatisfierSearch, SmallVec,
Arena, DecisionLevel, HashArena, Id, IncompDpId, IncompatIterItem, Incompatibility,
PartialSolution, Relation, SatisfierSearch, SmallVec,
};
use crate::{DependencyProvider, DerivationTree, Map, NoSolutionError, VersionSet};

Expand Down Expand Up @@ -74,20 +74,24 @@ impl<DP: DependencyProvider> State<DP> {
}

/// Add the dependencies for the current version of the current package as incompatibilities.
///
/// Returns the incompatibility that caused the current version to be rejected, if it was
/// rejected due to its dependencies.
pub fn add_package_version_dependencies(
&mut self,
package: Id<DP::P>,
version: DP::V,
dependencies: impl IntoIterator<Item = (DP::P, DP::VS)>,
) {
) -> Option<impl Iterator<Item = IncompatIterItem<'_, DP::P, DP::VS>>> {
let dep_incompats =
self.add_incompatibility_from_dependencies(package, version.clone(), dependencies);
self.partial_solution.add_package_version_incompatibilities(
package,
version.clone(),
dep_incompats,
&self.incompatibility_store,
)
self.partial_solution
.check_package_version_incompatibilities(
package,
version.clone(),
dep_incompats,
&self.incompatibility_store,
)
}

/// Add an incompatibility to the state.
Expand Down Expand Up @@ -124,8 +128,18 @@ impl<DP: DependencyProvider> State<DP> {

/// Unit propagation is the core mechanism of the solving algorithm.
/// CF <https://github.com/dart-lang/pub/blob/master/doc/solver.md#unit-propagation>
///
/// Returns the last incompatibility, if there was a conflict.
#[cold]
pub fn unit_propagation(&mut self, package: Id<DP::P>) -> Result<(), NoSolutionError<DP>> {
#[allow(clippy::type_complexity)] // Type definitions don't support impl trait.
pub fn unit_propagation(
&mut self,
package: Id<DP::P>,
) -> Result<
Option<impl Iterator<Item = IncompatIterItem<'_, DP::P, DP::VS>>>,
NoSolutionError<DP>,
> {
let mut last_incompat = None;
self.unit_propagation_buffer.clear();
self.unit_propagation_buffer.push(package);
while let Some(current_package) = self.unit_propagation_buffer.pop() {
Expand Down Expand Up @@ -183,6 +197,7 @@ impl<DP: DependencyProvider> State<DP> {
.map_err(|terminal_incompat_id| {
self.build_derivation_tree(terminal_incompat_id)
})?;
last_incompat = Some(root_cause);
self.unit_propagation_buffer.clear();
self.unit_propagation_buffer.push(package_almost);
// Add to the partial solution with incompat as cause.
Expand All @@ -198,7 +213,7 @@ impl<DP: DependencyProvider> State<DP> {
}
}
// If there are no more changed packages, unit propagation is done.
Ok(())
Ok(last_incompat.map(|incompat| self.incompatibility_store[incompat].iter()))
}

/// Return the root cause or the terminal incompatibility.
Expand Down Expand Up @@ -249,7 +264,8 @@ impl<DP: DependencyProvider> State<DP> {
}
}

/// Backtracking.
/// After a conflict occurred, backtrack the partial solution to a given decision level, and add
/// the incompatibility if it was new.
fn backtrack(
&mut self,
incompat: IncompDpId<DP>,
Expand All @@ -265,6 +281,21 @@ impl<DP: DependencyProvider> State<DP> {
}
}

/// Manually backtrack before the given package was selected.
///
/// This can be used to switch the order of packages if the previous prioritization was bad.
///
/// Returns the number of the decisions that were backtracked, or `None` if the package was not
/// decided on yet.
pub fn backtrack_package(&mut self, package: Id<DP::P>) -> Option<u32> {
let base_decision_level = self.partial_solution.current_decision_level();
let new_decision_level = self.partial_solution.backtrack_package(package).ok()?;
// Remove contradicted incompatibilities that depend on decisions we just backtracked away.
self.contradicted_incompatibilities
.retain(|_, dl| *dl <= new_decision_level);
Some(base_decision_level.0 - new_decision_level.0)
}

/// Add this incompatibility into the set of all incompatibilities.
///
/// PubGrub collapses identical dependencies from adjacent package versions
Expand Down
5 changes: 4 additions & 1 deletion src/internal/incompatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use crate::{
VersionSet,
};

// An entry in an incompatibility when iterating over one.
pub(crate) type IncompatIterItem<'term, P, VS> = (Id<P>, &'term Term<VS>);

/// An incompatibility is a set of terms for different packages
/// that should never be satisfied all together.
/// An incompatibility usually originates from a package dependency.
Expand Down Expand Up @@ -248,7 +251,7 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
}

/// Iterate over packages.
pub(crate) fn iter(&self) -> impl Iterator<Item = (Id<P>, &Term<VS>)> {
pub(crate) fn iter(&self) -> impl Iterator<Item = IncompatIterItem<'_, P, VS>> {
self.package_terms
.iter()
.map(|(package, term)| (*package, term))
Expand Down
2 changes: 1 addition & 1 deletion src/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod small_map;
mod small_vec;

pub(crate) use arena::{Arena, HashArena};
pub(crate) use incompatibility::{IncompDpId, IncompId, Relation};
pub(crate) use incompatibility::{IncompDpId, IncompId, IncompatIterItem, Relation};
pub(crate) use partial_solution::{DecisionLevel, PartialSolution, SatisfierSearch};
pub(crate) use small_map::SmallMap;
pub(crate) use small_vec::SmallVec;
Expand Down
116 changes: 84 additions & 32 deletions src/internal/partial_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use std::fmt::{Debug, Display};
use std::hash::BuildHasherDefault;

use log::debug;
use priority_queue::PriorityQueue;
use rustc_hash::FxHasher;

use crate::internal::{
Arena, HashArena, Id, IncompDpId, IncompId, Incompatibility, Relation, SmallMap, SmallVec,
Arena, HashArena, Id, IncompDpId, IncompId, IncompatIterItem, Incompatibility, Relation,
SmallMap, SmallVec,
};
use crate::{DependencyProvider, Package, Term, VersionSet};

Expand Down Expand Up @@ -210,6 +212,13 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
self.next_global_index += 1;
}

/// The list of package that have not been selected after the last prioritization.
///
/// This list gets updated by [`Self::pick_highest_priority_pkg`] and cleared by backtracking.
pub fn undecided_packages(&self) -> impl Iterator<Item = (&Id<DP::P>, &DP::Priority)> {
self.prioritized_potential_packages.iter()
}

/// Add a derivation.
pub(crate) fn add_derivation(
&mut self,
Expand Down Expand Up @@ -323,7 +332,21 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
.map(|(&p, pa)| match &pa.assignments_intersection {
AssignmentsIntersection::Decision((_, v, _)) => (p, v.clone()),
AssignmentsIntersection::Derivations(_) => {
panic!("Derivations in the Decision part")
let mut context = String::new();
for (id, assignment) in self
.package_assignments
.iter()
.take(self.current_decision_level.0 as usize)
{
context.push_str(&format!(
" * {:?} {:?}\n",
id, assignment.assignments_intersection
));
}
panic!(
"Derivations in the Decision part. Decision level {}\n{}",
self.current_decision_level.0, context
)
}
})
}
Expand Down Expand Up @@ -370,45 +393,74 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
self.has_ever_backtracked = true;
}

/// We can add the version to the partial solution as a decision
/// if it doesn't produce any conflict with the new incompatibilities.
/// In practice I think it can only produce a conflict if one of the dependencies
/// (which are used to make the new incompatibilities)
/// is already in the partial solution with an incompatible version.
pub(crate) fn add_package_version_incompatibilities(
&mut self,
/// Backtrack the partial solution before a particular package was selected.
///
/// This can be used to switch the order of packages if the previous prioritization was bad.
///
/// Returns the new decision level on success and an error if the package was not decided on
/// yet.
pub(crate) fn backtrack_package(&mut self, package: Id<DP::P>) -> Result<DecisionLevel, ()> {
let Some(decision_level) = self.package_assignments.get_index_of(&package) else {
return Err(());
};
let decision_level = DecisionLevel(decision_level as u32);
if decision_level > self.current_decision_level {
return Err(());
}
debug!(
"Package backtracking ot decision level {}",
decision_level.0
);
self.backtrack(decision_level);
Ok(decision_level)
}

/// Add a package version as decision if none of its dependencies conflicts with the partial
/// solution.
///
/// If the resolution never backtracked before, a fast path adds the package version directly
/// without checking dependencies.
///
/// Returns the incompatibility that caused the current version to be rejected, if a conflict
/// in the dependencies was found.
pub(crate) fn check_package_version_incompatibilities<'a>(
&'a mut self,
package: Id<DP::P>,
version: DP::V,
new_incompatibilities: std::ops::Range<IncompId<DP::P, DP::VS, DP::M>>,
store: &Arena<Incompatibility<DP::P, DP::VS, DP::M>>,
) {
store: &'a Arena<Incompatibility<DP::P, DP::VS, DP::M>>,
) -> Option<impl Iterator<Item = IncompatIterItem<'a, DP::P, DP::VS>> + 'a> {
if !self.has_ever_backtracked {
// Nothing has yet gone wrong during this resolution. This call is unlikely to be the first problem.
// Fast path: Nothing has yet gone wrong during this resolution. This call is unlikely to be the first problem.
// So let's live with a little bit of risk and add the decision without checking the dependencies.
// The worst that can happen is we will have to do a full backtrack which only removes this one decision.
log::info!("add_decision: {package:?} @ {version} without checking dependencies");
self.add_decision(package, version);
return None;
}

// Check if any of the dependencies preclude deciding on this crate version.
let package_term = Term::exact(version.clone());
let relation = |incompat: &Incompatibility<DP::P, DP::VS, DP::M>| {
incompat.relation(|p| {
// The current package isn't part of the package assignments yet.
if p == package {
Some(&package_term)
} else {
self.term_intersection_for_package(p)
}
})
};
if let Some(satisfied) = store[new_incompatibilities]
.iter()
.find(|incompat| relation(incompat) == Relation::Satisfied)
{
log::info!("not adding {package:?} @ {version} because its dependencies conflict");
Some(satisfied.iter())
} else {
// Check if any of the new dependencies preclude deciding on this crate version.
let exact = Term::exact(version.clone());
let not_satisfied = |incompat: &Incompatibility<DP::P, DP::VS, DP::M>| {
incompat.relation(|p| {
if p == package {
Some(&exact)
} else {
self.term_intersection_for_package(p)
}
}) != Relation::Satisfied
};

// Check none of the dependencies (new_incompatibilities)
// would create a conflict (be satisfied).
if store[new_incompatibilities].iter().all(not_satisfied) {
log::info!("add_decision: {package:?} @ {version}");
self.add_decision(package, version);
} else {
log::info!("not adding {package:?} @ {version} because of its dependencies",);
}
log::info!("add_decision: {package:?} @ {version}");
self.add_decision(package, version);
None
}
}

Expand Down
Loading