Skip to content

Commit 4cc3f4f

Browse files
committed
Auto merge of #10017 - willcrichton:example-analyzer, r=alexcrichton
Change --scrape-examples flag to -Z rustdoc-scrape-examples I'm working on getting the scrape examples feature working on docs.rs. However, docs.rs uses `cargo rustdoc` instead of `cargo doc`, and right now the `--scrape-examples` flag is only allowed for `cargo doc`. So this PR changes it to a `-Z` flag that can be passed to either command.
2 parents 0a98b1d + 9fb78cf commit 4cc3f4f

File tree

7 files changed

+35
-57
lines changed

7 files changed

+35
-57
lines changed

src/bin/cargo/commands/doc.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::command_prelude::*;
22

3-
use anyhow::anyhow;
4-
use cargo::ops::{self, CompileFilter, DocOptions, FilterRule, LibRule};
3+
use cargo::ops::{self, DocOptions};
54

65
pub fn cli() -> App {
76
subcommand("doc")
@@ -20,13 +19,6 @@ pub fn cli() -> App {
2019
)
2120
.arg(opt("no-deps", "Don't build documentation for dependencies"))
2221
.arg(opt("document-private-items", "Document private items"))
23-
.arg(
24-
opt(
25-
"scrape-examples",
26-
"Scrape examples to include as function documentation",
27-
)
28-
.value_name("FLAGS"),
29-
)
3022
.arg_jobs()
3123
.arg_targets_lib_bin_example(
3224
"Document only this package's library",
@@ -56,33 +48,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
5648
args.compile_options(config, mode, Some(&ws), ProfileChecking::Custom)?;
5749
compile_opts.rustdoc_document_private_items = args.is_present("document-private-items");
5850

59-
// TODO(wcrichto): move scrape example configuration into Cargo.toml before stabilization
60-
// See: https://github.com/rust-lang/cargo/pull/9525#discussion_r728470927
61-
compile_opts.rustdoc_scrape_examples = match args.value_of("scrape-examples") {
62-
Some(s) => Some(match s {
63-
"all" => CompileFilter::new_all_targets(),
64-
"examples" => CompileFilter::new(
65-
LibRule::False,
66-
FilterRule::none(),
67-
FilterRule::none(),
68-
FilterRule::All,
69-
FilterRule::none(),
70-
),
71-
_ => {
72-
return Err(CliError::from(anyhow!(
73-
r#"--scrape-examples must take "all" or "examples" as an argument"#
74-
)));
75-
}
76-
}),
77-
None => None,
78-
};
79-
80-
if compile_opts.rustdoc_scrape_examples.is_some() {
81-
config
82-
.cli_unstable()
83-
.fail_if_stable_opt("--scrape-examples", 9910)?;
84-
}
85-
8651
let doc_opts = DocOptions {
8752
open_result: args.is_present("open"),
8853
compile_opts,

src/cargo/core/features.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@ unstable_cli_options!(
654654
timings: Option<Vec<String>> = ("Display concurrency information"),
655655
unstable_options: bool = ("Allow the usage of unstable options"),
656656
weak_dep_features: bool = ("Allow `dep_name?/feature` feature syntax"),
657+
// TODO(wcrichto): move scrape example configuration into Cargo.toml before stabilization
658+
// See: https://github.com/rust-lang/cargo/pull/9525#discussion_r728470927
659+
rustdoc_scrape_examples: Option<String> = ("Allow rustdoc to scrape examples from reverse-dependencies for documentation"),
657660
skip_rustdoc_fingerprint: bool = (HIDDEN),
658661
);
659662

@@ -871,6 +874,7 @@ impl CliUnstable {
871874
"namespaced-features" => self.namespaced_features = parse_empty(k, v)?,
872875
"weak-dep-features" => self.weak_dep_features = parse_empty(k, v)?,
873876
"credential-process" => self.credential_process = parse_empty(k, v)?,
877+
"rustdoc-scrape-examples" => self.rustdoc_scrape_examples = v.map(|s| s.to_string()),
874878
"skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?,
875879
"compile-progress" => stabilized_warn(k, "1.30", STABILIZED_COMPILE_PROGRESS),
876880
"offline" => stabilized_err(k, "1.36", STABILIZED_OFFLINE)?,

src/cargo/ops/cargo_compile.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::util::interning::InternedString;
4545
use crate::util::restricted_names::is_glob_pattern;
4646
use crate::util::{closest_msg, profile, CargoResult, StableHasher};
4747

48-
use anyhow::Context as _;
48+
use anyhow::{bail, Context as _};
4949

5050
/// Contains information about how a package should be compiled.
5151
///
@@ -76,9 +76,6 @@ pub struct CompileOptions {
7676
/// Whether the `--document-private-items` flags was specified and should
7777
/// be forwarded to `rustdoc`.
7878
pub rustdoc_document_private_items: bool,
79-
/// Whether the `--scrape-examples` flag was specified and build flags for
80-
/// examples should be forwarded to `rustdoc`.
81-
pub rustdoc_scrape_examples: Option<CompileFilter>,
8279
/// Whether the build process should check the minimum Rust version
8380
/// defined in the cargo metadata for a crate.
8481
pub honor_rust_version: bool,
@@ -97,7 +94,6 @@ impl<'a> CompileOptions {
9794
target_rustc_args: None,
9895
local_rustdoc_args: None,
9996
rustdoc_document_private_items: false,
100-
rustdoc_scrape_examples: None,
10197
honor_rust_version: true,
10298
})
10399
}
@@ -338,7 +334,6 @@ pub fn create_bcx<'a, 'cfg>(
338334
ref target_rustc_args,
339335
ref local_rustdoc_args,
340336
rustdoc_document_private_items,
341-
ref rustdoc_scrape_examples,
342337
honor_rust_version,
343338
} = *options;
344339
let config = ws.config();
@@ -369,6 +364,7 @@ pub fn create_bcx<'a, 'cfg>(
369364
let target_data = RustcTargetData::new(ws, &build_config.requested_kinds)?;
370365

371366
let all_packages = &Packages::All;
367+
let rustdoc_scrape_examples = &config.cli_unstable().rustdoc_scrape_examples;
372368
let need_reverse_dependencies = rustdoc_scrape_examples.is_some();
373369
let full_specs = if need_reverse_dependencies {
374370
all_packages
@@ -506,15 +502,30 @@ pub fn create_bcx<'a, 'cfg>(
506502
)?;
507503

508504
let mut scrape_units = match rustdoc_scrape_examples {
509-
Some(scrape_filter) => {
505+
Some(arg) => {
506+
let filter = match arg.as_str() {
507+
"all" => CompileFilter::new_all_targets(),
508+
"examples" => CompileFilter::new(
509+
LibRule::False,
510+
FilterRule::none(),
511+
FilterRule::none(),
512+
FilterRule::All,
513+
FilterRule::none(),
514+
),
515+
_ => {
516+
bail!(
517+
r#"-Z rustdoc-scrape-examples must take "all" or "examples" as an argument"#
518+
)
519+
}
520+
};
510521
let to_build_ids = resolve.specs_to_ids(&resolve_specs)?;
511522
let to_builds = pkg_set.get_many(to_build_ids)?;
512523
let mode = CompileMode::Docscrape;
513524

514525
generate_targets(
515526
ws,
516527
&to_builds,
517-
scrape_filter,
528+
&filter,
518529
&build_config.requested_kinds,
519530
explicit_host_kind,
520531
mode,

src/cargo/ops/cargo_package.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,6 @@ fn run_verify(
765765
target_rustc_args: rustc_args,
766766
local_rustdoc_args: None,
767767
rustdoc_document_private_items: false,
768-
rustdoc_scrape_examples: None,
769768
honor_rust_version: true,
770769
},
771770
&exec,

src/cargo/util/command_prelude.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ pub trait ArgMatchesExt {
544544
target_rustc_args: None,
545545
local_rustdoc_args: None,
546546
rustdoc_document_private_items: false,
547-
rustdoc_scrape_examples: None,
548547
honor_rust_version: !self._is_present("ignore-rust-version"),
549548
};
550549

src/doc/src/reference/unstable.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,11 +1390,11 @@ Custom named profiles have been stabilized in the 1.57 release. See the
13901390
* RFC: [#3123](https://github.com/rust-lang/rfcs/pull/3123)
13911391
* Tracking Issue: [#9910](https://github.com/rust-lang/cargo/issues/9910)
13921392

1393-
The `--scrape-examples` argument to the `doc` command tells Rustdoc to search
1394-
crates in the current workspace for calls to functions. Those call-sites are then
1395-
included as documentation. The flag can take an argument of `all` or `examples`
1396-
which configures which crate in the workspace to analyze for examples. For instance:
1393+
The `-Z rustdoc-scrape-examples` argument tells Rustdoc to search crates in the current workspace
1394+
for calls to functions. Those call-sites are then included as documentation. The flag can take an
1395+
argument of `all` or `examples` which configures which crate in the workspace to analyze for examples.
1396+
For instance:
13971397

13981398
```
1399-
cargo doc -Z unstable-options --scrape-examples examples
1399+
cargo doc -Z unstable-options -Z rustdoc-scrape-examples=examples
14001400
```

tests/testsuite/doc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,7 @@ fn doc_fingerprint_unusual_behavior() {
21522152
#[cargo_test]
21532153
fn scrape_examples_basic() {
21542154
if !is_nightly() {
2155-
// --scrape-examples is unstable
2155+
// -Z rustdoc-scrape-examples is unstable
21562156
return;
21572157
}
21582158

@@ -2170,7 +2170,7 @@ fn scrape_examples_basic() {
21702170
.file("src/lib.rs", "pub fn foo() {}\npub fn bar() { foo(); }")
21712171
.build();
21722172

2173-
p.cargo("doc -Zunstable-options --scrape-examples all")
2173+
p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples=all")
21742174
.masquerade_as_nightly_cargo()
21752175
.with_stderr(
21762176
"\
@@ -2192,7 +2192,7 @@ fn scrape_examples_basic() {
21922192
#[cargo_test]
21932193
fn scrape_examples_avoid_build_script_cycle() {
21942194
if !is_nightly() {
2195-
// --scrape-examples is unstable
2195+
// -Z rustdoc-scrape-examples is unstable
21962196
return;
21972197
}
21982198

@@ -2231,15 +2231,15 @@ fn scrape_examples_avoid_build_script_cycle() {
22312231
.file("bar/build.rs", "fn main(){}")
22322232
.build();
22332233

2234-
p.cargo("doc --all -Zunstable-options --scrape-examples all")
2234+
p.cargo("doc --all -Zunstable-options -Z rustdoc-scrape-examples=all")
22352235
.masquerade_as_nightly_cargo()
22362236
.run();
22372237
}
22382238

22392239
#[cargo_test]
22402240
fn scrape_examples_complex_reverse_dependencies() {
22412241
if !is_nightly() {
2242-
// --scrape-examples is unstable
2242+
// -Z rustdoc-scrape-examples is unstable
22432243
return;
22442244
}
22452245

@@ -2293,7 +2293,7 @@ fn scrape_examples_complex_reverse_dependencies() {
22932293
.file("b/src/lib.rs", "")
22942294
.build();
22952295

2296-
p.cargo("doc -Zunstable-options --scrape-examples all")
2296+
p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples=all")
22972297
.masquerade_as_nightly_cargo()
22982298
.run();
22992299
}

0 commit comments

Comments
 (0)