Skip to content

Commit 14ed92c

Browse files
Don't merge cfg and doc(cfg) attributes for re-exports
1 parent 73bc121 commit 14ed92c

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

src/librustdoc/clean/mod.rs

+40-34
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,38 @@ fn filter_tokens_from_list(
26482648
tokens
26492649
}
26502650

2651+
fn filter_doc_attr_ident(ident: Symbol, is_inline: bool) -> bool {
2652+
if is_inline {
2653+
ident == sym::hidden || ident == sym::inline || ident == sym::no_inline
2654+
} else {
2655+
ident == sym::cfg
2656+
}
2657+
}
2658+
2659+
fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
2660+
match normal.item.args {
2661+
ast::AttrArgs::Delimited(ref mut args) => {
2662+
let tokens = filter_tokens_from_list(&args.tokens, |token| {
2663+
!matches!(
2664+
token,
2665+
TokenTree::Token(
2666+
Token {
2667+
kind: TokenKind::Ident(
2668+
ident,
2669+
_,
2670+
),
2671+
..
2672+
},
2673+
_,
2674+
) if filter_doc_attr_ident(*ident, is_inline),
2675+
)
2676+
});
2677+
args.tokens = TokenStream::new(tokens);
2678+
}
2679+
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {}
2680+
}
2681+
}
2682+
26512683
/// When inlining items, we merge their attributes (and all the reexports attributes too) with the
26522684
/// final reexport. For example:
26532685
///
@@ -2674,13 +2706,6 @@ fn add_without_unwanted_attributes<'hir>(
26742706
is_inline: bool,
26752707
import_parent: Option<DefId>,
26762708
) {
2677-
// If it's not `#[doc(inline)]`, we don't want all attributes, otherwise we keep everything.
2678-
if !is_inline {
2679-
for attr in new_attrs {
2680-
attrs.push((Cow::Borrowed(attr), import_parent));
2681-
}
2682-
return;
2683-
}
26842709
for attr in new_attrs {
26852710
if matches!(attr.kind, ast::AttrKind::DocComment(..)) {
26862711
attrs.push((Cow::Borrowed(attr), import_parent));
@@ -2689,33 +2714,14 @@ fn add_without_unwanted_attributes<'hir>(
26892714
let mut attr = attr.clone();
26902715
match attr.kind {
26912716
ast::AttrKind::Normal(ref mut normal) => {
2692-
if let [ident] = &*normal.item.path.segments
2693-
&& let ident = ident.ident.name
2694-
&& ident == sym::doc
2695-
{
2696-
match normal.item.args {
2697-
ast::AttrArgs::Delimited(ref mut args) => {
2698-
let tokens = filter_tokens_from_list(&args.tokens, |token| {
2699-
!matches!(
2700-
token,
2701-
TokenTree::Token(
2702-
Token {
2703-
kind: TokenKind::Ident(
2704-
sym::hidden | sym::inline | sym::no_inline,
2705-
_,
2706-
),
2707-
..
2708-
},
2709-
_,
2710-
),
2711-
)
2712-
});
2713-
args.tokens = TokenStream::new(tokens);
2714-
attrs.push((Cow::Owned(attr), import_parent));
2715-
}
2716-
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {
2717-
attrs.push((Cow::Owned(attr), import_parent));
2718-
}
2717+
if let [ident] = &*normal.item.path.segments {
2718+
let ident = ident.ident.name;
2719+
if ident == sym::doc {
2720+
filter_doc_attr(normal, is_inline);
2721+
attrs.push((Cow::Owned(attr), import_parent));
2722+
} else if ident != sym::cfg {
2723+
// If it's not a `cfg()` attribute, we keep it.
2724+
attrs.push((Cow::Owned(attr), import_parent));
27192725
}
27202726
}
27212727
}

src/librustdoc/html/render/print_item.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use clean::AttributesExt;
2-
31
use rustc_data_structures::captures::Captures;
42
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
53
use rustc_hir as hir;
@@ -464,16 +462,9 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
464462

465463
clean::ImportItem(ref import) => {
466464
let stab_tags = if let Some(import_def_id) = import.source.did {
467-
let ast_attrs = tcx.get_attrs_unchecked(import_def_id);
468-
let import_attrs = Box::new(clean::Attributes::from_ast(ast_attrs));
469-
470465
// Just need an item with the correct def_id and attrs
471-
let import_item = clean::Item {
472-
item_id: import_def_id.into(),
473-
attrs: import_attrs,
474-
cfg: ast_attrs.cfg(tcx, &cx.cache().hidden_cfg),
475-
..myitem.clone()
476-
};
466+
let import_item =
467+
clean::Item { item_id: import_def_id.into(), ..myitem.clone() };
477468

478469
let stab_tags = Some(extra_info_tags(&import_item, item, tcx).to_string());
479470
stab_tags

0 commit comments

Comments
 (0)