Skip to content

Commit 374f798

Browse files
Correctly handle reexports of #[doc(hidden)] is reexport does not use #[doc(inline)]
1 parent 1ec1d94 commit 374f798

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/librustdoc/clean/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -2100,14 +2100,15 @@ fn get_all_import_attributes<'hir>(
21002100
tcx: TyCtxt<'hir>,
21012101
target_def_id: LocalDefId,
21022102
attributes: &mut Vec<ast::Attribute>,
2103+
is_inline: bool,
21032104
) {
21042105
let hir_map = tcx.hir();
21052106
let mut visitor = OneLevelVisitor::new(hir_map, target_def_id);
21062107
let mut visited = FxHashSet::default();
21072108
// If the item is an import and has at least a path with two parts, we go into it.
21082109
while let hir::ItemKind::Use(path, _) = item.kind && visited.insert(item.hir_id()) {
21092110
// We add the attributes from this import into the list.
2110-
add_without_unwanted_attributes(attributes, hir_map.attrs(item.hir_id()));
2111+
add_without_unwanted_attributes(attributes, hir_map.attrs(item.hir_id()), is_inline);
21112112

21122113
let def_id = if path.segments.len() > 1 {
21132114
match path.segments[path.segments.len() - 2].res {
@@ -2189,7 +2190,16 @@ fn filter_tokens_from_list(
21892190
/// * `doc(inline)`
21902191
/// * `doc(no_inline)`
21912192
/// * `doc(hidden)`
2192-
fn add_without_unwanted_attributes(attrs: &mut Vec<ast::Attribute>, new_attrs: &[ast::Attribute]) {
2193+
fn add_without_unwanted_attributes(
2194+
attrs: &mut Vec<ast::Attribute>,
2195+
new_attrs: &[ast::Attribute],
2196+
is_inline: bool,
2197+
) {
2198+
// If it's `#[doc(inline)]`, we don't want all attributes, otherwise we keep everything.
2199+
if !is_inline {
2200+
attrs.extend_from_slice(new_attrs);
2201+
return;
2202+
}
21932203
for attr in new_attrs {
21942204
let mut attr = attr.clone();
21952205
match attr.kind {
@@ -2321,9 +2331,10 @@ fn clean_maybe_renamed_item<'tcx>(
23212331
{
23222332
// First, we add the attributes from the current import.
23232333
extra_attrs.extend_from_slice(inline::load_attrs(cx, import_id.to_def_id()));
2334+
let is_inline = extra_attrs.lists(sym::doc).get_word_attr(sym::inline).is_some();
23242335
// Then we get all the various imports' attributes.
2325-
get_all_import_attributes(use_node, cx.tcx, item.owner_id.def_id, &mut extra_attrs);
2326-
add_without_unwanted_attributes(&mut extra_attrs, inline::load_attrs(cx, def_id));
2336+
get_all_import_attributes(use_node, cx.tcx, item.owner_id.def_id, &mut extra_attrs, is_inline);
2337+
add_without_unwanted_attributes(&mut extra_attrs, inline::load_attrs(cx, def_id), is_inline);
23272338
} else {
23282339
// We only keep the item's attributes.
23292340
extra_attrs.extend_from_slice(inline::load_attrs(cx, def_id));

tests/rustdoc/reexport-attr-merge.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Regression test for <https://github.com/rust-lang/rust/issues/59368>.
2-
// The goal is to ensure that `doc(hidden)`, `doc(inline)` and `doc(no_inline`)
2+
// The goal is to ensure that `doc(hidden)`, `doc(inline)` and `doc(no_inline)`
3+
// are not copied from an item when inlined.
34

45
#![crate_name = "foo"]
56
#![feature(doc_cfg)]
@@ -15,12 +16,18 @@ pub use Foo as Foo1;
1516
#[doc(hidden, inline)]
1617
pub use Foo1 as Foo2;
1718

18-
// First we ensure that none of the other items are generated.
19-
// @count - '//a[@class="struct"]' 1
19+
// First we ensure that only the reexport `Bar2` and the inlined struct `Bar`
20+
// are inlined.
21+
// @count - '//a[@class="struct"]' 2
2022
// Then we check that both `cfg` are displayed.
2123
// @has - '//*[@class="stab portability"]' 'foo'
2224
// @has - '//*[@class="stab portability"]' 'bar'
2325
// And finally we check that the only element displayed is `Bar`.
2426
// @has - '//a[@class="struct"]' 'Bar'
2527
#[doc(inline)]
2628
pub use Foo2 as Bar;
29+
30+
// This one should appear but `Bar2` won't be linked because there is no
31+
// `#[doc(inline)]`.
32+
// @has - '//*[@id="reexport.Bar2"]' 'pub use Foo2 as Bar2;'
33+
pub use Foo2 as Bar2;

0 commit comments

Comments
 (0)