Skip to content

Commit 9fe8aae

Browse files
Correctly handle doc items inlining
1 parent cf2a260 commit 9fe8aae

File tree

3 files changed

+20
-50
lines changed

3 files changed

+20
-50
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ fn generate_item_with_correct_attrs(
196196
// For glob re-exports the item may or may not exist to be re-exported (potentially the
197197
// cfgs on the path up until the glob can be removed, and only cfgs on the globbed item
198198
// itself matter), for non-inlined re-exports see #85043.
199-
let import_is_inline = find_attr!(inline::load_attrs(cx, import_id.to_def_id()), AttributeKind::Doc(d) if d.inline.is_some())
200-
|| (is_glob_import(cx.tcx, import_id)
201-
&& (cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id)));
199+
let import_is_inline = find_attr!(
200+
inline::load_attrs(cx, import_id.to_def_id()),
201+
AttributeKind::Doc(d)
202+
if d.inline.is_some_and(|(inline, _)| inline == DocInline::Inline)
203+
) || (is_glob_import(cx.tcx, import_id)
204+
&& (cx.document_hidden() || !cx.tcx.is_doc_hidden(def_id)));
202205
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
203206
is_inline = is_inline || import_is_inline;
204207
}
@@ -2666,10 +2669,10 @@ fn add_without_unwanted_attributes<'hir>(
26662669
let DocAttribute { hidden, inline, cfg, .. } = d;
26672670
let mut attr = DocAttribute::default();
26682671
if is_inline {
2672+
attr.cfg = cfg.clone();
2673+
} else {
26692674
attr.inline = inline.clone();
26702675
attr.hidden = hidden.clone();
2671-
} else {
2672-
attr.cfg = cfg.clone();
26732676
}
26742677
attrs.push((
26752678
Cow::Owned(hir::Attribute::Parsed(AttributeKind::Doc(Box::new(attr)))),
@@ -2914,10 +2917,12 @@ fn clean_extern_crate<'tcx>(
29142917
let attrs = cx.tcx.hir_attrs(krate.hir_id());
29152918
let ty_vis = cx.tcx.visibility(krate.owner_id);
29162919
let please_inline = ty_vis.is_public()
2917-
&& attrs.iter().any(|a| matches!(
2920+
&& attrs.iter().any(|a| {
2921+
matches!(
29182922
a,
2919-
hir::Attribute::Parsed(AttributeKind::Doc(d)) if d.inline.is_some_and(|(i, _)| i == DocInline::Inline))
2920-
)
2923+
hir::Attribute::Parsed(AttributeKind::Doc(d))
2924+
if d.inline.is_some_and(|(i, _)| i == DocInline::Inline))
2925+
})
29212926
&& !cx.is_json_output();
29222927

29232928
let krate_owner_def_id = krate.owner_id.def_id;

src/librustdoc/clean/types.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -929,37 +929,6 @@ pub(crate) struct Module {
929929
pub(crate) span: Span,
930930
}
931931

932-
pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
933-
attrs: I,
934-
name: Symbol,
935-
) -> impl Iterator<Item = ast::MetaItemInner> + use<'a, I> {
936-
attrs
937-
.into_iter()
938-
.filter(move |attr| attr.has_name(name))
939-
.filter_map(ast::attr::AttributeExt::meta_item_list)
940-
.flatten()
941-
}
942-
943-
pub(crate) trait NestedAttributesExt {
944-
/// Returns `true` if the attribute list contains a specific `word`
945-
fn has_word(self, word: Symbol) -> bool
946-
where
947-
Self: Sized,
948-
{
949-
<Self as NestedAttributesExt>::get_word_attr(self, word).is_some()
950-
}
951-
952-
/// Returns `Some(attr)` if the attribute list contains 'attr'
953-
/// corresponding to a specific `word`
954-
fn get_word_attr(self, word: Symbol) -> Option<ast::MetaItemInner>;
955-
}
956-
957-
impl<I: Iterator<Item = ast::MetaItemInner>> NestedAttributesExt for I {
958-
fn get_word_attr(mut self, word: Symbol) -> Option<ast::MetaItemInner> {
959-
self.find(|attr| attr.is_word() && attr.has_name(word))
960-
}
961-
}
962-
963932
/// A link that has not yet been rendered.
964933
///
965934
/// This link will be turned into a rendered link by [`Item::links`].

src/librustdoc/visit_ast.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
44
use std::mem;
55

6+
use rustc_ast::attr::AttributeExt;
67
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
78
use rustc_hir as hir;
8-
use rustc_hir::attrs::AttributeKind;
9+
use rustc_hir::attrs::{AttributeKind, DocInline};
910
use rustc_hir::def::{DefKind, MacroKinds, Res};
1011
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
1112
use rustc_hir::intravisit::{Visitor, walk_body, walk_item};
@@ -14,11 +15,11 @@ use rustc_middle::hir::nested_filter;
1415
use rustc_middle::ty::TyCtxt;
1516
use rustc_span::Span;
1617
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
17-
use rustc_span::symbol::{Symbol, kw, sym};
18+
use rustc_span::symbol::{Symbol, kw};
1819
use tracing::debug;
1920

21+
use crate::clean::reexport_chain;
2022
use crate::clean::utils::{inherits_doc_hidden, should_ignore_res};
21-
use crate::clean::{NestedAttributesExt, hir_attr_lists, reexport_chain};
2223
use crate::core;
2324

2425
/// This module is used to store stuff from Rust's AST in a more convenient
@@ -246,8 +247,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
246247
let document_hidden = self.cx.document_hidden();
247248
let use_attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
248249
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
249-
let is_no_inline = hir_attr_lists(use_attrs, sym::doc).has_word(sym::no_inline)
250-
|| (document_hidden && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden));
250+
let is_no_inline = find_attr!(use_attrs, AttributeKind::Doc(d) if d.inline.is_some_and(|(inline, _)| inline == DocInline::NoInline))
251+
|| (document_hidden && use_attrs.iter().any(|attr| attr.is_doc_hidden()));
251252

252253
if is_no_inline {
253254
return false;
@@ -464,12 +465,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
464465
// If there was a private module in the current path then don't bother inlining
465466
// anything as it will probably be stripped anyway.
466467
if is_pub && self.inside_public_path {
467-
let please_inline = attrs.iter().any(|item| match item.meta_item_list() {
468-
Some(ref list) if item.has_name(sym::doc) => {
469-
list.iter().any(|i| i.has_name(sym::inline))
470-
}
471-
_ => false,
472-
});
468+
let please_inline = find_attr!(attrs, AttributeKind::Doc(d) if d.inline.is_some_and(|(inline, _)| inline == DocInline::Inline));
473469
let ident = match kind {
474470
hir::UseKind::Single(ident) => Some(ident.name),
475471
hir::UseKind::Glob => None,

0 commit comments

Comments
 (0)