Skip to content

Commit 18fdd81

Browse files
Nemo157GuillaumeGomez
authored andcommitted
Allow adding a set of cfg's to hide from being implicitly doc(cfg)'d
By adding #![doc(cfg_hide(foobar))] to the crate attributes the cfg #[cfg(foobar)] (and _only_ that _exact_ cfg) will not be implicitly treated as a doc(cfg) to render a message in the documentation.
1 parent 10cdbd8 commit 18fdd81

File tree

11 files changed

+74
-9
lines changed

11 files changed

+74
-9
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
279279

280280
gate_doc!(
281281
cfg => doc_cfg
282+
cfg_hide => doc_cfg_hide
282283
masked => doc_masked
283284
notable_trait => doc_notable_trait
284285
keyword => doc_keyword

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ declare_features! (
380380
/// Allows `#[doc(cfg(...))]`.
381381
(active, doc_cfg, "1.21.0", Some(43781), None),
382382

383+
/// Allows `#[doc(cfg_hide(...))]`.
384+
(active, doc_cfg_hide, "1.49.0", Some(43781), None),
385+
383386
/// Allows `#[doc(masked)]`.
384387
(active, doc_masked, "1.21.0", Some(44027), None),
385388

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ symbols! {
399399
cfg_attr_multi,
400400
cfg_doctest,
401401
cfg_eval,
402+
cfg_hide,
402403
cfg_panic,
403404
cfg_sanitize,
404405
cfg_target_abi,
@@ -547,6 +548,7 @@ symbols! {
547548
doc,
548549
doc_alias,
549550
doc_cfg,
551+
doc_cfg_hide,
550552
doc_keyword,
551553
doc_masked,
552554
doc_notable_trait,

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
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.tcx),
321+
both.cfg(cx.tcx, &cx.cache.hidden_cfg),
322322
)
323323
} else {
324-
(old_attrs.clean(cx), old_attrs.cfg(cx.tcx))
324+
(old_attrs.clean(cx), old_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg))
325325
}
326326
}
327327

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
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.tcx),
1976+
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
19771977
}]
19781978
}
19791979

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 3 deletions
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.tcx),
424+
ast_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
425425
)
426426
}
427427

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

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

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

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

775-
fn cfg(&self, tcx: TyCtxt<'_>) -> Option<Arc<Cfg>> {
775+
fn cfg(&self, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet<Cfg>) -> Option<Arc<Cfg>> {
776776
let sess = tcx.sess;
777777
let doc_cfg_active = tcx.features().doc_cfg;
778778

@@ -813,6 +813,7 @@ impl AttributesExt for [ast::Attribute] {
813813
.filter_map(|attr| {
814814
Cfg::parse(&attr).map_err(|e| sess.diagnostic().span_err(e.span, e.msg)).ok()
815815
})
816+
.filter(|cfg| !hidden_cfg.contains(cfg))
816817
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
817818
}
818819
} else {
@@ -844,6 +845,8 @@ impl AttributesExt for [ast::Attribute] {
844845
}
845846
}
846847

848+
// treat #[target_feature(enable = "feat")] attributes as if they were
849+
// #[doc(cfg(target_feature = "feat"))] attributes as well
847850
for attr in self.lists(sym::target_feature) {
848851
if attr.has_name(sym::enable) {
849852
if let Some(feat) = attr.value_str() {

src/librustdoc/doctest.rs

Lines changed: 1 addition & 1 deletion
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.tcx) {
1126+
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
11271127
if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
11281128
return;
11291129
}

src/librustdoc/formats/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ crate struct Cache {
119119
///
120120
/// Links are indexed by the DefId of the item they document.
121121
crate intra_doc_links: FxHashMap<ItemId, Vec<clean::ItemLink>>,
122+
/// Cfg that have been hidden via #![doc(cfg_hide(...))]
123+
crate hidden_cfg: FxHashSet<clean::cfg::Cfg>,
122124
}
123125

124126
/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.

src/librustdoc/html/render/print_item.rs

Lines changed: 1 addition & 1 deletion
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.tcx()),
326+
cfg: ast_attrs.cfg(cx.tcx(), &cx.cache().hidden_cfg),
327327
..myitem.clone()
328328
};
329329

src/librustdoc/visit_ast.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
55
use rustc_hir as hir;
6+
use rustc_hir::CRATE_HIR_ID;
67
use rustc_hir::def::{DefKind, Res};
78
use rustc_hir::def_id::DefId;
89
use rustc_hir::Node;
@@ -15,7 +16,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
1516

1617
use std::mem;
1718

18-
use crate::clean::{self, AttributesExt, NestedAttributesExt};
19+
use crate::clean::{self, cfg::Cfg, AttributesExt, NestedAttributesExt};
1920
use crate::core;
2021
use crate::doctree::*;
2122

@@ -97,6 +98,27 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
9798
}
9899
}
99100
}
101+
102+
self.cx.cache.hidden_cfg = self.cx.tcx.hir().attrs(CRATE_HIR_ID)
103+
.iter()
104+
.filter(|attr| attr.has_name(sym::doc))
105+
.flat_map(|attr| attr.meta_item_list().into_iter().flatten())
106+
.filter(|attr| attr.has_name(sym::cfg_hide))
107+
.flat_map(|attr| {
108+
attr.meta_item_list()
109+
.unwrap_or(&[])
110+
.iter()
111+
.filter_map(|attr| {
112+
Some(
113+
Cfg::parse(attr.meta_item()?)
114+
.map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
115+
.ok()?,
116+
)
117+
})
118+
.collect::<Vec<_>>()
119+
})
120+
.collect();
121+
100122
self.cx.cache.exact_paths = self.exact_paths;
101123
top_level_module
102124
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![crate_name = "oud"]
2+
#![feature(doc_cfg, doc_cfg_hide)]
3+
4+
#![doc(cfg_hide(feature = "solecism"))]
5+
6+
// @has 'oud/struct.Solecism.html'
7+
// @count - '//*[@class="stab portability"]' 0
8+
// compile-flags:--cfg feature="solecism"
9+
#[cfg(feature = "solecism")]
10+
pub struct Solecism;
11+
12+
// @has 'oud/struct.Scribacious.html'
13+
// @count - '//*[@class="stab portability"]' 1
14+
// @matches - '//*[@class="stab portability"]' 'crate feature solecism'
15+
#[cfg(feature = "solecism")]
16+
#[doc(cfg(feature = "solecism"))]
17+
pub struct Scribacious;
18+
19+
// @has 'oud/struct.Hyperdulia.html'
20+
// @count - '//*[@class="stab portability"]' 1
21+
// @matches - '//*[@class="stab portability"]' 'crate feature hyperdulia'
22+
// compile-flags:--cfg feature="hyperdulia"
23+
#[cfg(feature = "solecism")]
24+
#[cfg(feature = "hyperdulia")]
25+
pub struct Hyperdulia;
26+
27+
// @has 'oud/struct.Oystercatcher.html'
28+
// @count - '//*[@class="stab portability"]' 1
29+
// @matches - '//*[@class="stab portability"]' 'crate features solecism and oystercatcher'
30+
// compile-flags:--cfg feature="oystercatcher"
31+
#[cfg(all(feature = "solecism", feature = "oystercatcher"))]
32+
pub struct Oystercatcher;

0 commit comments

Comments
 (0)