Skip to content

Commit

Permalink
Auto merge of #10341 - hi-rustin:rustin-patch-doc-deps, r=ehuss
Browse files Browse the repository at this point in the history
Compute non custom build and non transitive deps for doc

### What does this PR try to resolve?

close #10318 and close #9198
### How should we test and review this PR?

Compute non custom build and non transitive deps for doc.
Add test for it.
  • Loading branch information
bors committed Feb 3, 2022
2 parents 25fcb13 + bd45ac8 commit be4bb61
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 32 deletions.
53 changes: 21 additions & 32 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
use crate::core::compiler::UnitInterner;
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::dependency::DepKind;
use crate::core::profiles::{Profile, Profiles, UnitFor};
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
use crate::core::resolver::Resolve;
Expand Down Expand Up @@ -243,29 +242,7 @@ fn compute_deps(
}

let id = unit.pkg.package_id();
let filtered_deps = state.deps(unit, unit_for, &|dep| {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
}

// If we've gotten past all that, then this dependency is
// actually used!
true
});
let filtered_deps = state.deps(unit, unit_for);

let mut ret = Vec::new();
let mut dev_deps = Vec::new();
Expand Down Expand Up @@ -423,7 +400,7 @@ fn compute_deps_doc(
state: &mut State<'_, '_>,
unit_for: UnitFor,
) -> CargoResult<Vec<UnitDep>> {
let deps = state.deps(unit, unit_for, &|dep| dep.kind() == DepKind::Normal);
let deps = state.deps(unit, unit_for);

// To document a library, we depend on dependencies actually being
// built. If we're documenting *all* libraries, then we also depend on
Expand Down Expand Up @@ -831,22 +808,32 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}

/// Returns a filtered set of dependencies for the given unit.
fn deps(
&self,
unit: &Unit,
unit_for: UnitFor,
filter: &dyn Fn(&Dependency) -> bool,
) -> Vec<(PackageId, &HashSet<Dependency>)> {
fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, &HashSet<Dependency>)> {
let pkg_id = unit.pkg.package_id();
let kind = unit.kind;
self.resolve()
.deps(pkg_id)
.filter(|&(_id, deps)| {
assert!(!deps.is_empty());
deps.iter().any(|dep| {
if !filter(dep) {
// If this target is a build command, then we only want build
// dependencies, otherwise we want everything *other than* build
// dependencies.
if unit.target.is_custom_build() != dep.is_build() {
return false;
}

// If this dependency is **not** a transitive dependency, then it
// only applies to test/example targets.
if !dep.is_transitive()
&& !unit.target.is_test()
&& !unit.target.is_example()
&& !unit.mode.is_doc_scrape()
&& !unit.mode.is_any_test()
{
return false;
}

// If this dependency is only available for certain platforms,
// make sure we're only enabling it for that platform.
if !self.target_data.dep_platform_activated(dep, kind) {
Expand All @@ -862,6 +849,8 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}
}

// If we've gotten past all that, then this dependency is
// actually used!
true
})
})
Expand Down
64 changes: 64 additions & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,70 @@ fn doc_example() {
.exists());
}

#[cargo_test]
fn doc_example_with_deps() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[[example]]
crate-type = ["lib"]
name = "ex"
doc = true
[dev-dependencies]
a = {path = "a"}
b = {path = "b"}
"#,
)
.file("src/lib.rs", "")
.file(
"examples/ex.rs",
r#"
use a::fun;
/// Example
pub fn x() { fun(); }
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.0.1"
[dependencies]
b = {path = "../b"}
"#,
)
.file("a/src/fun.rs", "pub fn fun() {}")
.file("a/src/lib.rs", "pub mod fun;")
.file(
"b/Cargo.toml",
r#"
[package]
name = "b"
version = "0.0.1"
"#,
)
.file("b/src/lib.rs", "")
.build();

p.cargo("doc --examples").run();
assert!(p
.build_dir()
.join("doc")
.join("ex")
.join("fn.x.html")
.exists());
}

#[cargo_test]
fn bin_private_items() {
let p = project()
Expand Down

0 comments on commit be4bb61

Please sign in to comment.