Skip to content

Commit 32cf756

Browse files
committed
Auto merge of #6995 - Eh2406:new-test-is-worng, r=@alexcrichton
the testing SAT solver was messed up by a refactor This fixes a mistake in #6980 introduced in [this commit](c68334f#diff-4317936c037e49f70800a86656c67569L308). This also reverts [8458661](8458661) with some test cases to show that it was wrong. This only causes problems when proptest is set to make public dependencies (witch is not true on master) and it gens a `reverse_alphabetical` example. Despite the low impact of these bugs, I would like it to be left incorrect as short as possible.
2 parents 9be4594 + 3052e59 commit 32cf756

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/cargo/core/resolver/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub type Activations =
5757
/// A type that represents when cargo treats two Versions as compatible.
5858
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
5959
/// same.
60-
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
60+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
6161
pub enum SemverCompatibility {
6262
Major(NonZeroU64),
6363
Minor(NonZeroU64),

tests/testsuite/resolve.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn public_dependency_skipping() {
357357
];
358358
let reg = registry(input);
359359

360-
resolve(pkg_id("root"), vec![dep("c")], &reg).unwrap();
360+
resolve_and_validated(pkg_id("root"), vec![dep("c")], &reg, None).unwrap();
361361
}
362362

363363
#[test]
@@ -377,7 +377,50 @@ fn public_dependency_skipping_in_backtracking() {
377377
];
378378
let reg = registry(input);
379379

380-
resolve(pkg_id("root"), vec![dep("C")], &reg).unwrap();
380+
resolve_and_validated(pkg_id("root"), vec![dep("C")], &reg, None).unwrap();
381+
}
382+
383+
#[test]
384+
fn public_sat_topological_order() {
385+
let input = vec![
386+
pkg!(("a", "0.0.1")),
387+
pkg!(("a", "0.0.0")),
388+
pkg!(("b", "0.0.1") => [dep_req_kind("a", "= 0.0.1", Kind::Normal, true),]),
389+
pkg!(("b", "0.0.0") => [dep("bad"),]),
390+
pkg!("A" => [dep_req("a", "= 0.0.0"),dep_req_kind("b", "*", Kind::Normal, true)]),
391+
];
392+
393+
let reg = registry(input);
394+
assert!(resolve_and_validated(pkg_id("root"), vec![dep("A")], &reg, None).is_err());
395+
}
396+
397+
#[test]
398+
fn public_sat_unused_makes_things_pub() {
399+
let input = vec![
400+
pkg!(("a", "0.0.1")),
401+
pkg!(("a", "0.0.0")),
402+
pkg!(("b", "8.0.1") => [dep_req_kind("a", "= 0.0.1", Kind::Normal, true),]),
403+
pkg!(("b", "8.0.0") => [dep_req("a", "= 0.0.1"),]),
404+
pkg!("c" => [dep_req("b", "= 8.0.0"),dep_req("a", "= 0.0.0"),]),
405+
];
406+
let reg = registry(input);
407+
408+
resolve_and_validated(pkg_id("root"), vec![dep("c")], &reg, None).unwrap();
409+
}
410+
411+
#[test]
412+
fn public_sat_unused_makes_things_pub_2() {
413+
let input = vec![
414+
pkg!(("c", "0.0.2")),
415+
pkg!(("c", "0.0.1")),
416+
pkg!(("a-sys", "0.0.2")),
417+
pkg!(("a-sys", "0.0.1") => [dep_req_kind("c", "= 0.0.1", Kind::Normal, true),]),
418+
pkg!("P" => [dep_req_kind("a-sys", "*", Kind::Normal, true),dep_req("c", "= 0.0.1"),]),
419+
pkg!("A" => [dep("P"),dep_req("c", "= 0.0.2"),]),
420+
];
421+
let reg = registry(input);
422+
423+
resolve_and_validated(pkg_id("root"), vec![dep("A")], &reg, None).unwrap();
381424
}
382425

383426
#[test]

tests/testsuite/support/resolver.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ fn sat_at_most_one(solver: &mut impl varisat::ExtendFormula, vars: &[varisat::Va
203203
return;
204204
} else if vars.len() == 2 {
205205
solver.add_clause(&[vars[0].negative(), vars[1].negative()]);
206+
return;
206207
} else if vars.len() == 3 {
207208
solver.add_clause(&[vars[0].negative(), vars[1].negative()]);
208209
solver.add_clause(&[vars[0].negative(), vars[2].negative()]);
209210
solver.add_clause(&[vars[1].negative(), vars[2].negative()]);
211+
return;
210212
}
211213
// use the "Binary Encoding" from
212214
// https://www.it.uu.se/research/group/astra/ModRef10/papers/Alan%20M.%20Frisch%20and%20Paul%20A.%20Giannoros.%20SAT%20Encodings%20of%20the%20At-Most-k%20Constraint%20-%20ModRef%202010.pdf
@@ -305,6 +307,7 @@ impl SatResolve {
305307
.iter()
306308
.filter(|&p| dep.matches_id(*p))
307309
{
310+
graph.link(p.package_id(), m);
308311
by_key
309312
.entry(m.as_activations_key())
310313
.or_default()
@@ -374,12 +377,12 @@ impl SatResolve {
374377
// we already ensure there is only one version for each `activations_key` so we can think of
375378
// `can_see` as being in terms of a set of `activations_key`s
376379
// and if `p` `publicly_exports` `export` then it `can_see` `export`
377-
let mut can_see: HashMap<_, HashMap<_, varisat::Var>> = publicly_exports.clone();
380+
let mut can_see: HashMap<_, HashMap<_, varisat::Var>> = HashMap::new();
378381

379382
// if `p` has a `dep` that selected `ver` then it `can_see` all the things that the selected version `publicly_exports`
380383
for (&p, deps) in version_selected_for.iter() {
381-
let p_can_see = can_see.entry(p.as_activations_key()).or_default();
382-
for (_, versions) in deps.iter().filter(|(d, _)| !d.is_public()) {
384+
let p_can_see = can_see.entry(p).or_default();
385+
for (_, versions) in deps.iter() {
383386
for (&ver, sel) in versions {
384387
for (&export_pid, &export_var) in publicly_exports[&ver].iter() {
385388
let our_var = p_can_see.entry(export_pid).or_insert_with(|| cnf.new_var());

0 commit comments

Comments
 (0)