Skip to content

Commit e3a1e19

Browse files
authored
Rollup merge of #93497 - willcrichton:rustdoc-scrape-test, r=GuillaumeGomez
Pass `--test` flag through rustdoc to rustc so `#[test]` functions can be scraped As a part of stabilizing the scrape examples extension in Cargo, I uncovered a bug where examples cannot be scraped from tests. See this test: https://github.com/rust-lang/cargo/pull/10343/files#diff-27aa4f012ebfebaaee61498d91d2370de460628405d136b05e77efe61e044679R2496 The issue is that when rustdoc is run on a test file, because `--test` is not passed as a rustc option, then functions annotated with `#[test]` are ignored by the compiler. So this PR changes rustdoc so when `--test` is passed in conjunction with a `--scrape-example-<suffix>` flag, then the `test` field of `rustc_interface::Config` is true. r? `@camelid`
2 parents 5c08c39 + fbbcb08 commit e3a1e19

File tree

8 files changed

+37
-5
lines changed

8 files changed

+37
-5
lines changed

src/doc/rustdoc/src/unstable-features.md

+3
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,6 @@ reverse-dependency like `examples/ex.rs` is given to rustdoc with the target
509509
crate being documented (`foobar`) and a path to output the calls
510510
(`output.calls`). Then, the generated calls file can be passed via
511511
`--with-examples` to the subsequent documentation of `foobar`.
512+
513+
To scrape examples from test code, e.g. functions marked `#[test]`, then
514+
add the `--scrape-tests` flag.

src/librustdoc/core.rs

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ crate fn create_config(
200200
lint_opts,
201201
describe_lints,
202202
lint_cap,
203+
scrape_examples_options,
203204
..
204205
}: RustdocOptions,
205206
) -> rustc_interface::Config {
@@ -227,6 +228,7 @@ crate fn create_config(
227228

228229
let crate_types =
229230
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
231+
let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false);
230232
// plays with error output here!
231233
let sessopts = config::Options {
232234
maybe_sysroot,
@@ -244,6 +246,7 @@ crate fn create_config(
244246
edition,
245247
describe_lints,
246248
crate_name,
249+
test,
247250
..Options::default()
248251
};
249252

src/librustdoc/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ fn opts() -> Vec<RustcOptGroup> {
596596
"collect function call information for functions from the target crate",
597597
)
598598
}),
599+
unstable("scrape-tests", |o| {
600+
o.optflag("", "scrape-tests", "Include test code when scraping examples")
601+
}),
599602
unstable("with-examples", |o| {
600603
o.optmulti(
601604
"",

src/librustdoc/scrape_examples.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::path::PathBuf;
3434
crate struct ScrapeExamplesOptions {
3535
output_path: PathBuf,
3636
target_crates: Vec<String>,
37+
crate scrape_tests: bool,
3738
}
3839

3940
impl ScrapeExamplesOptions {
@@ -43,16 +44,22 @@ impl ScrapeExamplesOptions {
4344
) -> Result<Option<Self>, i32> {
4445
let output_path = matches.opt_str("scrape-examples-output-path");
4546
let target_crates = matches.opt_strs("scrape-examples-target-crate");
46-
match (output_path, !target_crates.is_empty()) {
47-
(Some(output_path), true) => Ok(Some(ScrapeExamplesOptions {
47+
let scrape_tests = matches.opt_present("scrape-tests");
48+
match (output_path, !target_crates.is_empty(), scrape_tests) {
49+
(Some(output_path), true, _) => Ok(Some(ScrapeExamplesOptions {
4850
output_path: PathBuf::from(output_path),
4951
target_crates,
52+
scrape_tests,
5053
})),
51-
(Some(_), false) | (None, true) => {
54+
(Some(_), false, _) | (None, true, _) => {
5255
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate together");
5356
Err(1)
5457
}
55-
(None, false) => Ok(None),
58+
(None, false, true) => {
59+
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate with --scrape-tests");
60+
Err(1)
61+
}
62+
(None, false, false) => Ok(None),
5663
}
5764
}
5865
}

src/test/run-make/rustdoc-scrape-examples-multiple/scrape.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ $(TMPDIR)/%.calls: $(TMPDIR)/libfoobar.rmeta
77
--extern foobar=$(TMPDIR)/libfoobar.rmeta \
88
-Z unstable-options \
99
--scrape-examples-output-path $@ \
10-
--scrape-examples-target-crate foobar
10+
--scrape-examples-target-crate foobar \
11+
$(extra_flags)
1112

1213
$(TMPDIR)/lib%.rmeta: src/lib.rs
1314
$(RUSTC) src/lib.rs --crate-name $* --crate-type lib --emit=metadata
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extra_flags := --scrape-tests
2+
deps := ex
3+
4+
-include ../rustdoc-scrape-examples-multiple/scrape.mk
5+
6+
all: scrape
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {}
2+
3+
#[test]
4+
fn a_test() {
5+
foobar::ok();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]' ''
2+
3+
pub fn ok() {}

0 commit comments

Comments
 (0)