Skip to content

Commit f92b32c

Browse files
committed
rustdoc: Eliminate AttributesExt
The new code is more explicit and avoids trait magic that added needless complexity to this part of rustdoc.
1 parent 5ccd6c0 commit f92b32c

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,12 @@ pub(crate) fn merge_attrs(
408408
} else {
409409
Attributes::from_hir(&both)
410410
},
411-
extract_cfg_from_attrs(&both[..], cx.tcx, &cx.cache.hidden_cfg),
411+
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
412412
)
413413
} else {
414414
(
415415
Attributes::from_hir(old_attrs),
416-
extract_cfg_from_attrs(&old_attrs[..], cx.tcx, &cx.cache.hidden_cfg),
416+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
417417
)
418418
}
419419
}

src/librustdoc/clean/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ fn generate_item_with_correct_attrs(
198198
// We only keep the item's attributes.
199199
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
200200
};
201-
202-
let cfg = extract_cfg_from_attrs(&attrs[..], cx.tcx, &cx.cache.hidden_cfg);
201+
let cfg = extract_cfg_from_attrs(
202+
attrs.iter().map(move |(attr, _)| match attr {
203+
Cow::Borrowed(attr) => *attr,
204+
Cow::Owned(attr) => attr,
205+
}),
206+
cx.tcx,
207+
&cx.cache.hidden_cfg,
208+
);
203209
let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
204210

205211
let name = renamed.or(Some(name));

src/librustdoc/clean/types.rs

+10-41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Cow;
21
use std::hash::Hash;
32
use std::path::PathBuf;
43
use std::sync::{Arc, OnceLock as OnceCell};
@@ -479,7 +478,7 @@ impl Item {
479478
name,
480479
kind,
481480
Attributes::from_hir(hir_attrs),
482-
extract_cfg_from_attrs(hir_attrs, cx.tcx, &cx.cache.hidden_cfg),
481+
extract_cfg_from_attrs(hir_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
483482
)
484483
}
485484

@@ -979,27 +978,19 @@ pub(crate) struct Module {
979978
pub(crate) span: Span,
980979
}
981980

982-
pub(crate) trait AttributesExt {
983-
type Attributes<'a>: Iterator<Item = &'a hir::Attribute>
984-
where
985-
Self: 'a;
986-
987-
fn iter(&self) -> Self::Attributes<'_>;
988-
}
989-
990-
pub fn hir_attr_lists<A: AttributesExt + ?Sized>(
991-
attrs: &A,
981+
pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
982+
attrs: I,
992983
name: Symbol,
993-
) -> impl Iterator<Item = ast::MetaItemInner> + use<'_, A> {
984+
) -> impl Iterator<Item = ast::MetaItemInner> + use<'a, I> {
994985
attrs
995-
.iter()
986+
.into_iter()
996987
.filter(move |attr| attr.has_name(name))
997988
.filter_map(ast::attr::AttributeExt::meta_item_list)
998989
.flatten()
999990
}
1000991

1001-
pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
1002-
attrs: &A,
992+
pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> + Clone>(
993+
attrs: I,
1003994
tcx: TyCtxt<'_>,
1004995
hidden_cfg: &FxHashSet<Cfg>,
1005996
) -> Option<Arc<Cfg>> {
@@ -1018,7 +1009,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10181009

10191010
let mut cfg = if doc_cfg_active || doc_auto_cfg_active {
10201011
let mut doc_cfg = attrs
1021-
.iter()
1012+
.clone()
10221013
.filter(|attr| attr.has_name(sym::doc))
10231014
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
10241015
.filter(|attr| attr.has_name(sym::cfg))
@@ -1031,7 +1022,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10311022
// If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
10321023
// `doc(cfg())` overrides `cfg()`).
10331024
attrs
1034-
.iter()
1025+
.clone()
10351026
.filter(|attr| attr.has_name(sym::cfg))
10361027
.filter_map(|attr| single(attr.meta_item_list()?))
10371028
.filter_map(|attr| Cfg::parse_without(attr.meta_item()?, hidden_cfg).ok().flatten())
@@ -1043,7 +1034,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10431034
Cfg::True
10441035
};
10451036

1046-
for attr in attrs.iter() {
1037+
for attr in attrs.clone() {
10471038
// #[doc]
10481039
if attr.doc_str().is_none() && attr.has_name(sym::doc) {
10491040
// #[doc(...)]
@@ -1090,28 +1081,6 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10901081
if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) }
10911082
}
10921083

1093-
impl AttributesExt for [hir::Attribute] {
1094-
type Attributes<'a> = impl Iterator<Item = &'a hir::Attribute> + 'a;
1095-
1096-
fn iter(&self) -> Self::Attributes<'_> {
1097-
self.iter()
1098-
}
1099-
}
1100-
1101-
impl AttributesExt for [(Cow<'_, hir::Attribute>, Option<DefId>)] {
1102-
type Attributes<'a>
1103-
= impl Iterator<Item = &'a hir::Attribute> + 'a
1104-
where
1105-
Self: 'a;
1106-
1107-
fn iter(&self) -> Self::Attributes<'_> {
1108-
self.iter().map(move |(attr, _)| match attr {
1109-
Cow::Borrowed(attr) => *attr,
1110-
Cow::Owned(attr) => attr,
1111-
})
1112-
}
1113-
}
1114-
11151084
pub(crate) trait NestedAttributesExt {
11161085
/// Returns `true` if the attribute list contains a specific `word`
11171086
fn has_word(self, word: Symbol) -> bool

src/librustdoc/doctest/rust.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ impl HirCollector<'_> {
9696
nested: F,
9797
) {
9898
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
99-
if let Some(ref cfg) = extract_cfg_from_attrs(ast_attrs, self.tcx, &FxHashSet::default()) {
99+
if let Some(ref cfg) =
100+
extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
101+
{
100102
if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
101103
return;
102104
}

0 commit comments

Comments
 (0)