Skip to content

Commit f58c8ab

Browse files
Fix duplicated attributes for first reexport
1 parent eebdfb5 commit f58c8ab

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/librustdoc/clean/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -2114,17 +2114,29 @@ fn get_all_import_attributes<'hir>(
21142114
attributes: &mut Vec<ast::Attribute>,
21152115
is_inline: bool,
21162116
) {
2117+
let mut first = true;
21172118
let hir_map = tcx.hir();
21182119
let mut visitor = OneLevelVisitor::new(hir_map, target_def_id);
21192120
let mut visited = FxHashSet::default();
2121+
21202122
// If the item is an import and has at least a path with two parts, we go into it.
21212123
while let hir::ItemKind::Use(path, _) = item.kind && visited.insert(item.hir_id()) {
2122-
// We add the attributes from this import into the list.
2123-
add_without_unwanted_attributes(attributes, hir_map.attrs(item.hir_id()), is_inline);
2124+
if first {
2125+
// This is the "original" reexport so we get all its attributes without filtering them.
2126+
attributes.extend_from_slice(hir_map.attrs(item.hir_id()));
2127+
first = false;
2128+
} else {
2129+
add_without_unwanted_attributes(attributes, hir_map.attrs(item.hir_id()), is_inline);
2130+
}
21242131

2125-
let def_id = if path.segments.len() > 1 {
2126-
match path.segments[path.segments.len() - 2].res {
2132+
let def_id = if let [.., parent_segment, _] = &path.segments {
2133+
match parent_segment.res {
21272134
hir::def::Res::Def(_, def_id) => def_id,
2135+
_ if parent_segment.ident.name == kw::Crate => {
2136+
// In case the "parent" is the crate, it'll give `Res::Err` so we need to
2137+
// circumvent it this way.
2138+
tcx.parent(item.owner_id.def_id.to_def_id())
2139+
}
21282140
_ => break,
21292141
}
21302142
} else {
@@ -2341,9 +2353,7 @@ fn clean_maybe_renamed_item<'tcx>(
23412353
if let Some(import_id) = import_id &&
23422354
let Some(hir::Node::Item(use_node)) = cx.tcx.hir().find_by_def_id(import_id)
23432355
{
2344-
// First, we add the attributes from the current import.
2345-
extra_attrs.extend_from_slice(inline::load_attrs(cx, import_id.to_def_id()));
2346-
let is_inline = extra_attrs.lists(sym::doc).get_word_attr(sym::inline).is_some();
2356+
let is_inline = inline::load_attrs(cx, import_id.to_def_id()).lists(sym::doc).get_word_attr(sym::inline).is_some();
23472357
// Then we get all the various imports' attributes.
23482358
get_all_import_attributes(use_node, cx.tcx, item.owner_id.def_id, &mut extra_attrs, is_inline);
23492359
add_without_unwanted_attributes(&mut extra_attrs, inline::load_attrs(cx, def_id), is_inline);

0 commit comments

Comments
 (0)