Skip to content

Commit 067b7aa

Browse files
authored
Unrolled build for rust-lang#123459
Rollup merge of rust-lang#123459 - GuillaumeGomez:fix-123435, r=notriddle Correctly handle inlining of doc hidden foreign items Fixes rust-lang#123435. In case a foreign item has doc(hidden) attribute, we simply merged its attributes with the re-export's, making it being removed once in the `strip_hidden` pass. The solution was to use the same as for local reexported items: merge attributes, but not some of them (like `doc(hidden)`). I originally checked if we could simply update `Item::is_doc_hidden` method to use `self.inline_stmt_id.is_some_and(|def_id| tcx.is_doc_hidden(def_id))` but unfortunately, it added (local) items that shouldn't be inlined. At least it unifies local and foreign items inlining, which I think is the best course of action here. r? `@notriddle`
2 parents aa6a697 + 5a0be6f commit 067b7aa

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

src/librustdoc/clean/inline.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ pub(crate) fn try_inline(
6363

6464
let import_def_id = attrs.and_then(|(_, def_id)| def_id);
6565

66-
let (attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
67-
6866
let kind = match res {
6967
Res::Def(DefKind::Trait, did) => {
7068
record_extern_fqn(cx, did, ItemType::Trait);
@@ -134,7 +132,11 @@ pub(crate) fn try_inline(
134132
cx.with_param_env(did, |cx| clean::ConstantItem(build_const(cx, did)))
135133
}
136134
Res::Def(DefKind::Macro(kind), did) => {
137-
let mac = build_macro(cx, did, name, import_def_id, kind, attrs.is_doc_hidden());
135+
let is_doc_hidden = cx.tcx.is_doc_hidden(did)
136+
|| attrs_without_docs
137+
.map(|(attrs, _)| attrs)
138+
.is_some_and(|attrs| utils::attrs_have_doc_flag(attrs.iter(), sym::hidden));
139+
let mac = build_macro(cx, did, name, import_def_id, kind, is_doc_hidden);
138140

139141
let type_kind = match kind {
140142
MacroKind::Bang => ItemType::Macro,
@@ -148,8 +150,14 @@ pub(crate) fn try_inline(
148150
};
149151

150152
cx.inlined.insert(did.into());
151-
let mut item =
152-
clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, Box::new(attrs), cfg);
153+
let mut item = crate::clean::generate_item_with_correct_attrs(
154+
cx,
155+
kind,
156+
did,
157+
name,
158+
import_def_id.and_then(|def_id| def_id.as_local()),
159+
None,
160+
);
153161
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
154162
item.inline_stmt_id = import_def_id;
155163
ret.push(item);
@@ -179,6 +187,7 @@ pub(crate) fn try_inline_glob(
179187
.iter()
180188
.filter(|child| !child.reexport_chain.is_empty())
181189
.filter_map(|child| child.res.opt_def_id())
190+
.filter(|def_id| !cx.tcx.is_doc_hidden(def_id))
182191
.collect();
183192
let attrs = cx.tcx.hir().attrs(import.hir_id());
184193
let mut items = build_module_items(

src/librustdoc/clean/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -3007,22 +3007,22 @@ fn clean_use_statement_inner<'tcx>(
30073007
// were specifically asked for it
30083008
denied = true;
30093009
}
3010-
if !denied {
3011-
if let Some(mut items) = inline::try_inline(
3010+
if !denied
3011+
&& let Some(mut items) = inline::try_inline(
30123012
cx,
30133013
path.res,
30143014
name,
30153015
Some((attrs, Some(import_def_id))),
30163016
&mut Default::default(),
3017-
) {
3018-
items.push(Item::from_def_id_and_parts(
3019-
import_def_id,
3020-
None,
3021-
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
3022-
cx,
3023-
));
3024-
return items;
3025-
}
3017+
)
3018+
{
3019+
items.push(Item::from_def_id_and_parts(
3020+
import_def_id,
3021+
None,
3022+
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
3023+
cx,
3024+
));
3025+
return items;
30263026
}
30273027
Import::new_simple(name, resolve_use_source(cx, path), true)
30283028
};

src/librustdoc/clean/utils.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,14 @@ pub(crate) fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Opti
580580
/// This function exists because it runs on `hir::Attributes` whereas the other is a
581581
/// `clean::Attributes` method.
582582
pub(crate) fn has_doc_flag(tcx: TyCtxt<'_>, did: DefId, flag: Symbol) -> bool {
583-
tcx.get_attrs(did, sym::doc)
583+
attrs_have_doc_flag(tcx.get_attrs(did, sym::doc), flag)
584+
}
585+
586+
pub(crate) fn attrs_have_doc_flag<'a>(
587+
mut attrs: impl Iterator<Item = &'a ast::Attribute>,
588+
flag: Symbol,
589+
) -> bool {
590+
attrs
584591
.any(|attr| attr.meta_item_list().is_some_and(|l| rustc_attr::list_contains_name(&l, flag)))
585592
}
586593

src/librustdoc/formats/cache.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ impl Cache {
203203
impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
204204
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
205205
if item.item_id.is_local() {
206-
let is_stripped = matches!(*item.kind, clean::ItemKind::StrippedItem(..));
207206
debug!(
208-
"folding {} (stripped: {is_stripped:?}) \"{:?}\", id {:?}",
207+
"folding {} (stripped: {:?}) \"{:?}\", id {:?}",
209208
item.type_(),
209+
item.is_stripped(),
210210
item.name,
211211
item.item_id
212212
);
@@ -246,13 +246,11 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
246246
// trait.
247247
if let clean::TraitItem(ref t) = *item.kind {
248248
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
249-
}
250-
251-
// Collect all the implementors of traits.
252-
if let clean::ImplItem(ref i) = *item.kind
249+
} else if let clean::ImplItem(ref i) = *item.kind
253250
&& let Some(trait_) = &i.trait_
254251
&& !i.kind.is_blanket()
255252
{
253+
// Collect all the implementors of traits.
256254
self.cache
257255
.implementors
258256
.entry(trait_.def_id())

tests/rustdoc/inline_cross/inline_hidden.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,23 @@
44

55
extern crate rustdoc_hidden;
66

7+
// @has inline_hidden/index.html
8+
// Ensures this item is not inlined.
9+
// @has - '//*[@id="reexport.Foo"]/code' 'pub use rustdoc_hidden::Foo;'
710
#[doc(no_inline)]
811
pub use rustdoc_hidden::Foo;
912

13+
// Even if the foreign item has `doc(hidden)`, we should be able to inline it.
14+
// @has - '//*[@class="item-name"]/a[@class="struct"]' 'Inlined'
15+
#[doc(inline)]
16+
pub use rustdoc_hidden::Foo as Inlined;
17+
18+
// Even with this import, we should not see `Foo`.
19+
// @count - '//*[@class="item-name"]' 4
20+
// @has - '//*[@class="item-name"]/a[@class="struct"]' 'Bar'
21+
// @has - '//*[@class="item-name"]/a[@class="fn"]' 'foo'
22+
pub use rustdoc_hidden::*;
23+
1024
// @has inline_hidden/fn.foo.html
1125
// @!has - '//a/@title' 'Foo'
1226
pub fn foo(_: Foo) {}

0 commit comments

Comments
 (0)