-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathincompatibility.rs
296 lines (268 loc) · 11.5 KB
/
incompatibility.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: MPL-2.0
//! An incompatibility is a set of terms for different packages
//! that should never be satisfied all together.
use std::collections::HashSet as Set;
use std::fmt;
use crate::internal::arena::{Arena, Id};
use crate::internal::small_map::SmallMap;
use crate::package::Package;
use crate::range::Range;
use crate::report::{DefaultStringReporter, DerivationTree, Derived, External};
use crate::term::{self, Term};
use crate::version::Version;
/// 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.
/// For example, if package A at version 1 depends on package B
/// at version 2, you can never have both terms `A = 1`
/// and `not B = 2` satisfied at the same time in a partial solution.
/// This would mean that we found a solution with package A at version 1
/// but not with package B at version 2.
/// Yet A at version 1 depends on B at version 2 so this is not possible.
/// Therefore, the set `{ A = 1, not B = 2 }` is an incompatibility,
/// defined from dependencies of A at version 1.
///
/// Incompatibilities can also be derived from two other incompatibilities
/// during conflict resolution. More about all this in
/// [PubGrub documentation](/~https://github.com/dart-lang/pub/blob/master/doc/solver.md#incompatibility).
#[derive(Debug, Clone)]
pub struct Incompatibility<P: Package, V: Version> {
package_terms: SmallMap<P, Term<V>>,
kind: Kind<P, V>,
}
/// Type alias of unique identifiers for incompatibilities.
pub type IncompId<P, V> = Id<Incompatibility<P, V>>;
#[derive(Debug, Clone)]
enum Kind<P: Package, V: Version> {
/// Initial incompatibility aiming at picking the root package for the first decision.
NotRoot(P, V),
/// There are no versions in the given range for this package.
NoVersions(P, Range<V>),
/// Dependencies of the package are unavailable for versions in that range.
UnavailableDependencies(P, Range<V>),
/// Incompatibility coming from the dependencies of a given package.
FromDependencyOf(P, Range<V>, P, Range<V>),
/// Derived from two causes. Stores cause ids.
DerivedFrom(IncompId<P, V>, IncompId<P, V>),
}
/// A Relation describes how a set of terms can be compared to an incompatibility.
/// Typically, the set of terms comes from the partial solution.
#[derive(Eq, PartialEq, Debug)]
pub enum Relation<P: Package> {
/// We say that a set of terms S satisfies an incompatibility I
/// if S satisfies every term in I.
Satisfied,
/// We say that S contradicts I
/// if S contradicts at least one term in I.
Contradicted(P),
/// If S satisfies all but one of I's terms and is inconclusive for the remaining term,
/// we say S "almost satisfies" I and we call the remaining term the "unsatisfied term".
AlmostSatisfied(P),
/// Otherwise, we say that their relation is inconclusive.
Inconclusive,
}
impl<P: Package, V: Version> Incompatibility<P, V> {
/// Create the initial "not Root" incompatibility.
pub fn not_root(package: P, version: V) -> Self {
Self {
package_terms: SmallMap::One([(
package.clone(),
Term::Negative(Range::exact(version.clone())),
)]),
kind: Kind::NotRoot(package, version),
}
}
/// Create an incompatibility to remember
/// that a given range does not contain any version.
pub fn no_versions(package: P, term: Term<V>) -> Self {
let range = match &term {
Term::Positive(r) => r.clone(),
Term::Negative(_) => panic!("No version should have a positive term"),
};
Self {
package_terms: SmallMap::One([(package.clone(), term)]),
kind: Kind::NoVersions(package, range),
}
}
/// Create an incompatibility to remember
/// that a package version is not selectable
/// because its list of dependencies is unavailable.
pub fn unavailable_dependencies(package: P, version: V) -> Self {
let range = Range::exact(version);
Self {
package_terms: SmallMap::One([(package.clone(), Term::Positive(range.clone()))]),
kind: Kind::UnavailableDependencies(package, range),
}
}
/// Build an incompatibility from a given dependency.
pub fn from_dependency(package: P, version: V, dep: (&P, &Range<V>)) -> Self {
let range1 = Range::exact(version);
let (p2, range2) = dep;
Self {
package_terms: SmallMap::Two([
(package.clone(), Term::Positive(range1.clone())),
(p2.clone(), Term::Negative(range2.clone())),
]),
kind: Kind::FromDependencyOf(package, range1, p2.clone(), range2.clone()),
}
}
/// Prior cause of two incompatibilities using the rule of resolution.
pub fn prior_cause(
incompat: Id<Self>,
satisfier_cause: Id<Self>,
package: &P,
incompatibility_store: &Arena<Self>,
) -> Self {
let kind = Kind::DerivedFrom(incompat, satisfier_cause);
let mut package_terms = incompatibility_store[incompat].package_terms.clone();
let t1 = package_terms.remove(package).unwrap();
let satisfier_cause_terms = &incompatibility_store[satisfier_cause].package_terms;
package_terms.merge(
satisfier_cause_terms.iter().filter(|(p, _)| p != &package),
|t1, t2| Some(t1.intersection(t2)),
);
let term = t1.union(satisfier_cause_terms.get(package).unwrap());
if term != Term::any() {
package_terms.insert(package.clone(), term);
}
Self {
package_terms,
kind,
}
}
/// Check if an incompatibility should mark the end of the algorithm
/// because it satisfies the root package.
pub fn is_terminal(&self, root_package: &P, root_version: &V) -> bool {
if self.package_terms.len() == 0 {
true
} else if self.package_terms.len() > 1 {
false
} else {
let (package, term) = self.package_terms.iter().next().unwrap();
(package == root_package) && term.contains(&root_version)
}
}
/// Get the term related to a given package (if it exists).
pub fn get(&self, package: &P) -> Option<&Term<V>> {
self.package_terms.get(package)
}
/// Iterate over packages.
pub fn iter(&self) -> impl Iterator<Item = (&P, &Term<V>)> {
self.package_terms.iter()
}
// Reporting ###############################################################
/// Retrieve parent causes if of type DerivedFrom.
pub fn causes(&self) -> Option<(Id<Self>, Id<Self>)> {
match self.kind {
Kind::DerivedFrom(id1, id2) => Some((id1, id2)),
_ => None,
}
}
/// Build a derivation tree for error reporting.
pub fn build_derivation_tree(
self_id: Id<Self>,
shared_ids: &Set<Id<Self>>,
store: &Arena<Self>,
) -> DerivationTree<P, V> {
match &store[self_id].kind {
Kind::DerivedFrom(id1, id2) => {
let cause1 = Self::build_derivation_tree(*id1, shared_ids, store);
let cause2 = Self::build_derivation_tree(*id2, shared_ids, store);
let derived = Derived {
terms: store[self_id].package_terms.as_map(),
shared_id: shared_ids.get(&self_id).map(|id| id.into_raw()),
cause1: Box::new(cause1),
cause2: Box::new(cause2),
};
DerivationTree::Derived(derived)
}
Kind::NotRoot(package, version) => {
DerivationTree::External(External::NotRoot(package.clone(), version.clone()))
}
Kind::NoVersions(package, range) => {
DerivationTree::External(External::NoVersions(package.clone(), range.clone()))
}
Kind::UnavailableDependencies(package, range) => DerivationTree::External(
External::UnavailableDependencies(package.clone(), range.clone()),
),
Kind::FromDependencyOf(package, range, dep_package, dep_range) => {
DerivationTree::External(External::FromDependencyOf(
package.clone(),
range.clone(),
dep_package.clone(),
dep_range.clone(),
))
}
}
}
}
impl<'a, P: Package, V: Version + 'a> Incompatibility<P, V> {
/// CF definition of Relation enum.
pub fn relation(&self, terms: impl Fn(&P) -> Option<&'a Term<V>>) -> Relation<P> {
let mut relation = Relation::Satisfied;
for (package, incompat_term) in self.package_terms.iter() {
match terms(package).map(|term| incompat_term.relation_with(&term)) {
Some(term::Relation::Satisfied) => {}
Some(term::Relation::Contradicted) => {
return Relation::Contradicted(package.clone());
}
None | Some(term::Relation::Inconclusive) => {
// If a package is not present, the intersection is the same as [Term::any].
// According to the rules of satisfactions, the relation would be inconclusive.
// It could also be satisfied if the incompatibility term was also [Term::any],
// but we systematically remove those from incompatibilities
// so we're safe on that front.
if relation == Relation::Satisfied {
relation = Relation::AlmostSatisfied(package.clone());
} else {
relation = Relation::Inconclusive;
}
}
}
}
relation
}
}
impl<P: Package, V: Version> fmt::Display for Incompatibility<P, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
DefaultStringReporter::string_terms(&self.package_terms.as_map())
)
}
}
// TESTS #######################################################################
#[cfg(test)]
pub mod tests {
use super::*;
use crate::term::tests::strategy as term_strat;
use crate::type_aliases::Map;
use proptest::prelude::*;
proptest! {
/// For any three different packages p1, p2 and p3,
/// for any three terms t1, t2 and t3,
/// if we have the two following incompatibilities:
/// { p1: t1, p2: not t2 }
/// { p2: t2, p3: t3 }
/// the rule of resolution says that we can deduce the following incompatibility:
/// { p1: t1, p3: t3 }
#[test]
fn rule_of_resolution(t1 in term_strat(), t2 in term_strat(), t3 in term_strat()) {
let mut store = Arena::new();
let i1 = store.alloc(Incompatibility {
package_terms: SmallMap::Two([("p1", t1.clone()), ("p2", t2.negate())]),
kind: Kind::UnavailableDependencies("0", Range::any())
});
let i2 = store.alloc(Incompatibility {
package_terms: SmallMap::Two([("p2", t2), ("p3", t3.clone())]),
kind: Kind::UnavailableDependencies("0", Range::any())
});
let mut i3 = Map::default();
i3.insert("p1", t1);
i3.insert("p3", t3);
let i_resolution = Incompatibility::prior_cause(i1, i2, &"p2", &store);
assert_eq!(i_resolution.package_terms.as_map(), i3);
}
}
}