Skip to content

Commit d4b3a1d

Browse files
committed
Fix panic with cargo doc and new resolver and proc-macros.
1 parent 89dcb2a commit d4b3a1d

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn compute_deps(
225225
return compute_deps_custom_build(unit, unit_for, state);
226226
} else if unit.mode.is_doc() {
227227
// Note: this does not include doc test.
228-
return compute_deps_doc(unit, state);
228+
return compute_deps_doc(unit, state, unit_for);
229229
}
230230

231231
let id = unit.pkg.package_id();
@@ -395,9 +395,13 @@ fn compute_deps_custom_build(
395395
}
396396

397397
/// Returns the dependencies necessary to document a package.
398-
fn compute_deps_doc(unit: &Unit, state: &mut State<'_, '_>) -> CargoResult<Vec<UnitDep>> {
398+
fn compute_deps_doc(
399+
unit: &Unit,
400+
state: &mut State<'_, '_>,
401+
unit_for: UnitFor,
402+
) -> CargoResult<Vec<UnitDep>> {
399403
let deps = state
400-
.deps(unit, UnitFor::new_normal())
404+
.deps(unit, unit_for)
401405
.into_iter()
402406
.filter(|&(_id, deps)| deps.iter().any(|dep| dep.kind() == DepKind::Normal));
403407

@@ -414,7 +418,7 @@ fn compute_deps_doc(unit: &Unit, state: &mut State<'_, '_>) -> CargoResult<Vec<U
414418
// Rustdoc only needs rmeta files for regular dependencies.
415419
// However, for plugins/proc macros, deps should be built like normal.
416420
let mode = check_or_build_mode(unit.mode, lib);
417-
let dep_unit_for = UnitFor::new_normal().with_dependency(unit, lib);
421+
let dep_unit_for = unit_for.with_dependency(unit, lib);
418422
let lib_unit_dep = new_unit_dep(
419423
state,
420424
unit,
@@ -441,11 +445,11 @@ fn compute_deps_doc(unit: &Unit, state: &mut State<'_, '_>) -> CargoResult<Vec<U
441445
}
442446

443447
// Be sure to build/run the build script for documented libraries.
444-
ret.extend(dep_build_script(unit, UnitFor::new_normal(), state)?);
448+
ret.extend(dep_build_script(unit, unit_for, state)?);
445449

446450
// If we document a binary/example, we need the library available.
447451
if unit.target.is_bin() || unit.target.is_example() {
448-
ret.extend(maybe_lib(unit, state, UnitFor::new_normal())?);
452+
ret.extend(maybe_lib(unit, state, unit_for)?);
449453
}
450454
Ok(ret)
451455
}

tests/testsuite/features2.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,3 +2277,50 @@ fn pm_with_int_shared() {
22772277
)
22782278
.run();
22792279
}
2280+
2281+
#[cargo_test]
2282+
fn doc_proc_macro() {
2283+
// Checks for a bug when documenting a proc-macro with a dependency. The
2284+
// doc unit builder was not carrying the "for host" setting through the
2285+
// dependencies, and the `pm-dep` dependency was causing a panic because
2286+
// it was looking for target features instead of host features.
2287+
let p = project()
2288+
.file(
2289+
"Cargo.toml",
2290+
r#"
2291+
[package]
2292+
name = "foo"
2293+
version = "0.1.0"
2294+
resolver = "2"
2295+
2296+
[dependencies]
2297+
pm = { path = "pm" }
2298+
"#,
2299+
)
2300+
.file("src/lib.rs", "")
2301+
.file(
2302+
"pm/Cargo.toml",
2303+
r#"
2304+
[package]
2305+
name = "pm"
2306+
version = "0.1.0"
2307+
2308+
[lib]
2309+
proc-macro = true
2310+
2311+
[dependencies]
2312+
pm-dep = { path = "../pm-dep" }
2313+
"#,
2314+
)
2315+
.file("pm/src/lib.rs", "")
2316+
.file("pm-dep/Cargo.toml", &basic_manifest("pm-dep", "0.1.0"))
2317+
.file("pm-dep/src/lib.rs", "")
2318+
.build();
2319+
2320+
// Unfortunately this cannot check the output because what it prints is
2321+
// nondeterministic. Sometimes it says "Compiling pm-dep" and sometimes
2322+
// "Checking pm-dep". This is because it is both building it and checking
2323+
// it in parallel (building so it can build the proc-macro, and checking
2324+
// so rustdoc can load it).
2325+
p.cargo("doc").run();
2326+
}

0 commit comments

Comments
 (0)