Skip to content

Commit

Permalink
Add State::add_incompatibility_from_dependencies (#27) (#300)
Browse files Browse the repository at this point in the history
This wrapper avoids accessing the `incompatibility_store` directly in uv
code.

Before:

```rust
let dep_incompats = self.pubgrub.add_version(
    package.clone(),
    version.clone(),
    dependencies,
);
self.pubgrub.partial_solution.add_version(
    package.clone(),
    version.clone(),
    dep_incompats,
    &self.pubgrub.incompatibility_store,
);
```

After:

```rust
self.pubgrub.add_incompatibility_from_dependencies(package.clone(), version.clone(), dependencies);
```

`add_incompatibility_from_dependencies` is one of the main methods for
the custom interface to pubgrub.
  • Loading branch information
konstin authored Dec 20, 2024
1 parent 7767ef2 commit 10cb803
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
21 changes: 19 additions & 2 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, IncompId, Incompatibility, PartialSolution,
Relation, SatisfierSearch, SmallVec,
};
use crate::{DependencyProvider, DerivationTree, Map, NoSolutionError, VersionSet};

Expand Down Expand Up @@ -73,6 +73,23 @@ impl<DP: DependencyProvider> State<DP> {
}
}

/// Add the dependencies for the current version of the current package as incompatibilities.
pub(crate) fn add_package_version_dependencies(
&mut self,
package: Id<DP::P>,
version: DP::V,
dependencies: impl IntoIterator<Item = (DP::P, DP::VS)>,
) -> Option<IncompId<DP::P, DP::VS, DP::M>> {
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,
)
}

/// Add an incompatibility to the state.
pub(crate) fn add_incompatibility(&mut self, incompat: Incompatibility<DP::P, DP::VS, DP::M>) {
let id = self.incompatibility_store.alloc(incompat);
Expand Down
15 changes: 9 additions & 6 deletions src/internal/partial_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,15 @@ 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_version(
/// 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 add_package_version_incompatibilities(
&mut self,
package: Id<DP::P>,
version: DP::V,
Expand Down
12 changes: 3 additions & 9 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,9 @@ pub fn resolve<DP: DependencyProvider>(
};

// Add that package and version if the dependencies are not problematic.
let dep_incompats =
state.add_incompatibility_from_dependencies(p, v.clone(), dependencies);

if let Some(conflict) = state.partial_solution.add_version(
p,
v,
dep_incompats,
&state.incompatibility_store,
) {
if let Some(conflict) =
state.add_package_version_dependencies(p, v.clone(), dependencies)
{
conflict_tracker.entry(p).or_default().dependencies_affected += 1;
for (incompat_package, _) in state.incompatibility_store[conflict].iter() {
if incompat_package == p {
Expand Down

0 comments on commit 10cb803

Please sign in to comment.