Skip to content

Commit f93dbfc

Browse files
authored
Rollup merge of #120501 - GuillaumeGomez:glob-reexport-attr-merge-bugfix, r=notriddle
rustdoc: Correctly handle attribute merge if this is a glob reexport Fixes #120487. The regression was introduced in #113091. Only non-glob reexports should have been impacted. cc ```@Nemo157``` r? ```@notriddle```
2 parents 684756b + 024364a commit f93dbfc

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/librustdoc/clean/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
148148
)
149149
}
150150

151+
fn is_glob_import(tcx: TyCtxt<'_>, import_id: LocalDefId) -> bool {
152+
if let Some(node) = tcx.opt_hir_node_by_def_id(import_id)
153+
&& let hir::Node::Item(item) = node
154+
&& let hir::ItemKind::Use(_, use_kind) = item.kind
155+
{
156+
use_kind == hir::UseKind::Glob
157+
} else {
158+
false
159+
}
160+
}
161+
151162
fn generate_item_with_correct_attrs(
152163
cx: &mut DocContext<'_>,
153164
kind: ItemKind,
@@ -158,10 +169,17 @@ fn generate_item_with_correct_attrs(
158169
) -> Item {
159170
let target_attrs = inline::load_attrs(cx, def_id);
160171
let attrs = if let Some(import_id) = import_id {
172+
// glob reexports are treated the same as `#[doc(inline)]` items.
173+
//
174+
// For glob re-exports the item may or may not exist to be re-exported (potentially the cfgs
175+
// on the path up until the glob can be removed, and only cfgs on the globbed item itself
176+
// matter), for non-inlined re-exports see #85043.
161177
let is_inline = inline::load_attrs(cx, import_id.to_def_id())
162178
.lists(sym::doc)
163179
.get_word_attr(sym::inline)
164-
.is_some();
180+
.is_some()
181+
|| (is_glob_import(cx.tcx, import_id)
182+
&& (cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id)));
165183
let mut attrs = get_all_import_attributes(cx, import_id, def_id, is_inline);
166184
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
167185
attrs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This test ensures that non-glob reexports don't get their attributes merge with
2+
// the reexported item whereas glob reexports do.
3+
// Regression test for <https://github.com/rust-lang/rust/issues/120487>.
4+
5+
#![crate_name = "foo"]
6+
#![feature(doc_cfg)]
7+
8+
// @has 'foo/index.html'
9+
// There are two items.
10+
// @count - '//*[@class="item-table"]//div[@class="item-name"]' 2
11+
// Only one of them should have an attribute.
12+
// @count - '//*[@class="item-table"]//div[@class="item-name"]/*[@class="stab portability"]' 1
13+
14+
mod a {
15+
#[doc(cfg(not(feature = "a")))]
16+
#[cfg(not(feature = "a"))]
17+
pub struct Test1;
18+
}
19+
20+
mod b {
21+
#[doc(cfg(not(feature = "a")))]
22+
#[cfg(not(feature = "a"))]
23+
pub struct Test2;
24+
}
25+
26+
// @has 'foo/struct.Test1.html'
27+
// @count - '//*[@id="main-content"]/*[@class="item-info"]' 1
28+
// @has - '//*[@id="main-content"]/*[@class="item-info"]' 'Available on non-crate feature a only.'
29+
pub use a::*;
30+
// @has 'foo/struct.Test2.html'
31+
// @count - '//*[@id="main-content"]/*[@class="item-info"]' 0
32+
pub use b::Test2;

0 commit comments

Comments
 (0)