Skip to content

Commit 034c590

Browse files
committed
check that the SAT solver exempts the result from the resolver
1 parent f203dea commit 034c590

File tree

2 files changed

+119
-64
lines changed

2 files changed

+119
-64
lines changed

crates/resolver-tests/src/lib.rs

Lines changed: 117 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::cell::RefCell;
12
use std::cmp::PartialEq;
23
use std::cmp::{max, min};
34
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
45
use std::fmt;
6+
use std::fmt::Write;
7+
use std::rc::Rc;
58
use std::time::Instant;
69

710
use cargo::core::dependency::Kind;
@@ -24,66 +27,80 @@ pub fn resolve(deps: Vec<Dependency>, registry: &[Summary]) -> CargoResult<Vec<P
2427
pub fn resolve_and_validated(
2528
deps: Vec<Dependency>,
2629
registry: &[Summary],
27-
sat_resolve: Option<&mut SatResolve>,
30+
sat_resolve: Option<SatResolve>,
2831
) -> CargoResult<Vec<PackageId>> {
29-
let should_resolve = if let Some(sat) = sat_resolve {
30-
sat.sat_resolve(&deps)
31-
} else {
32-
SatResolve::new(registry).sat_resolve(&deps)
33-
};
34-
let resolve = resolve_with_config_raw(deps, registry, None);
35-
assert_eq!(resolve.is_ok(), should_resolve);
36-
37-
let resolve = resolve?;
38-
let mut stack = vec![pkg_id("root")];
39-
let mut used = HashSet::new();
40-
let mut links = HashSet::new();
41-
while let Some(p) = stack.pop() {
42-
assert!(resolve.contains(&p));
43-
if used.insert(p) {
44-
// in the tests all `links` crates end in `-sys`
45-
if p.name().ends_with("-sys") {
46-
assert!(links.insert(p.name()));
32+
let resolve = resolve_with_config_raw(deps.clone(), registry, None);
33+
34+
match resolve {
35+
Err(e) => {
36+
let sat_resolve = sat_resolve.unwrap_or_else(|| SatResolve::new(registry));
37+
if sat_resolve.sat_resolve(&deps) {
38+
panic!(
39+
"the resolve err but the sat_resolve thinks this will work:\n{}",
40+
sat_resolve.use_packages().unwrap()
41+
);
4742
}
48-
stack.extend(resolve.deps(p).map(|(dp, deps)| {
49-
for d in deps {
50-
assert!(d.matches_id(dp));
51-
}
52-
dp
53-
}));
43+
Err(e)
5444
}
55-
}
56-
let out = resolve.sort();
57-
assert_eq!(out.len(), used.len());
58-
59-
let mut pub_deps: HashMap<PackageId, HashSet<_>> = HashMap::new();
60-
for &p in out.iter() {
61-
// make the list of `p` public dependencies
62-
let mut self_pub_dep = HashSet::new();
63-
self_pub_dep.insert(p);
64-
for (dp, deps) in resolve.deps(p) {
65-
if deps.iter().any(|d| d.is_public()) {
66-
self_pub_dep.extend(pub_deps[&dp].iter().cloned())
45+
Ok(resolve) => {
46+
let mut stack = vec![pkg_id("root")];
47+
let mut used = HashSet::new();
48+
let mut links = HashSet::new();
49+
while let Some(p) = stack.pop() {
50+
assert!(resolve.contains(&p));
51+
if used.insert(p) {
52+
// in the tests all `links` crates end in `-sys`
53+
if p.name().ends_with("-sys") {
54+
assert!(links.insert(p.name()));
55+
}
56+
stack.extend(resolve.deps(p).map(|(dp, deps)| {
57+
for d in deps {
58+
assert!(d.matches_id(dp));
59+
}
60+
dp
61+
}));
62+
}
6763
}
68-
}
69-
pub_deps.insert(p, self_pub_dep);
64+
let out = resolve.sort();
65+
assert_eq!(out.len(), used.len());
66+
67+
let mut pub_deps: HashMap<PackageId, HashSet<_>> = HashMap::new();
68+
for &p in out.iter() {
69+
// make the list of `p` public dependencies
70+
let mut self_pub_dep = HashSet::new();
71+
self_pub_dep.insert(p);
72+
for (dp, deps) in resolve.deps(p) {
73+
if deps.iter().any(|d| d.is_public()) {
74+
self_pub_dep.extend(pub_deps[&dp].iter().cloned())
75+
}
76+
}
77+
pub_deps.insert(p, self_pub_dep);
7078

71-
// check if `p` has a public dependencies conflicts
72-
let seen_dep: BTreeSet<_> = resolve
73-
.deps(p)
74-
.flat_map(|(dp, _)| pub_deps[&dp].iter().cloned())
75-
.collect();
76-
let seen_dep: Vec<_> = seen_dep.iter().collect();
77-
for a in seen_dep.windows(2) {
78-
if a[0].name() == a[1].name() {
79+
// check if `p` has a public dependencies conflicts
80+
let seen_dep: BTreeSet<_> = resolve
81+
.deps(p)
82+
.flat_map(|(dp, _)| pub_deps[&dp].iter().cloned())
83+
.collect();
84+
let seen_dep: Vec<_> = seen_dep.iter().collect();
85+
for a in seen_dep.windows(2) {
86+
if a[0].name() == a[1].name() {
87+
panic!(
88+
"the package {:?} can publicly see {:?} and {:?}",
89+
p, a[0], a[1]
90+
)
91+
}
92+
}
93+
}
94+
let sat_resolve = sat_resolve.unwrap_or_else(|| SatResolve::new(registry));
95+
if !sat_resolve.sat_is_valid_solution(&out) {
7996
panic!(
80-
"the package {:?} can publicly see {:?} and {:?}",
81-
p, a[0], a[1]
82-
)
97+
"the sat_resolve err but the resolve thinks this will work:\n{:?}",
98+
resolve
99+
);
83100
}
101+
Ok(out)
84102
}
85103
}
86-
Ok(out)
87104
}
88105

89106
pub fn resolve_with_config(
@@ -234,7 +251,9 @@ fn sat_at_most_one_by_key<K: std::hash::Hash + Eq>(
234251
///
235252
/// The SAT library dose not optimize for the newer version,
236253
/// so the selected packages may not match the real resolver.
237-
pub struct SatResolve {
254+
#[derive(Clone)]
255+
pub struct SatResolve(Rc<RefCell<SatResolveInner>>);
256+
struct SatResolveInner {
238257
solver: varisat::Solver<'static>,
239258
var_for_is_packages_used: HashMap<PackageId, varisat::Var>,
240259
by_name: HashMap<&'static str, Vec<PackageId>>,
@@ -397,50 +416,86 @@ impl SatResolve {
397416
solver
398417
.solve()
399418
.expect("docs say it can't error in default config");
400-
401-
SatResolve {
419+
SatResolve(Rc::new(RefCell::new(SatResolveInner {
402420
solver,
403421
var_for_is_packages_used,
404422
by_name,
405-
}
423+
})))
406424
}
407-
pub fn sat_resolve(&mut self, deps: &[Dependency]) -> bool {
425+
pub fn sat_resolve(&self, deps: &[Dependency]) -> bool {
426+
let mut s = self.0.borrow_mut();
408427
let mut assumption = vec![];
409428
let mut this_call = None;
410429

411430
// the starting `deps` need to be satisfied
412431
for dep in deps.iter() {
413432
let empty_vec = vec![];
414-
let matches: Vec<varisat::Lit> = self
433+
let matches: Vec<varisat::Lit> = s
415434
.by_name
416435
.get(dep.package_name().as_str())
417436
.unwrap_or(&empty_vec)
418437
.iter()
419438
.filter(|&p| dep.matches_id(*p))
420-
.map(|p| self.var_for_is_packages_used[p].positive())
439+
.map(|p| s.var_for_is_packages_used[p].positive())
421440
.collect();
422441
if matches.is_empty() {
423442
return false;
424443
} else if matches.len() == 1 {
425444
assumption.extend_from_slice(&matches)
426445
} else {
427446
if this_call.is_none() {
428-
let new_var = self.solver.new_var();
447+
let new_var = s.solver.new_var();
429448
this_call = Some(new_var);
430449
assumption.push(new_var.positive());
431450
}
432451
let mut matches = matches;
433452
matches.push(this_call.unwrap().negative());
434-
self.solver.add_clause(&matches);
453+
s.solver.add_clause(&matches);
454+
}
455+
}
456+
457+
s.solver.assume(&assumption);
458+
459+
s.solver
460+
.solve()
461+
.expect("docs say it can't error in default config")
462+
}
463+
pub fn sat_is_valid_solution(&self, pids: &[PackageId]) -> bool {
464+
let mut s = self.0.borrow_mut();
465+
for p in pids {
466+
if p.name().as_str() != "root" && !s.var_for_is_packages_used.contains_key(p) {
467+
return false;
435468
}
436469
}
470+
let assumption: Vec<_> = s
471+
.var_for_is_packages_used
472+
.iter()
473+
.map(|(p, v)| v.lit(pids.contains(p)))
474+
.collect();
437475

438-
self.solver.assume(&assumption);
476+
s.solver.assume(&assumption);
439477

440-
self.solver
478+
s.solver
441479
.solve()
442480
.expect("docs say it can't error in default config")
443481
}
482+
fn use_packages(&self) -> Option<String> {
483+
self.0.borrow().solver.model().map(|lits| {
484+
let lits: HashSet<_> = lits
485+
.iter()
486+
.filter(|l| l.is_positive())
487+
.map(|l| l.var())
488+
.collect();
489+
let mut out = String::new();
490+
out.push_str("used:\n");
491+
for (p, v) in self.0.borrow().var_for_is_packages_used.iter() {
492+
if lits.contains(v) {
493+
writeln!(&mut out, " {}", p).unwrap();
494+
}
495+
}
496+
out
497+
})
498+
}
444499
}
445500

446501
pub trait ToDep {

crates/resolver-tests/tests/resolve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ proptest! {
3939
PrettyPrintRegistry(input) in registry_strategy(50, 20, 60)
4040
) {
4141
let reg = registry(input.clone());
42-
let mut sat_resolve = SatResolve::new(&reg);
42+
let sat_resolve = SatResolve::new(&reg);
4343
// there is only a small chance that any one
4444
// crate will be interesting.
4545
// So we try some of the most complicated.
4646
for this in input.iter().rev().take(20) {
4747
let _ = resolve_and_validated(
4848
vec![dep_req(&this.name(), &format!("={}", this.version()))],
4949
&reg,
50-
Some(&mut sat_resolve),
50+
Some(sat_resolve.clone()),
5151
);
5252
}
5353
}

0 commit comments

Comments
 (0)