Split doc_cfg and doc_auto_cfg features

This commit is contained in:
Guillaume Gomez 2021-11-02 14:36:54 +01:00
parent dca3f1b786
commit d50a4753b8
7 changed files with 28 additions and 5 deletions

View File

@ -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
// -------------------------------------------------------------------------

View File

@ -549,6 +549,7 @@
div_assign,
doc,
doc_alias,
doc_auto_cfg,
doc_cfg,
doc_cfg_hide,
doc_keyword,

View File

@ -789,6 +789,7 @@ fn other_attrs(&self) -> Vec<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();
@ -799,24 +800,26 @@ fn single<T: IntoIterator>(it: T) -> Option<T::Item> {
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

View File

@ -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() {}

View File

@ -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"))]

View File

@ -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

View File

@ -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() {}