Skip to content

Commit 74d0d3f

Browse files
Nemo157jyn514
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 85ec783 commit 74d0d3f

File tree

11 files changed

+85
-7
lines changed

11 files changed

+85
-7
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
320320
gate_doc!(
321321
include => external_doc
322322
cfg => doc_cfg
323+
cfg_hide => doc_cfg_hide
323324
masked => doc_masked
324325
notable_trait => doc_notable_trait
325326
keyword => doc_keyword

compiler/rustc_feature/src/active.rs

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

373+
/// Allows `#[doc(cfg_hide(...))]`.
374+
(active, doc_cfg_hide, "1.49.0", Some(43781), None),
375+
373376
/// Allows `#[doc(masked)]`.
374377
(active, doc_masked, "1.21.0", Some(44027), None),
375378

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ symbols! {
346346
cfg_attr_multi,
347347
cfg_doctest,
348348
cfg_eval,
349+
cfg_hide,
349350
cfg_panic,
350351
cfg_sanitize,
351352
cfg_target_feature,
@@ -477,6 +478,7 @@ symbols! {
477478
doc,
478479
doc_alias,
479480
doc_cfg,
481+
doc_cfg_hide,
480482
doc_keyword,
481483
doc_masked,
482484
doc_notable_trait,

src/librustdoc/clean/inline.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ fn merge_attrs(
297297
if let Some(new_id) = parent_module {
298298
let diag = cx.sess().diagnostic();
299299
let doc_cfg_active = cx.tcx.features().doc_cfg;
300-
Attributes::from_ast(diag, old_attrs, Some((inner, new_id)), doc_cfg_active)
300+
Attributes::from_ast(
301+
diag,
302+
old_attrs,
303+
Some((inner, new_id)),
304+
doc_cfg_active,
305+
&cx.hidden_cfg,
306+
)
301307
} else {
302308
let mut both = inner.to_vec();
303309
both.extend_from_slice(old_attrs);

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Clean<Item> for doctree::Module<'_> {
125125
impl Clean<Attributes> for [ast::Attribute] {
126126
fn clean(&self, cx: &mut DocContext<'_>) -> Attributes {
127127
let doc_cfg_active = cx.tcx.features().doc_cfg;
128-
Attributes::from_ast(cx.sess().diagnostic(), self, None, doc_cfg_active)
128+
Attributes::from_ast(cx.sess().diagnostic(), self, None, doc_cfg_active, &cx.hidden_cfg)
129129
}
130130
}
131131

src/librustdoc/clean/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ impl Attributes {
871871
attrs: &[ast::Attribute],
872872
additional_attrs: Option<(&[ast::Attribute], DefId)>,
873873
doc_cfg_active: bool,
874+
hidden_cfg: &FxHashSet<Cfg>,
874875
) -> Attributes {
875876
let mut doc_strings: Vec<DocFragment> = vec![];
876877
let mut sp = None;
@@ -989,6 +990,7 @@ impl Attributes {
989990
.filter_map(|attr| {
990991
Cfg::parse(&attr).map_err(|e| diagnostic.span_err(e.span, e.msg)).ok()
991992
})
993+
.filter(|cfg| !hidden_cfg.contains(cfg))
992994
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
993995
}
994996
} else {

src/librustdoc/core.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::rc::Rc;
3232

3333
use crate::clean;
3434
use crate::clean::inline::build_external_trait;
35-
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
35+
use crate::clean::{cfg::Cfg, AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
3636
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3737
use crate::formats::cache::Cache;
3838
use crate::passes::{self, Condition::*, ConditionalPass};
@@ -84,6 +84,8 @@ crate struct DocContext<'tcx> {
8484
crate inlined: FxHashSet<DefId>,
8585
/// Used by `calculate_doc_coverage`.
8686
crate output_format: OutputFormat,
87+
/// Cfg that have been hidden via #![doc(cfg_hide(...))]
88+
crate hidden_cfg: FxHashSet<Cfg>,
8789
}
8890

8991
impl<'tcx> DocContext<'tcx> {
@@ -432,6 +434,7 @@ crate fn run_global_ctxt(
432434
inlined: FxHashSet::default(),
433435
output_format,
434436
render_options,
437+
hidden_cfg: FxHashSet::default(),
435438
};
436439

437440
// Small hack to force the Sized trait to be present.

src/librustdoc/doctest.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,13 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
10941094
) {
10951095
let doc_cfg_active = self.tcx.features().doc_cfg;
10961096
let attrs = self.tcx.hir().attrs(hir_id);
1097-
let mut attrs = Attributes::from_ast(self.sess.diagnostic(), attrs, None, doc_cfg_active);
1097+
let mut attrs = Attributes::from_ast(
1098+
self.sess.diagnostic(),
1099+
attrs,
1100+
None,
1101+
doc_cfg_active,
1102+
&FxHashSet::default(),
1103+
);
10981104
if let Some(ref cfg) = attrs.cfg {
10991105
if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
11001106
return;

src/librustdoc/html/render/print_item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
290290
cx.tcx().get_attrs(import_def_id),
291291
None,
292292
doc_cfg_active,
293+
&Default::default(), // TODO: this looks wrong
293294
));
294295

295296
// Just need an item with the correct def_id and attrs

src/librustdoc/visit_ast.rs

Lines changed: 25 additions & 3 deletions
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;
@@ -14,7 +15,7 @@ use rustc_span::{self, Span};
1415

1516
use std::mem;
1617

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

@@ -71,13 +72,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7172
}
7273

7374
crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
75+
let tcx = self.cx.tcx;
7476
let span = krate.item.inner;
7577
let mut top_level_module = self.visit_mod_contents(
7678
span,
7779
&Spanned { span, node: hir::VisibilityKind::Public },
7880
hir::CRATE_HIR_ID,
7981
&krate.item,
80-
self.cx.tcx.crate_name,
82+
tcx.crate_name,
8183
);
8284
// Attach the crate's exported macros to the top-level module.
8385
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
@@ -93,7 +95,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
9395
top_level_module.macros.push((def, None));
9496
continue 'exported_macros;
9597
}
96-
let tcx = self.cx.tcx;
9798
// Note: this is not the same as `.parent_module()`. Indeed, the latter looks
9899
// for the closest module _ancestor_, which is not necessarily a direct parent
99100
// (since a direct parent isn't necessarily a module, c.f. #77828).
@@ -123,6 +124,27 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
123124
assert_eq!(cur_mod_def_id, macro_parent_def_id);
124125
cur_mod.macros.push((def, None));
125126
}
127+
128+
self.cx.hidden_cfg = tcx.hir().attrs(CRATE_HIR_ID)
129+
.iter()
130+
.filter(|attr| attr.has_name(sym::doc))
131+
.flat_map(|attr| attr.meta_item_list().into_iter().flatten())
132+
.filter(|attr| attr.has_name(sym::cfg_hide))
133+
.flat_map(|attr| {
134+
attr.meta_item_list()
135+
.unwrap_or(&[])
136+
.iter()
137+
.filter_map(|attr| {
138+
Some(
139+
Cfg::parse(attr.meta_item()?)
140+
.map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
141+
.ok()?,
142+
)
143+
})
144+
.collect::<Vec<_>>()
145+
})
146+
.collect();
147+
126148
self.cx.cache.exact_paths = self.exact_paths;
127149
top_level_module
128150
}

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)