From d50a4753b8cffdb17e2d83282d95b61e266521fb Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 2 Nov 2021 14:36:54 +0100 Subject: [PATCH] Split doc_cfg and doc_auto_cfg features --- compiler/rustc_feature/src/active.rs | 3 +++ compiler/rustc_span/src/symbol.rs | 1 + src/librustdoc/clean/types.rs | 9 ++++++--- src/test/rustdoc/doc-auto-cfg.rs | 8 ++++++++ src/test/rustdoc/doc-cfg-hide.rs | 2 +- src/test/rustdoc/doc-cfg-implicit.rs | 2 +- src/test/rustdoc/feature-gate-doc_auto_cfg.rs | 8 ++++++++ 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/test/rustdoc/doc-auto-cfg.rs create mode 100644 src/test/rustdoc/feature-gate-doc_auto_cfg.rs diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 2bbfb561ba5..42af8e1faad 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -688,6 +688,9 @@ pub fn set(&self, features: &mut Features, span: Span) { /// not changed from prior instances of the same struct (RFC #2528) (incomplete, type_changing_struct_update, "1.58.0", Some(86555), None), + /// Tells rustdoc to automatically generate `#[doc(cfg(...))]`. + (active, doc_auto_cfg, "1.58.0", Some(43781), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 1b431589632..5f71e955e2a 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -549,6 +549,7 @@ div_assign, doc, doc_alias, + doc_auto_cfg, doc_cfg, doc_cfg_hide, doc_keyword, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 88fffaecb93..56ae43855de 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -789,6 +789,7 @@ fn other_attrs(&self) -> Vec { fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet) -> Option> { let sess = tcx.sess; let doc_cfg_active = tcx.features().doc_cfg; + let doc_auto_cfg_active = tcx.features().doc_auto_cfg; fn single(it: T) -> Option { let mut iter = it.into_iter(); @@ -799,24 +800,26 @@ fn single(it: T) -> Option { Some(item) } - let mut cfg = if doc_cfg_active { + let mut cfg = if doc_cfg_active || doc_auto_cfg_active { let mut doc_cfg = self .iter() .filter(|attr| attr.has_name(sym::doc)) .flat_map(|attr| attr.meta_item_list().unwrap_or_else(Vec::new)) .filter(|attr| attr.has_name(sym::cfg)) .peekable(); - if doc_cfg.peek().is_some() { + if doc_cfg.peek().is_some() && doc_cfg_active { doc_cfg .filter_map(|attr| Cfg::parse(attr.meta_item()?).ok()) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) - } else { + } else if doc_auto_cfg_active { self.iter() .filter(|attr| attr.has_name(sym::cfg)) .filter_map(|attr| single(attr.meta_item_list()?)) .filter_map(|attr| Cfg::parse(attr.meta_item()?).ok()) .filter(|cfg| !hidden_cfg.contains(cfg)) .fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg) + } else { + Cfg::True } } else { Cfg::True diff --git a/src/test/rustdoc/doc-auto-cfg.rs b/src/test/rustdoc/doc-auto-cfg.rs new file mode 100644 index 00000000000..fcdd8354569 --- /dev/null +++ b/src/test/rustdoc/doc-auto-cfg.rs @@ -0,0 +1,8 @@ +#![feature(doc_auto_cfg)] + +#![crate_name = "foo"] + +// @has foo/fn.foo.html +// @has - '//*[@class="item-info"]/*[@class="stab portability"]' 'non-test' +#[cfg(not(test))] +pub fn foo() {} diff --git a/src/test/rustdoc/doc-cfg-hide.rs b/src/test/rustdoc/doc-cfg-hide.rs index b9d0d323137..424fa6d6a91 100644 --- a/src/test/rustdoc/doc-cfg-hide.rs +++ b/src/test/rustdoc/doc-cfg-hide.rs @@ -1,5 +1,5 @@ #![crate_name = "oud"] -#![feature(doc_cfg, doc_cfg_hide)] +#![feature(doc_auto_cfg, doc_cfg, doc_cfg_hide)] #![doc(cfg_hide(feature = "solecism"))] diff --git a/src/test/rustdoc/doc-cfg-implicit.rs b/src/test/rustdoc/doc-cfg-implicit.rs index 36c2025785d..5d17a4ede6a 100644 --- a/src/test/rustdoc/doc-cfg-implicit.rs +++ b/src/test/rustdoc/doc-cfg-implicit.rs @@ -1,5 +1,5 @@ #![crate_name = "funambulism"] -#![feature(doc_cfg)] +#![feature(doc_auto_cfg, doc_cfg)] // @has 'funambulism/struct.Disorbed.html' // @count - '//*[@class="stab portability"]' 1 diff --git a/src/test/rustdoc/feature-gate-doc_auto_cfg.rs b/src/test/rustdoc/feature-gate-doc_auto_cfg.rs new file mode 100644 index 00000000000..da76381e480 --- /dev/null +++ b/src/test/rustdoc/feature-gate-doc_auto_cfg.rs @@ -0,0 +1,8 @@ +#![feature(doc_cfg)] + +#![crate_name = "foo"] + +// @has foo/fn.foo.html +// @count - '//*[@class="item-info"]/*[@class="stab portability"]' 0 +#[cfg(not(test))] +pub fn foo() {}