Skip to content

Commit 991eb2d

Browse files
Add --check for rustdoc
1 parent 2af662e commit 991eb2d

File tree

9 files changed

+107
-32
lines changed

9 files changed

+107
-32
lines changed

src/bin/cargo/commands/check.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::command_prelude::*;
2+
use crate::commands::doc;
23

4+
use cargo::core::features;
35
use cargo::ops;
46

57
pub fn cli() -> App {
@@ -55,5 +57,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5557
let compile_opts = args.compile_options(config, mode, Some(&ws), ProfileChecking::Unchecked)?;
5658

5759
ops::compile(&ws, &compile_opts)?;
60+
61+
if features::nightly_features_allowed() {
62+
doc::exec_doc(
63+
config,
64+
args,
65+
CompileMode::DocCheck,
66+
ProfileChecking::Checked,
67+
)?;
68+
}
69+
5870
Ok(())
5971
}

src/bin/cargo/commands/doc.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::command_prelude::*;
22

3+
use cargo::core::features;
34
use cargo::ops::{self, DocOptions};
45

56
pub fn cli() -> App {
@@ -23,6 +24,7 @@ pub fn cli() -> App {
2324
"Document only the specified binary",
2425
"Document all binaries",
2526
)
27+
.arg(opt("check", "Runs `rustdoc --check` (nightly only)"))
2628
.arg_release("Build artifacts in release mode, with optimizations")
2729
.arg_profile("Build artifacts with the specified profile")
2830
.arg_features()
@@ -35,18 +37,54 @@ pub fn cli() -> App {
3537
}
3638

3739
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
40+
if args.is_present("check") {
41+
if !features::nightly_features_allowed() {
42+
Err(CliError::new(
43+
anyhow::format_err!("This option is only available in nightly"),
44+
1,
45+
))?;
46+
}
47+
exec_doc(
48+
config,
49+
args,
50+
CompileMode::DocCheck,
51+
ProfileChecking::Unchecked,
52+
)
53+
} else {
54+
exec_doc(
55+
config,
56+
args,
57+
CompileMode::Doc {
58+
deps: !args.is_present("no-deps"),
59+
},
60+
ProfileChecking::Checked,
61+
)
62+
}
63+
}
64+
65+
pub fn exec_doc(
66+
config: &mut Config,
67+
args: &ArgMatches<'_>,
68+
mode: CompileMode,
69+
profile: ProfileChecking,
70+
) -> CliResult {
3871
let ws = args.workspace(config)?;
39-
let mode = CompileMode::Doc {
40-
deps: !args.is_present("no-deps"),
41-
};
42-
let mut compile_opts =
43-
args.compile_options(config, mode, Some(&ws), ProfileChecking::Checked)?;
44-
compile_opts.rustdoc_document_private_items = args.is_present("document-private-items");
4572

46-
let doc_opts = DocOptions {
47-
open_result: args.is_present("open"),
73+
let mut compile_opts = args.compile_options(config, mode, Some(&ws), profile)?;
74+
75+
if !mode.is_check() {
76+
compile_opts.rustdoc_document_private_items = args.is_present("document-private-items");
77+
}
78+
79+
let mut doc_opts = DocOptions {
80+
open_result: false,
4881
compile_opts,
4982
};
83+
84+
if !mode.is_check() {
85+
doc_opts.open_result = args.is_present("open");
86+
}
87+
5088
ops::doc(&ws, &doc_opts)?;
5189
Ok(())
5290
}

src/cargo/core/compiler/build_config.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ pub enum CompileMode {
132132
/// Building a target with `rustc` to emit `rmeta` metadata only. If
133133
/// `test` is true, then it is also compiled with `--test` to check it like
134134
/// a test.
135+
///
136+
/// It then runs `rustdoc --check`.
135137
Check { test: bool },
136138
/// Used to indicate benchmarks should be built. This is not used in
137139
/// `Unit`, because it is essentially the same as `Test` (indicating
@@ -143,6 +145,8 @@ pub enum CompileMode {
143145
Doc { deps: bool },
144146
/// A target that will be tested with `rustdoc`.
145147
Doctest,
148+
/// Runs `rustdoc --check`.
149+
DocCheck,
146150
/// A marker for Units that represent the execution of a `build.rs` script.
147151
RunCustomBuild,
148152
}
@@ -160,6 +164,7 @@ impl ser::Serialize for CompileMode {
160164
Bench => "bench".serialize(s),
161165
Doc { .. } => "doc".serialize(s),
162166
Doctest => "doctest".serialize(s),
167+
DocCheck => "doccheck".serialize(s),
163168
RunCustomBuild => "run-custom-build".serialize(s),
164169
}
165170
}
@@ -168,12 +173,12 @@ impl ser::Serialize for CompileMode {
168173
impl CompileMode {
169174
/// Returns `true` if the unit is being checked.
170175
pub fn is_check(self) -> bool {
171-
matches!(self, CompileMode::Check { .. })
176+
matches!(self, CompileMode::Check { .. } | CompileMode::DocCheck)
172177
}
173178

174179
/// Returns `true` if this is generating documentation.
175180
pub fn is_doc(self) -> bool {
176-
matches!(self, CompileMode::Doc { .. })
181+
matches!(self, CompileMode::Doc { .. } | CompileMode::DocCheck)
177182
}
178183

179184
/// Returns `true` if this a doc test.

src/cargo/core/compiler/build_context/target_info.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ impl TargetInfo {
436436
}
437437
}
438438
CompileMode::Check { .. } => Ok((vec![FileType::new_rmeta()], Vec::new())),
439-
CompileMode::Doc { .. } | CompileMode::Doctest | CompileMode::RunCustomBuild => {
439+
CompileMode::Doc { .. }
440+
| CompileMode::Doctest
441+
| CompileMode::RunCustomBuild
442+
| CompileMode::DocCheck => {
440443
panic!("asked for rustc output for non-rustc mode")
441444
}
442445
}

src/cargo/core/compiler/context/compilation_files.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
395395
// outputs.
396396
vec![]
397397
}
398-
CompileMode::Doctest => {
398+
CompileMode::Doctest | CompileMode::DocCheck => {
399399
// Doctests are built in a temporary directory and then
400400
// deleted. There is the `--persist-doctests` unstable flag,
401401
// but Cargo does not know about that.

src/cargo/core/compiler/mod.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,14 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
562562

563563
let doc_dir = cx.files().out_dir(unit);
564564

565-
// Create the documentation directory ahead of time as rustdoc currently has
566-
// a bug where concurrent invocations will race to create this directory if
567-
// it doesn't already exist.
568-
paths::create_dir_all(&doc_dir)?;
565+
if !unit.mode.is_check() {
566+
// Create the documentation directory ahead of time as rustdoc currently has
567+
// a bug where concurrent invocations will race to create this directory if
568+
// it doesn't already exist.
569+
paths::create_dir_all(&doc_dir)?;
569570

570-
rustdoc.arg("-o").arg(doc_dir);
571+
rustdoc.arg("-o").arg(doc_dir);
572+
}
571573

572574
for feat in &unit.features {
573575
rustdoc.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
@@ -596,6 +598,14 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
596598
let pkg_id = unit.pkg.package_id();
597599
let script_metadata = cx.find_build_script_metadata(unit.clone());
598600

601+
if unit.mode.is_check() {
602+
rustdoc.arg("-Z");
603+
rustdoc.arg("unstable-options");
604+
rustdoc.arg("--check");
605+
// rustdoc.arg("--warn");
606+
// rustdoc.arg("rustdoc");
607+
}
608+
599609
Ok(Work::new(move |state| {
600610
if let Some(script_metadata) = script_metadata {
601611
if let Some(output) = build_script_outputs

src/cargo/core/compiler/timings.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ impl<'cfg> Timings<'cfg> {
170170
CompileMode::Test => target.push_str(" (test)"),
171171
CompileMode::Build => {}
172172
CompileMode::Check { test: true } => target.push_str(" (check-test)"),
173-
CompileMode::Check { test: false } => target.push_str(" (check)"),
173+
CompileMode::Check { test: false } | CompileMode::DocCheck => {
174+
target.push_str(" (check)")
175+
}
174176
CompileMode::Bench => target.push_str(" (bench)"),
175177
CompileMode::Doc { .. } => target.push_str(" (doc)"),
176178
CompileMode::Doctest => target.push_str(" (doc test)"),

src/cargo/core/profiles.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@ impl Profiles {
312312
)
313313
}
314314
}
315-
CompileMode::Build | CompileMode::Check { .. } | CompileMode::RunCustomBuild => {
315+
CompileMode::Build
316+
| CompileMode::Check { .. }
317+
| CompileMode::RunCustomBuild
318+
| CompileMode::DocCheck => {
316319
// Note: `RunCustomBuild` doesn't normally use this code path.
317320
// `build_unit_profiles` normally ensures that it selects the
318321
// ancestor's profile. However, `cargo clean -p` can hit this

src/cargo/ops/cargo_compile.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ pub fn create_bcx<'a, 'cfg>(
315315
| CompileMode::Build
316316
| CompileMode::Check { .. }
317317
| CompileMode::Bench
318-
| CompileMode::RunCustomBuild => {
318+
| CompileMode::RunCustomBuild
319+
| CompileMode::DocCheck => {
319320
if std::env::var("RUST_FLAGS").is_ok() {
320321
config.shell().warn(
321322
"Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?",
@@ -678,17 +679,18 @@ impl CompileFilter {
678679
match mode {
679680
CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,
680681
CompileMode::Check { test: true } => true,
681-
CompileMode::Build | CompileMode::Doc { .. } | CompileMode::Check { test: false } => {
682-
match *self {
683-
CompileFilter::Default { .. } => false,
684-
CompileFilter::Only {
685-
ref examples,
686-
ref tests,
687-
ref benches,
688-
..
689-
} => examples.is_specific() || tests.is_specific() || benches.is_specific(),
690-
}
691-
}
682+
CompileMode::Build
683+
| CompileMode::Doc { .. }
684+
| CompileMode::Check { test: false }
685+
| CompileMode::DocCheck => match *self {
686+
CompileFilter::Default { .. } => false,
687+
CompileFilter::Only {
688+
ref examples,
689+
ref tests,
690+
ref benches,
691+
..
692+
} => examples.is_specific() || tests.is_specific() || benches.is_specific(),
693+
},
692694
CompileMode::RunCustomBuild => panic!("Invalid mode"),
693695
}
694696
}
@@ -1177,7 +1179,7 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target>
11771179
.iter()
11781180
.filter(|t| t.tested() || t.is_example())
11791181
.collect(),
1180-
CompileMode::Build | CompileMode::Check { .. } => targets
1182+
CompileMode::Build | CompileMode::Check { .. } | CompileMode::DocCheck => targets
11811183
.iter()
11821184
.filter(|t| t.is_bin() || t.is_lib())
11831185
.collect(),

0 commit comments

Comments
 (0)