Skip to content

Commit 10cdbd8

Browse files
Nemo157GuillaumeGomez
authored andcommitted
Make cfg implicitly imply doc(cfg)
This is only active when the `doc_cfg` feature is active. The implicit cfg can be overridden via #[doc(cfg(...))], so e.g. to hide a #[cfg] you can use something like: ```rust #[cfg(unix)] #[doc(cfg(all()))] pub struct Unix; ``` (since `all()` is always true, it is never shown in the docs)
1 parent 074f636 commit 10cdbd8

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ fn merge_attrs(
318318
} else {
319319
Attributes::from_ast(&both, None)
320320
},
321-
both.cfg(cx.sess()),
321+
both.cfg(cx.tcx),
322322
)
323323
} else {
324-
(old_attrs.clean(cx), old_attrs.cfg(cx.sess()))
324+
(old_attrs.clean(cx), old_attrs.cfg(cx.tcx))
325325
}
326326
}
327327

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,7 @@ fn clean_extern_crate(
19731973
def_id: crate_def_id.into(),
19741974
visibility: krate.vis.clean(cx),
19751975
kind: box ExternCrateItem { src: orig_name },
1976-
cfg: attrs.cfg(cx.sess()),
1976+
cfg: attrs.cfg(cx.tcx),
19771977
}]
19781978
}
19791979

src/librustdoc/clean/types.rs

+48-4
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl Item {
421421
kind,
422422
box ast_attrs.clean(cx),
423423
cx,
424-
ast_attrs.cfg(cx.sess()),
424+
ast_attrs.cfg(cx.tcx),
425425
)
426426
}
427427

@@ -747,7 +747,7 @@ crate trait AttributesExt {
747747

748748
fn other_attrs(&self) -> Vec<ast::Attribute>;
749749

750-
fn cfg(&self, sess: &Session) -> Option<Arc<Cfg>>;
750+
fn cfg(&self, tcx: TyCtxt<'_>) -> Option<Arc<Cfg>>;
751751
}
752752

753753
impl AttributesExt for [ast::Attribute] {
@@ -772,8 +772,52 @@ impl AttributesExt for [ast::Attribute] {
772772
self.iter().filter(|attr| attr.doc_str().is_none()).cloned().collect()
773773
}
774774

775-
fn cfg(&self, sess: &Session) -> Option<Arc<Cfg>> {
776-
let mut cfg = Cfg::True;
775+
fn cfg(&self, tcx: TyCtxt<'_>) -> Option<Arc<Cfg>> {
776+
let sess = tcx.sess;
777+
let doc_cfg_active = tcx.features().doc_cfg;
778+
779+
trait SingleExt {
780+
type Item;
781+
fn single(self) -> Option<Self::Item>;
782+
}
783+
784+
impl<T: IntoIterator> SingleExt for T {
785+
type Item = T::Item;
786+
fn single(self) -> Option<Self::Item> {
787+
let mut iter = self.into_iter();
788+
let item = iter.next()?;
789+
iter.next().is_none().then_some(())?;
790+
Some(item)
791+
}
792+
}
793+
794+
let mut cfg = if doc_cfg_active {
795+
let mut doc_cfg = self
796+
.iter()
797+
.filter(|attr| attr.has_name(sym::doc))
798+
.filter_map(|attr| Some(attr.meta_item_list()?.single()?))
799+
.filter(|attr| attr.has_name(sym::cfg))
800+
.filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone()))
801+
.peekable();
802+
if doc_cfg.peek().is_some() {
803+
doc_cfg
804+
.filter_map(|attr| {
805+
Cfg::parse(&attr).map_err(|e| sess.diagnostic().span_err(e.span, e.msg)).ok()
806+
})
807+
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
808+
} else {
809+
self
810+
.iter()
811+
.filter(|attr| attr.has_name(sym::cfg))
812+
.filter_map(|attr| Some(attr.meta_item_list()?.single()?.meta_item()?.clone()))
813+
.filter_map(|attr| {
814+
Cfg::parse(&attr).map_err(|e| sess.diagnostic().span_err(e.span, e.msg)).ok()
815+
})
816+
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
817+
}
818+
} else {
819+
Cfg::True
820+
};
777821

778822
for attr in self.iter() {
779823
// #[doc]

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
11231123
let ast_attrs = self.tcx.hir().attrs(hir_id);
11241124
let mut attrs = Attributes::from_ast(ast_attrs, None);
11251125

1126-
if let Some(ref cfg) = ast_attrs.cfg(self.sess) {
1126+
if let Some(ref cfg) = ast_attrs.cfg(self.tcx) {
11271127
if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
11281128
return;
11291129
}

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
323323
let import_item = clean::Item {
324324
def_id: import_def_id.into(),
325325
attrs: import_attrs,
326-
cfg: ast_attrs.cfg(cx.sess()),
326+
cfg: ast_attrs.cfg(cx.tcx()),
327327
..myitem.clone()
328328
};
329329

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(rustc_private)]
66
#![feature(array_methods)]
77
#![feature(assert_matches)]
8+
#![feature(bool_to_option)]
89
#![feature(box_patterns)]
910
#![feature(control_flow_enum)]
1011
#![feature(box_syntax)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags:--cfg feature="worricow"
2+
#![crate_name = "xenogenous"]
3+
4+
// @has 'xenogenous/struct.Worricow.html'
5+
// @count - '//*[@class="stab portability"]' 0
6+
#[cfg(feature = "worricow")]
7+
pub struct Worricow;

src/test/rustdoc/doc-cfg-implicit.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![crate_name = "funambulism"]
2+
#![feature(doc_cfg)]
3+
4+
// @has 'funambulism/struct.Disorbed.html'
5+
// @count - '//*[@class="stab portability"]' 1
6+
// @matches - '//*[@class="stab portability"]' 'crate feature disorbed'
7+
// compile-flags:--cfg feature="disorbed"
8+
#[cfg(feature = "disorbed")]
9+
pub struct Disorbed;
10+
11+
// @has 'funambulism/struct.Aesthesia.html'
12+
// @count - '//*[@class="stab portability"]' 1
13+
// @matches - '//*[@class="stab portability"]' 'crate feature aesthesia'
14+
// compile-flags:--cfg feature="aesthesia"
15+
#[doc(cfg(feature = "aesthesia"))]
16+
pub struct Aesthesia;
17+
18+
// @has 'funambulism/struct.Pliothermic.html'
19+
// @count - '//*[@class="stab portability"]' 1
20+
// @matches - '//*[@class="stab portability"]' 'crate feature pliothermic'
21+
// compile-flags:--cfg feature="epopoeist"
22+
#[cfg(feature = "epopoeist")]
23+
#[doc(cfg(feature = "pliothermic"))]
24+
pub struct Pliothermic;
25+
26+
// @has 'funambulism/struct.Simillimum.html'
27+
// @count - '//*[@class="stab portability"]' 0
28+
// compile-flags:--cfg feature="simillimum"
29+
#[cfg(feature = "simillimum")]
30+
#[doc(cfg(all()))]
31+
pub struct Simillimum;

0 commit comments

Comments
 (0)