Skip to content

Commit be4bb61

Browse files
committed
Auto merge of #10341 - hi-rustin:rustin-patch-doc-deps, r=ehuss
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.
2 parents 25fcb13 + bd45ac8 commit be4bb61

File tree

2 files changed

+85
-32
lines changed

2 files changed

+85
-32
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use crate::core::compiler::unit_graph::{UnitDep, UnitGraph};
1919
use crate::core::compiler::UnitInterner;
2020
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
21-
use crate::core::dependency::DepKind;
2221
use crate::core::profiles::{Profile, Profiles, UnitFor};
2322
use crate::core::resolver::features::{FeaturesFor, ResolvedFeatures};
2423
use crate::core::resolver::Resolve;
@@ -243,29 +242,7 @@ fn compute_deps(
243242
}
244243

245244
let id = unit.pkg.package_id();
246-
let filtered_deps = state.deps(unit, unit_for, &|dep| {
247-
// If this target is a build command, then we only want build
248-
// dependencies, otherwise we want everything *other than* build
249-
// dependencies.
250-
if unit.target.is_custom_build() != dep.is_build() {
251-
return false;
252-
}
253-
254-
// If this dependency is **not** a transitive dependency, then it
255-
// only applies to test/example targets.
256-
if !dep.is_transitive()
257-
&& !unit.target.is_test()
258-
&& !unit.target.is_example()
259-
&& !unit.mode.is_doc_scrape()
260-
&& !unit.mode.is_any_test()
261-
{
262-
return false;
263-
}
264-
265-
// If we've gotten past all that, then this dependency is
266-
// actually used!
267-
true
268-
});
245+
let filtered_deps = state.deps(unit, unit_for);
269246

270247
let mut ret = Vec::new();
271248
let mut dev_deps = Vec::new();
@@ -423,7 +400,7 @@ fn compute_deps_doc(
423400
state: &mut State<'_, '_>,
424401
unit_for: UnitFor,
425402
) -> CargoResult<Vec<UnitDep>> {
426-
let deps = state.deps(unit, unit_for, &|dep| dep.kind() == DepKind::Normal);
403+
let deps = state.deps(unit, unit_for);
427404

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

833810
/// Returns a filtered set of dependencies for the given unit.
834-
fn deps(
835-
&self,
836-
unit: &Unit,
837-
unit_for: UnitFor,
838-
filter: &dyn Fn(&Dependency) -> bool,
839-
) -> Vec<(PackageId, &HashSet<Dependency>)> {
811+
fn deps(&self, unit: &Unit, unit_for: UnitFor) -> Vec<(PackageId, &HashSet<Dependency>)> {
840812
let pkg_id = unit.pkg.package_id();
841813
let kind = unit.kind;
842814
self.resolve()
843815
.deps(pkg_id)
844816
.filter(|&(_id, deps)| {
845817
assert!(!deps.is_empty());
846818
deps.iter().any(|dep| {
847-
if !filter(dep) {
819+
// If this target is a build command, then we only want build
820+
// dependencies, otherwise we want everything *other than* build
821+
// dependencies.
822+
if unit.target.is_custom_build() != dep.is_build() {
848823
return false;
849824
}
825+
826+
// If this dependency is **not** a transitive dependency, then it
827+
// only applies to test/example targets.
828+
if !dep.is_transitive()
829+
&& !unit.target.is_test()
830+
&& !unit.target.is_example()
831+
&& !unit.mode.is_doc_scrape()
832+
&& !unit.mode.is_any_test()
833+
{
834+
return false;
835+
}
836+
850837
// If this dependency is only available for certain platforms,
851838
// make sure we're only enabling it for that platform.
852839
if !self.target_data.dep_platform_activated(dep, kind) {
@@ -862,6 +849,8 @@ impl<'a, 'cfg> State<'a, 'cfg> {
862849
}
863850
}
864851

852+
// If we've gotten past all that, then this dependency is
853+
// actually used!
865854
true
866855
})
867856
})

tests/testsuite/doc.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,70 @@ fn doc_example() {
18151815
.exists());
18161816
}
18171817

1818+
#[cargo_test]
1819+
fn doc_example_with_deps() {
1820+
let p = project()
1821+
.file(
1822+
"Cargo.toml",
1823+
r#"
1824+
[package]
1825+
name = "foo"
1826+
version = "0.1.0"
1827+
edition = "2018"
1828+
1829+
[[example]]
1830+
crate-type = ["lib"]
1831+
name = "ex"
1832+
doc = true
1833+
1834+
[dev-dependencies]
1835+
a = {path = "a"}
1836+
b = {path = "b"}
1837+
"#,
1838+
)
1839+
.file("src/lib.rs", "")
1840+
.file(
1841+
"examples/ex.rs",
1842+
r#"
1843+
use a::fun;
1844+
1845+
/// Example
1846+
pub fn x() { fun(); }
1847+
"#,
1848+
)
1849+
.file(
1850+
"a/Cargo.toml",
1851+
r#"
1852+
[package]
1853+
name = "a"
1854+
version = "0.0.1"
1855+
1856+
[dependencies]
1857+
b = {path = "../b"}
1858+
"#,
1859+
)
1860+
.file("a/src/fun.rs", "pub fn fun() {}")
1861+
.file("a/src/lib.rs", "pub mod fun;")
1862+
.file(
1863+
"b/Cargo.toml",
1864+
r#"
1865+
[package]
1866+
name = "b"
1867+
version = "0.0.1"
1868+
"#,
1869+
)
1870+
.file("b/src/lib.rs", "")
1871+
.build();
1872+
1873+
p.cargo("doc --examples").run();
1874+
assert!(p
1875+
.build_dir()
1876+
.join("doc")
1877+
.join("ex")
1878+
.join("fn.x.html")
1879+
.exists());
1880+
}
1881+
18181882
#[cargo_test]
18191883
fn bin_private_items() {
18201884
let p = project()

0 commit comments

Comments
 (0)