8000 Make cfg imply doc(cfg) by Nemo157 · Pull Request #79341 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Make cfg imply doc(cfg) #79341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
Nemo157 authored and jyn514 committed Apr 26, 2021
commit 74d0d3f76b11c9951c81ec71b4c178aa6ce62ad6
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_doc!(
include => external_doc
cfg => doc_cfg
cfg_hide => doc_cfg_hide
masked => doc_masked
notable_trait => doc_notable_trait
keyword => doc_keyword
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ declare_features! (
/// Allows `#[doc(cfg(...))]`.
(active, doc_cfg, "1.21.0", Some(43781), None),

/// Allows `#[doc(cfg_hide(...))]`.
(active, doc_cfg_hide, "1.49.0", Some(43781), None),

/// Allows `#[doc(masked)]`.
(active, doc_masked, "1.21.0", Some(44027), None),

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ symbols! {
cfg_attr_multi,
cfg_doctest,
cfg_eval,
cfg_hide,
cfg_panic,
cfg_sanitize,
cfg_target_feature,
Expand Down Expand Up @@ -477,6 +478,7 @@ symbols! {
doc,
doc_alias,
doc_cfg,
doc_cfg_hide,
doc_keyword,
doc_masked,
doc_notable_trait,
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ fn merge_attrs(
if let Some(new_id) = parent_module {
let diag = cx.sess().diagnostic();
let doc_cfg_active = cx.tcx.features().doc_cfg;
Attributes::from_ast(diag, old_attrs, Some((inner, new_id)), doc_cfg_active)
Attributes::from_ast(
diag,
old_attrs,
Some((inner, new_id)),
doc_cfg_active,
&cx.hidden_cfg,
)
} else {
let mut both = inner.to_vec();
both.extend_from_slice(old_attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Clean<Item> for doctree::Module<'_> {
impl Clean<Attributes> for [ast::Attribute] {
fn clean(&self, cx: &mut DocContext<'_>) -> Attributes {
let doc_cfg_active = cx.tcx.features().doc_cfg;
Attributes::from_ast(cx.sess().diagnostic(), self, None, doc_cfg_active)
Attributes::from_ast(cx.sess().diagnostic(), self, None, doc_cfg_active, &cx.hidden_cfg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put this on Attributes instead of calling tcx.features() on demand?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found why - you use it directly in from_ast, not after Attributes is constructed. It's either this or pass a tcx; this seems fine.

}
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ impl Attributes {
attrs: &[ast::Attribute],
additional_attrs: Option<(&[ast::Attribute], DefId)>,
doc_cfg_active: bool,
hidden_cfg: &FxHashSet<Cfg>,
) -> Attributes {
let mut doc_strings: Vec<DocFragment> = vec![];
let mut sp = None;
Expand Down Expand Up @@ -989,6 +990,7 @@ impl Attributes {
.filter_map(|attr| {
Cfg::parse(&attr).map_err(|e| diagnostic.span_err(e.span, e.msg)).ok()
})
.filter(|cfg| !hidden_cfg.contains(cfg))
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::rc::Rc;

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

impl<'tcx> DocContext<'tcx> {
Expand Down Expand Up @@ -432,6 +434,7 @@ crate fn run_global_ctxt(
inlined: FxHashSet::default(),
output_format,
render_options,
hidden_cfg: FxHashSet::default(),
};

// Small hack to force the Sized trait to be present.
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,13 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
) {
let doc_cfg_active = self.tcx.features().doc_cfg;
let attrs = self.tcx.hir().attrs(hir_id);
let mut attrs = Attributes::from_ast(self.sess.diagnostic(), attrs, None, doc_cfg_active);
let mut attrs = Attributes::from_ast(
self.sess.diagnostic(),
attrs,
None,
doc_cfg_active,
&FxHashSet::default(),
);
if let Some(ref cfg) = attrs.cfg {
if !cfg.matches(&self.sess.parse_sess, Some(&self.sess.features_untracked())) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
cx.tcx().get_attrs(import_def_id),
None,
doc_cfg_active,
&Default::default(), // TODO: this looks wrong
));

// Just need an item with the correct def_id and attrs
Expand Down
28 changes: 25 additions & 3 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::CRATE_HIR_ID;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::Node;
Expand All @@ -14,7 +15,7 @@ use rustc_span::{self, Span};

use std::mem;

use crate::clean::{self, AttributesExt, NestedAttributesExt};
use crate::clean::{self, cfg::Cfg, AttributesExt, NestedAttributesExt};
use crate::core;
use crate::doctree::*;

Expand Down Expand Up @@ -71,13 +72,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
}

crate fn visit(mut self, krate: &'tcx hir::Crate<'_>) -> Module<'tcx> {
let tcx = self.cx.tcx;
let span = krate.item.inner;
let mut top_level_module = self.visit_mod_contents(
span,
&Spanned { span, node: hir::VisibilityKind::Public },
hir::CRATE_HIR_ID,
&krate.item,
self.cx.tcx.crate_name,
tcx.crate_name,
);
// Attach the crate's exported macros to the top-level module.
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
Expand All @@ -93,7 +95,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
top_level_module.macros.push((def, None));
continue 'exported_macros;
}
let tcx = self.cx.tcx;
// Note: this is not the same as `.parent_module()`. Indeed, the latter looks
// for the closest module _ancestor_, which is not necessarily a direct parent
// (since a direct parent isn't necessarily a module, c.f. #77828).
Expand Down Expand Up @@ -123,6 +124,27 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
assert_eq!(cur_mod_def_id, macro_parent_def_id);
cur_mod.macros.push((def, None));
}

self.cx.hidden_cfg = tcx.hir().attrs(CRATE_HIR_ID)
.iter()
.filter(|attr| attr.has_name(sym::doc))
.flat_map(|attr| attr.meta_item_list().into_iter().flatten())
.filter(|attr| attr.has_name(sym::cfg_hide))
.flat_map(|attr| {
attr.meta_item_list()
.unwrap_or(&[])
.iter()
.filter_map(|attr| {
Some(
Cfg::parse(attr.meta_item()?)
.map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
.ok()?,
)
})
.collect::<Vec<_>>()
})
.collect();

self.cx.cache.exact_paths = self.exact_paths;
top_level_module
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/rustdoc/doc-cfg-hide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![crate_name = "oud"]
#![feature(doc_cfg, doc_cfg_hide)]

#![doc(cfg_hide(feature = "solecism"))]

// @has 'oud/struct.Solecism.html'
// @count - '//*[@class="stab portability"]' 0
// compile-flags:--cfg feature="solecism"
#[cfg(feature = "solecism")]
pub struct Solecism;

// @has 'oud/struct.Scribacious.html'
// @count - '//*[@class="stab portability"]' 1
// @matches - '//*[@class="stab portability"]' 'crate feature solecism'
#[cfg(feature = "solecism")]
#[doc(cfg(feature = "solecism"))]
pub struct Scribacious;

// @has 'oud/struct.Hyperdulia.html'
// @count - '//*[@class="stab portability"]' 1
// @matches - '//*[@class="stab portability"]' 'crate feature hyperdulia'
// compile-flags:--cfg feature="hyperdulia"
#[cfg(feature = "solecism")]
#[cfg(feature = "hyperdulia")]
pub struct Hyperdulia;

// @has 'oud/struct.Oystercatcher.html'
// @count - '//*[@class="stab portability"]' 1
// @matches - '//*[@class="stab portability"]' 'crate features solecism and oystercatcher'
// compile-flags:--cfg feature="oystercatcher"
#[cfg(all(feature = "solecism", feature = "oystercatcher"))]
pub struct Oystercatcher;
0