Skip to content

Commit 25218d2

Browse files
committed
Simplify filtering
This adds a default filter to `state.deps(…)` making it similar to what's currently in master, while creating another version of it to allow setting a custom filter. This is needed as the default filter won't allow build dependencies, which we need in this particular case. `calc_artifact_deps(…)` now hard-codes the default filter which is needed due to the use of `any` here: https://github.com/rust-lang/cargo/blob/c0e6abe384c2c6282bdd631e2f2a3b092043e6c6/src/cargo/core/compiler/unit_dependencies.rs#L1119 .
1 parent 0cf581e commit 25218d2

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,14 @@ fn compute_deps(
264264

265265
let id = unit.pkg.package_id();
266266

267-
let dep_filter = &non_custom_and_non_transitive_deps;
268-
let filtered_deps = state.deps(unit, unit_for, dep_filter);
267+
let filtered_deps = state.deps(unit, unit_for);
269268

270269
let mut ret = Vec::new();
271270
let mut dev_deps = Vec::new();
272271
for (dep_pkg_id, deps) in filtered_deps {
273272
let dep_pkg = state.get(dep_pkg_id);
274-
let (could_have_non_artifact_lib, has_artifact_lib) = calc_artifact_deps(
275-
unit, unit_for, dep_pkg_id, deps, state, dep_filter, &mut ret,
276-
)?;
273+
let (could_have_non_artifact_lib, has_artifact_lib) =
274+
calc_artifact_deps(unit, unit_for, dep_pkg_id, deps, state, &mut ret)?;
277275

278276
let lib = package_lib(dep_pkg, could_have_non_artifact_lib, has_artifact_lib);
279277
let dep_lib = match lib {
@@ -421,7 +419,6 @@ fn calc_artifact_deps(
421419
dep_id: PackageId,
422420
deps: &HashSet<Dependency>,
423421
state: &State<'_, '_>,
424-
filter: &dyn Fn(&Unit, &Dependency) -> bool,
425422
ret: &mut Vec<UnitDep>,
426423
) -> CargoResult<(bool, bool)> {
427424
let mut has_artifact_lib = false;
@@ -431,7 +428,7 @@ fn calc_artifact_deps(
431428
for (dep, artifact) in deps
432429
.iter()
433430
.filter(|dep| {
434-
if filter(unit, dep) {
431+
if non_custom_and_non_transitive_deps(unit, dep) {
435432
deps_past_filter += 1;
436433
true
437434
} else {
@@ -522,7 +519,7 @@ fn compute_deps_custom_build(
522519
//
523520
// This is essentially the same as `calc_artifact_deps`, but there are some
524521
// subtle differences that require this to be implemented differently.
525-
let artifact_build_deps = state.deps(unit, script_unit_for, &|_unit, dep| {
522+
let artifact_build_deps = state.deps_filtered(unit, script_unit_for, &|_unit, dep| {
526523
dep.kind() == DepKind::Build && dep.artifact().is_some()
527524
});
528525

@@ -666,16 +663,15 @@ fn compute_deps_doc(
666663
state: &mut State<'_, '_>,
667664
unit_for: UnitFor,
668665
) -> CargoResult<Vec<UnitDep>> {
669-
let dep_filter = &non_custom_and_non_transitive_deps;
670-
let deps = state.deps(unit, unit_for, dep_filter);
666+
let deps = state.deps(unit, unit_for);
671667

672668
// To document a library, we depend on dependencies actually being
673669
// built. If we're documenting *all* libraries, then we also depend on
674670
// the documentation of the library being built.
675671
let mut ret = Vec::new();
676672
for (id, deps) in deps {
677673
let (could_have_non_artifact_lib, has_artifact_lib) =
678-
calc_artifact_deps(unit, unit_for, id, deps, state, dep_filter, &mut ret)?;
674+
calc_artifact_deps(unit, unit_for, id, deps, state, &mut ret)?;
679675

680676
let dep_pkg = state.get(id);
681677
let lib = package_lib(dep_pkg, could_have_non_artifact_lib, has_artifact_lib);
@@ -1102,8 +1098,13 @@ impl<'a, 'cfg> State<'a, 'cfg> {
11021098
.unwrap_or_else(|_| panic!("expected {} to be downloaded", id))
11031099
}
11041100

1101+
/// Returns a set of dependencies for the given unit, with a default filter.
1102+
fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, &HashSet<Dependency>)> {
1103+
self.deps_filtered(unit, unit_for, &non_custom_and_non_transitive_deps)
1104+
}
1105+
11051106
/// Returns a filtered set of dependencies for the given unit.
1106-
fn deps(
1107+
fn deps_filtered(
11071108
&self,
11081109
unit: &Unit,
11091110
unit_for: UnitFor,

0 commit comments

Comments
 (0)