Skip to content

Commit 3c1d700

Browse files
committed
Auto merge of #142848 - yotamofek:pr/rustdoc/clean-cleanups, r=<try>
Misc. cleanups to `librustdoc`s "clean" fns Small cleanups while working on bigger stuff. Might have very slight performance improvement (408dc50 avoids a few unneeded `Clone`s), but I doubt they'll register on a perf run. So this is mostly just making the code (hopefully) a bit nicer.
2 parents a30f178 + eb8e7d4 commit 3c1d700

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ pub const STDLIB_STABLE_CRATES: &[Symbol] = &[sym::std, sym::core, sym::alloc, s
23912391
#[derive(Copy, Clone, Eq, HashStable_Generic, Encodable, Decodable)]
23922392
pub struct Ident {
23932393
// `name` should never be the empty symbol. If you are considering that,
2394-
// you are probably conflating "empty identifer with "no identifier" and
2394+
// you are probably conflating "empty identifer" with "no identifier" and
23952395
// you should use `Option<Ident>` instead.
23962396
pub name: Symbol,
23972397
pub span: Span,

src/librustdoc/clean/mod.rs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn is_glob_import(tcx: TyCtxt<'_>, import_id: LocalDefId) -> bool {
178178
}
179179

180180
fn generate_item_with_correct_attrs(
181-
cx: &mut DocContext<'_>,
181+
cx: &DocContext<'_>,
182182
kind: ItemKind,
183183
def_id: DefId,
184184
name: Symbol,
@@ -198,7 +198,7 @@ fn generate_item_with_correct_attrs(
198198
|| (is_glob_import(cx.tcx, import_id)
199199
&& (cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id)));
200200
let mut attrs = get_all_import_attributes(cx, import_id, def_id, is_inline);
201-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
201+
attrs.extend(add_without_unwanted_attributes(target_attrs, is_inline, None));
202202
attrs
203203
} else {
204204
// We only keep the item's attributes.
@@ -2622,28 +2622,38 @@ pub(crate) fn reexport_chain(
26222622

26232623
/// Collect attributes from the whole import chain.
26242624
fn get_all_import_attributes<'hir>(
2625-
cx: &mut DocContext<'hir>,
2625+
cx: &DocContext<'hir>,
26262626
import_def_id: LocalDefId,
26272627
target_def_id: DefId,
26282628
is_inline: bool,
26292629
) -> Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)> {
2630-
let mut attrs = Vec::new();
2631-
let mut first = true;
2630+
let mut attrs = None;
26322631
for def_id in reexport_chain(cx.tcx, import_def_id, target_def_id)
26332632
.iter()
2634-
.flat_map(|reexport| reexport.id())
2633+
.filter_map(|reexport| reexport.id())
26352634
{
26362635
let import_attrs = inline::load_attrs(cx, def_id);
2637-
if first {
2638-
// This is the "original" reexport so we get all its attributes without filtering them.
2639-
attrs = import_attrs.iter().map(|attr| (Cow::Borrowed(attr), Some(def_id))).collect();
2640-
first = false;
2641-
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
2642-
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
2643-
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
2636+
match &mut attrs {
2637+
None => {
2638+
// This is the "original" reexport so we get all its attributes without filtering them.
2639+
attrs = Some(
2640+
import_attrs
2641+
.iter()
2642+
.map(|attr| (Cow::Borrowed(attr), Some(def_id)))
2643+
.collect::<Vec<_>>(),
2644+
);
2645+
}
2646+
Some(attrs) if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) => {
2647+
attrs.extend(add_without_unwanted_attributes(
2648+
import_attrs,
2649+
is_inline,
2650+
Some(def_id),
2651+
));
2652+
}
2653+
Some(_) => {}
26442654
}
26452655
}
2646-
attrs
2656+
attrs.unwrap_or_default()
26472657
}
26482658

26492659
fn filter_tokens_from_list(
@@ -2671,31 +2681,26 @@ fn filter_tokens_from_list(
26712681

26722682
fn filter_doc_attr_ident(ident: Symbol, is_inline: bool) -> bool {
26732683
if is_inline {
2674-
ident == sym::hidden || ident == sym::inline || ident == sym::no_inline
2684+
matches!(ident, sym::hidden | sym::inline | sym::no_inline)
26752685
} else {
2676-
ident == sym::cfg
2686+
matches!(ident, sym::cfg)
26772687
}
26782688
}
26792689

2680-
/// Remove attributes from `normal` that should not be inherited by `use` re-export.
2681-
/// Before calling this function, make sure `normal` is a `#[doc]` attribute.
2690+
/// Assuming `args` are the arguments to a `doc` attribute (i.e. `#[doc(args...)]`),
2691+
/// remove attribute arguments that should not be inherited by `use` re-export.
26822692
fn filter_doc_attr(args: &mut hir::AttrArgs, is_inline: bool) {
2693+
fn ident(tt: &TokenTree) -> Option<Symbol> {
2694+
match *tt {
2695+
TokenTree::Token(Token { kind: TokenKind::Ident(ident, _), .. }, ..) => Some(ident),
2696+
_ => None,
2697+
}
2698+
}
2699+
26832700
match args {
26842701
hir::AttrArgs::Delimited(args) => {
2685-
let tokens = filter_tokens_from_list(&args.tokens, |token| {
2686-
!matches!(
2687-
token,
2688-
TokenTree::Token(
2689-
Token {
2690-
kind: TokenKind::Ident(
2691-
ident,
2692-
_,
2693-
),
2694-
..
2695-
},
2696-
_,
2697-
) if filter_doc_attr_ident(*ident, is_inline),
2698-
)
2702+
let tokens = filter_tokens_from_list(&args.tokens, |tt| {
2703+
!ident(tt).is_some_and(|ident| filter_doc_attr_ident(ident, is_inline))
26992704
});
27002705
args.tokens = TokenStream::new(tokens);
27012706
}
@@ -2724,34 +2729,32 @@ fn filter_doc_attr(args: &mut hir::AttrArgs, is_inline: bool) {
27242729
/// * `doc(no_inline)`
27252730
/// * `doc(hidden)`
27262731
fn add_without_unwanted_attributes<'hir>(
2727-
attrs: &mut Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)>,
27282732
new_attrs: &'hir [hir::Attribute],
27292733
is_inline: bool,
27302734
import_parent: Option<DefId>,
2731-
) {
2732-
for attr in new_attrs {
2735+
) -> impl Iterator<Item = (Cow<'hir, hir::Attribute>, Option<DefId>)> {
2736+
new_attrs.iter().filter_map(move |attr| {
27332737
if attr.is_doc_comment() {
2734-
attrs.push((Cow::Borrowed(attr), import_parent));
2735-
continue;
2738+
return Some((Cow::Borrowed(attr), import_parent));
27362739
}
2737-
let mut attr = attr.clone();
27382740
match attr {
2739-
hir::Attribute::Unparsed(ref mut normal) if let [ident] = &*normal.path.segments => {
2741+
hir::Attribute::Unparsed(normal) if let [ident] = &*normal.path.segments => {
27402742
let ident = ident.name;
27412743
if ident == sym::doc {
2744+
let mut normal = normal.clone();
27422745
filter_doc_attr(&mut normal.args, is_inline);
2743-
attrs.push((Cow::Owned(attr), import_parent));
2746+
Some((Cow::Owned(hir::Attribute::Unparsed(normal)), import_parent))
27442747
} else if is_inline || ident != sym::cfg_trace {
27452748
// If it's not a `cfg()` attribute, we keep it.
2746-
attrs.push((Cow::Owned(attr), import_parent));
2749+
Some((Cow::Borrowed(attr), import_parent))
2750+
} else {
2751+
None
27472752
}
27482753
}
2749-
hir::Attribute::Parsed(..) if is_inline => {
2750-
attrs.push((Cow::Owned(attr), import_parent));
2751-
}
2752-
_ => {}
2754+
hir::Attribute::Parsed(..) if is_inline => Some((Cow::Borrowed(attr), import_parent)),
2755+
_ => None,
27532756
}
2754-
}
2757+
})
27552758
}
27562759

27572760
fn clean_maybe_renamed_item<'tcx>(

0 commit comments

Comments
 (0)