Skip to content

Commit 7cb641c

Browse files
Don't merge cfg and doc(cfg) attributes for re-exports
1 parent 95978b3 commit 7cb641c

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
@@ -2355,6 +2355,38 @@ fn filter_tokens_from_list(
23552355
tokens
23562356
}
23572357

2358+
fn filter_doc_attr_ident(ident: Symbol, is_inline: bool) -> bool {
2359+
if is_inline {
2360+
ident == sym::hidden || ident == sym::inline || ident == sym::no_inline
2361+
} else {
2362+
ident == sym::cfg
2363+
}
2364+
}
2365+
2366+
fn filter_doc_attr(normal: &mut ast::NormalAttr, is_inline: bool) {
2367+
match normal.item.args {
2368+
ast::AttrArgs::Delimited(ref mut args) => {
2369+
let tokens = filter_tokens_from_list(args.tokens.clone(), |token| {
2370+
!matches!(
2371+
token,
2372+
TokenTree::Token(
2373+
Token {
2374+
kind: TokenKind::Ident(
2375+
ident,
2376+
_,
2377+
),
2378+
..
2379+
},
2380+
_,
2381+
) if filter_doc_attr_ident(*ident, is_inline),
2382+
)
2383+
});
2384+
args.tokens = TokenStream::new(tokens);
2385+
}
2386+
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {}
2387+
}
2388+
}
2389+
23582390
/// When inlining items, we merge their attributes (and all the reexports attributes too) with the
23592391
/// final reexport. For example:
23602392
///
@@ -2381,13 +2413,6 @@ fn add_without_unwanted_attributes<'hir>(
23812413
is_inline: bool,
23822414
import_parent: Option<DefId>,
23832415
) {
2384-
// If it's not `#[doc(inline)]`, we don't want all attributes, otherwise we keep everything.
2385-
if !is_inline {
2386-
for attr in new_attrs {
2387-
attrs.push((Cow::Borrowed(attr), import_parent));
2388-
}
2389-
return;
2390-
}
23912416
for attr in new_attrs {
23922417
if matches!(attr.kind, ast::AttrKind::DocComment(..)) {
23932418
attrs.push((Cow::Borrowed(attr), import_parent));
@@ -2396,34 +2421,14 @@ fn add_without_unwanted_attributes<'hir>(
23962421
let mut attr = attr.clone();
23972422
match attr.kind {
23982423
ast::AttrKind::Normal(ref mut normal) => {
2399-
if let [ident] = &*normal.item.path.segments &&
2400-
let ident = ident.ident.name &&
2401-
ident == sym::doc
2402-
{
2403-
match normal.item.args {
2404-
ast::AttrArgs::Delimited(ref mut args) => {
2405-
let tokens =
2406-
filter_tokens_from_list(args.tokens.clone(), |token| {
2407-
!matches!(
2408-
token,
2409-
TokenTree::Token(
2410-
Token {
2411-
kind: TokenKind::Ident(
2412-
sym::hidden | sym::inline | sym::no_inline,
2413-
_,
2414-
),
2415-
..
2416-
},
2417-
_,
2418-
),
2419-
)
2420-
});
2421-
args.tokens = TokenStream::new(tokens);
2422-
attrs.push((Cow::Owned(attr), import_parent));
2423-
}
2424-
ast::AttrArgs::Empty | ast::AttrArgs::Eq(..) => {
2425-
attrs.push((Cow::Owned(attr), import_parent));
2426-
}
2424+
if let [ident] = &*normal.item.path.segments {
2425+
let ident = ident.ident.name;
2426+
if ident == sym::doc {
2427+
filter_doc_attr(normal, is_inline);
2428+
attrs.push((Cow::Owned(attr), import_parent));
2429+
} else if ident != sym::cfg {
2430+
// If it's not a `cfg()` attribute, we keep it.
2431+
attrs.push((Cow::Owned(attr), import_parent));
24272432
}
24282433
}
24292434
}

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;
@@ -465,16 +463,9 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
465463

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

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

0 commit comments

Comments
 (0)