Skip to content

Commit c994a4a

Browse files
committed
Auto merge of #11497 - willcrichton:fix-issue-11496, r=weihanglo
Fix examples of proc-macro crates being scraped for examples This PR fixes #11496, where examples in proc-macro crates would crash the build with `-Zrustdoc-scrape-examples`. The fix is to change `unit_needs_doc_scrape` to check if a unit is coming from a `proc-macro` package. I added a test to ensure the simple example passes. I also ensured that [automod](https://github.com/dtolnay/automod) also documents correctly. r? `@weihanglo`
2 parents 9f4efa5 + d588298 commit c994a4a

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/cargo/core/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ impl<'cfg> Workspace<'cfg> {
15151515
// (not documented) or proc macros (have no scrape-able exports). Additionally,
15161516
// naively passing a proc macro's unit_for to new_unit_dep will currently cause
15171517
// Cargo to panic, see issue #10545.
1518-
self.is_member(&unit.pkg) && !unit.target.for_host()
1518+
self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
15191519
}
15201520
}
15211521

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,15 @@ pub fn create_bcx<'a, 'cfg>(
375375
mode: CompileMode::Docscrape,
376376
..generator
377377
};
378-
let all_units = scrape_generator.generate_root_units()?;
379-
380-
let valid_units = all_units
381-
.into_iter()
382-
.filter(|unit| {
383-
ws.unit_needs_doc_scrape(unit)
384-
&& !matches!(
385-
unit.target.doc_scrape_examples(),
386-
RustdocScrapeExamples::Disabled
387-
)
388-
})
389-
.collect::<Vec<_>>();
390-
valid_units
378+
let mut scrape_units = scrape_generator.generate_root_units()?;
379+
scrape_units.retain(|unit| {
380+
ws.unit_needs_doc_scrape(unit)
381+
&& !matches!(
382+
unit.target.doc_scrape_examples(),
383+
RustdocScrapeExamples::Disabled
384+
)
385+
});
386+
scrape_units
391387
} else {
392388
Vec::new()
393389
};

tests/testsuite/docscrape.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,27 @@ fn only_scrape_documented_targets() {
595595
// be scraped.
596596
run_with_config("doc = false\ndoc-scrape-examples = true", true);
597597
}
598+
599+
#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
600+
fn issue_11496() {
601+
let p = project()
602+
.file(
603+
"Cargo.toml",
604+
r#"
605+
[package]
606+
name = "repro"
607+
version = "0.1.0"
608+
edition = "2021"
609+
610+
[lib]
611+
proc-macro = true
612+
"#,
613+
)
614+
.file("src/lib.rs", "")
615+
.file("examples/ex.rs", "fn main(){}")
616+
.build();
617+
618+
p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples")
619+
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
620+
.run();
621+
}

0 commit comments

Comments
 (0)