Skip to content

Commit 9b7ff47

Browse files
Don't merge cfg and doc(cfg) attributes for re-exports
1 parent adda05f commit 9b7ff47

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,38 @@ fn filter_tokens_from_list(
26422642
tokens
26432643
}
26442644

2645+
fn filter_doc_attr_ident(ident: Symbol, is_inline: bool) -> bool {
2646+
if is_inline {
2647+
ident == sym::hidden || ident == sym::inline || ident == sym::no_inline
2648+
} else {
2649+
ident == sym::cfg
2650+
}
2651+
}
2652+
2653+
fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
2654+
match normal.item.args {
2655+
ast::AttrArgs::Delimited(ref mut args) => {
2656+
let tokens = filter_tokens_from_list(&args.tokens, |token| {
2657+
!matches!(
2658+
token,
2659+
TokenTree::Token(
2660+
Token {
2661+
kind: TokenKind::Ident(
2662+
ident,
2663+
_,
2664+
),
2665+
..
2666+
},
2667+
_,
2668+
) if filter_doc_attr_ident(*ident, is_inline),
2669+
)
2670+
});
2671+
args.tokens = TokenStream::new(tokens);
2672+
}
2673+
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {}
2674+
}
2675+
}
2676+
26452677
/// When inlining items, we merge their attributes (and all the reexports attributes too) with the
26462678
/// final reexport. For example:
26472679
///
@@ -2668,13 +2700,6 @@ fn add_without_unwanted_attributes<'hir>(
26682700
is_inline: bool,
26692701
import_parent: Option<DefId>,
26702702
) {
2671-
// If it's not `#[doc(inline)]`, we don't want all attributes, otherwise we keep everything.
2672-
if !is_inline {
2673-
for attr in new_attrs {
2674-
attrs.push((Cow::Borrowed(attr), import_parent));
2675-
}
2676-
return;
2677-
}
26782703
for attr in new_attrs {
26792704
if matches!(attr.kind, ast::AttrKind::DocComment(..)) {
26802705
attrs.push((Cow::Borrowed(attr), import_parent));
@@ -2683,34 +2708,14 @@ fn add_without_unwanted_attributes<'hir>(
26832708
let mut attr = attr.clone();
26842709
match attr.kind {
26852710
ast::AttrKind::Normal(ref mut normal) => {
2686-
if let [ident] = &*normal.item.path.segments &&
2687-
let ident = ident.ident.name &&
2688-
ident == sym::doc
2689-
{
2690-
match normal.item.args {
2691-
ast::AttrArgs::Delimited(ref mut args) => {
2692-
let tokens =
2693-
filter_tokens_from_list(&args.tokens, |token| {
2694-
!matches!(
2695-
token,
2696-
TokenTree::Token(
2697-
Token {
2698-
kind: TokenKind::Ident(
2699-
sym::hidden | sym::inline | sym::no_inline,
2700-
_,
2701-
),
2702-
..
2703-
},
2704-
_,
2705-
),
2706-
)
2707-
});
2708-
args.tokens = TokenStream::new(tokens);
2709-
attrs.push((Cow::Owned(attr), import_parent));
2710-
}
2711-
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {
2712-
attrs.push((Cow::Owned(attr), import_parent));
2713-
}
2711+
if let [ident] = &*normal.item.path.segments {
2712+
let ident = ident.ident.name;
2713+
if ident == sym::doc {
2714+
filter_doc_attr(normal, is_inline);
2715+
attrs.push((Cow::Owned(attr), import_parent));
2716+
} else if ident != sym::cfg {
2717+
// If it's not a `cfg()` attribute, we keep it.
2718+
attrs.push((Cow::Owned(attr), import_parent));
27142719
}
27152720
}
27162721
}

src/librustdoc/html/render/print_item.rs

Lines changed: 2 additions & 11 deletions
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)