Skip to content

Commit

Permalink
Split doc_cfg and doc_auto_cfg features
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 2, 2021
1 parent dca3f1b commit d50a475
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ declare_features! (
/// 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
// -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ symbols! {
div_assign,
doc,
doc_alias,
doc_auto_cfg,
doc_cfg,
doc_cfg_hide,
doc_keyword,
Expand Down
9 changes: 6 additions & 3 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ impl AttributesExt for [ast::Attribute] {
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>> {
let sess = tcx.sess;
let doc_cfg_active = tcx.features().doc_cfg;
let doc_auto_cfg_active = tcx.features().doc_auto_cfg;

fn single<T: IntoIterator>(it: T) -> Option<T::Item> {
let mut iter = it.into_iter();
Expand All @@ -799,24 +800,26 @@ impl AttributesExt for [ast::Attribute] {
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
Expand Down
8 changes: 8 additions & 0 deletions src/test/rustdoc/doc-auto-cfg.rs
Original file line number Diff line number Diff line change
@@ -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() {}
2 changes: 1 addition & 1 deletion src/test/rustdoc/doc-cfg-hide.rs
Original file line number Diff line number Diff line change
@@ -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"))]

Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/doc-cfg-implicit.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/test/rustdoc/feature-gate-doc_auto_cfg.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit d50a475

Please sign in to comment.