Skip to content

Commit fb0653e

Browse files
committed
Auto merge of #52751 - QuietMisdreavus:you-shall-not-pass, r=GuillaumeGomez
rustdoc: rework how default passes are chosen This is a refactor that changes how we select default passes, and changes the set of passes used for `--document-private-items`. It's groundwork for a bigger refactor i want to do. The major changes: * There are now two sets of "default passes": one set for "no flags given" and one for "document private items". * These sets can be selected by a new `DefaultPassOption` enum, which is selected from based on the presence of `--no-defaults` or `--document-private-items` CLI flags, or their associated crate attributes. * When printing the list of passes, we also print the list of passes for `--document-private-items` in addition to the "default defaults". * I added `propagate-doc-cfg` and `strip-priv-imports` to the "document private items" set. The former is to ensure items are properly tagged with the full set of cfg flags even when "document private items" is active. The latter is based on feedback and personal experience navigating the `rustc` docs, which use that flag. `strip-priv-imports` only removes non-pub `use` statements, so it should be harmless from a documentation standpoint to remove those items from "private items" documentation.
2 parents 6a2c97c + 0db4317 commit fb0653e

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

Diff for: src/librustdoc/lib.rs

+23-26
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ pub fn main_args(args: &[String]) -> isize {
373373
for &name in passes::DEFAULT_PASSES {
374374
println!("{:>20}", name);
375375
}
376+
println!("\nPasses run with `--document-private-items`:");
377+
for &name in passes::DEFAULT_PRIVATE_PASSES {
378+
println!("{:>20}", name);
379+
}
376380
return 0;
377381
}
378382

@@ -623,20 +627,16 @@ fn rust_input<R, F>(cratefile: PathBuf,
623627
where R: 'static + Send,
624628
F: 'static + Send + FnOnce(Output) -> R
625629
{
626-
let mut default_passes = !matches.opt_present("no-defaults");
627-
let mut passes = matches.opt_strs("passes");
628-
let mut plugins = matches.opt_strs("plugins");
629-
630-
// We hardcode in the passes here, as this is a new flag and we
631-
// are generally deprecating passes.
632-
if matches.opt_present("document-private-items") {
633-
default_passes = false;
630+
let mut default_passes = if matches.opt_present("no-defaults") {
631+
passes::DefaultPassOption::None
632+
} else if matches.opt_present("document-private-items") {
633+
passes::DefaultPassOption::Private
634+
} else {
635+
passes::DefaultPassOption::Default
636+
};
634637

635-
passes = vec![
636-
String::from("collapse-docs"),
637-
String::from("unindent-comments"),
638-
];
639-
}
638+
let mut manual_passes = matches.opt_strs("passes");
639+
let mut plugins = matches.opt_strs("plugins");
640640

641641
// First, parse the crate and extract all relevant information.
642642
let mut paths = SearchPaths::new();
@@ -706,13 +706,15 @@ where R: 'static + Send,
706706
if attr.is_word() {
707707
if name == Some("no_default_passes") {
708708
report_deprecated_attr("no_default_passes", &diag);
709-
default_passes = false;
709+
if default_passes == passes::DefaultPassOption::Default {
710+
default_passes = passes::DefaultPassOption::None;
711+
}
710712
}
711713
} else if let Some(value) = attr.value_str() {
712714
let sink = match name {
713715
Some("passes") => {
714716
report_deprecated_attr("passes = \"...\"", &diag);
715-
&mut passes
717+
&mut manual_passes
716718
},
717719
Some("plugins") => {
718720
report_deprecated_attr("plugins = \"...\"", &diag);
@@ -726,20 +728,15 @@ where R: 'static + Send,
726728
}
727729

728730
if attr.is_word() && name == Some("document_private_items") {
729-
default_passes = false;
730-
731-
passes = vec![
732-
String::from("collapse-docs"),
733-
String::from("unindent-comments"),
734-
];
731+
if default_passes == passes::DefaultPassOption::Default {
732+
default_passes = passes::DefaultPassOption::Private;
733+
}
735734
}
736735
}
737736

738-
if default_passes {
739-
for name in passes::DEFAULT_PASSES.iter().rev() {
740-
passes.insert(0, name.to_string());
741-
}
742-
}
737+
let mut passes: Vec<String> =
738+
passes::defaults(default_passes).iter().map(|p| p.to_string()).collect();
739+
passes.extend(manual_passes);
743740

744741
if !plugins.is_empty() {
745742
eprintln!("WARNING: --plugins no longer functions; see CVE-2018-1000622");

Diff for: src/librustdoc/passes/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ pub const DEFAULT_PASSES: &'static [&'static str] = &[
6363
"propagate-doc-cfg",
6464
];
6565

66+
pub const DEFAULT_PRIVATE_PASSES: &'static [&'static str] = &[
67+
"strip-priv-imports",
68+
"collapse-docs",
69+
"unindent-comments",
70+
"propagate-doc-cfg",
71+
];
72+
73+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
74+
pub enum DefaultPassOption {
75+
Default,
76+
Private,
77+
None,
78+
}
79+
80+
pub fn defaults(default_set: DefaultPassOption) -> &'static [&'static str] {
81+
match default_set {
82+
DefaultPassOption::Default => {
83+
DEFAULT_PASSES
84+
},
85+
DefaultPassOption::Private => {
86+
DEFAULT_PRIVATE_PASSES
87+
},
88+
DefaultPassOption::None => {
89+
&[]
90+
},
91+
}
92+
}
6693

6794
struct Stripper<'a> {
6895
retained: &'a mut DefIdSet,

0 commit comments

Comments
 (0)