about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2025-01-12 20:31:53 -0800
committerNoah Lev <camelidcamel@gmail.com>2025-01-13 00:39:00 -0800
commitf92b32c5f64ea900f2cd6936f648f407daee0d25 (patch)
tree36489bbc911731106a581f2ad3e372541c009583
parent5ccd6c04e5b0e81c8a0fdd8c0e9363be49ef053d (diff)
downloadrust-f92b32c5f64ea900f2cd6936f648f407daee0d25.tar.gz
rust-f92b32c5f64ea900f2cd6936f648f407daee0d25.zip
rustdoc: Eliminate `AttributesExt`
The new code is more explicit and avoids trait magic that added needless
complexity to this part of rustdoc.
-rw-r--r--src/librustdoc/clean/inline.rs4
-rw-r--r--src/librustdoc/clean/mod.rs10
-rw-r--r--src/librustdoc/clean/types.rs51
-rw-r--r--src/librustdoc/doctest/rust.rs4
4 files changed, 23 insertions, 46 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index a17e0b1e4cc..3d51ab1967d 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -408,12 +408,12 @@ pub(crate) fn merge_attrs(
             } else {
                 Attributes::from_hir(&both)
             },
-            extract_cfg_from_attrs(&both[..], cx.tcx, &cx.cache.hidden_cfg),
+            extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
         )
     } else {
         (
             Attributes::from_hir(old_attrs),
-            extract_cfg_from_attrs(&old_attrs[..], cx.tcx, &cx.cache.hidden_cfg),
+            extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
         )
     }
 }
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index ed0c566d53d..ed064768c70 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -198,8 +198,14 @@ fn generate_item_with_correct_attrs(
         // We only keep the item's attributes.
         target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
     };
-
-    let cfg = extract_cfg_from_attrs(&attrs[..], cx.tcx, &cx.cache.hidden_cfg);
+    let cfg = extract_cfg_from_attrs(
+        attrs.iter().map(move |(attr, _)| match attr {
+            Cow::Borrowed(attr) => *attr,
+            Cow::Owned(attr) => attr,
+        }),
+        cx.tcx,
+        &cx.cache.hidden_cfg,
+    );
     let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
 
     let name = renamed.or(Some(name));
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
index 70be5742c08..edec6777c2e 100644
--- a/src/librustdoc/clean/types.rs
+++ b/src/librustdoc/clean/types.rs
@@ -1,4 +1,3 @@
-use std::borrow::Cow;
 use std::hash::Hash;
 use std::path::PathBuf;
 use std::sync::{Arc, OnceLock as OnceCell};
@@ -479,7 +478,7 @@ impl Item {
             name,
             kind,
             Attributes::from_hir(hir_attrs),
-            extract_cfg_from_attrs(hir_attrs, cx.tcx, &cx.cache.hidden_cfg),
+            extract_cfg_from_attrs(hir_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
         )
     }
 
@@ -979,27 +978,19 @@ pub(crate) struct Module {
     pub(crate) span: Span,
 }
 
-pub(crate) trait AttributesExt {
-    type Attributes<'a>: Iterator<Item = &'a hir::Attribute>
-    where
-        Self: 'a;
-
-    fn iter(&self) -> Self::Attributes<'_>;
-}
-
-pub fn hir_attr_lists<A: AttributesExt + ?Sized>(
-    attrs: &A,
+pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
+    attrs: I,
     name: Symbol,
-) -> impl Iterator<Item = ast::MetaItemInner> + use<'_, A> {
+) -> impl Iterator<Item = ast::MetaItemInner> + use<'a, I> {
     attrs
-        .iter()
+        .into_iter()
         .filter(move |attr| attr.has_name(name))
         .filter_map(ast::attr::AttributeExt::meta_item_list)
         .flatten()
 }
 
-pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
-    attrs: &A,
+pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> + Clone>(
+    attrs: I,
     tcx: TyCtxt<'_>,
     hidden_cfg: &FxHashSet<Cfg>,
 ) -> Option<Arc<Cfg>> {
@@ -1018,7 +1009,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
 
     let mut cfg = if doc_cfg_active || doc_auto_cfg_active {
         let mut doc_cfg = attrs
-            .iter()
+            .clone()
             .filter(|attr| attr.has_name(sym::doc))
             .flat_map(|attr| attr.meta_item_list().unwrap_or_default())
             .filter(|attr| attr.has_name(sym::cfg))
@@ -1031,7 +1022,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
             // If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
             // `doc(cfg())` overrides `cfg()`).
             attrs
-                .iter()
+                .clone()
                 .filter(|attr| attr.has_name(sym::cfg))
                 .filter_map(|attr| single(attr.meta_item_list()?))
                 .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>(
         Cfg::True
     };
 
-    for attr in attrs.iter() {
+    for attr in attrs.clone() {
         // #[doc]
         if attr.doc_str().is_none() && attr.has_name(sym::doc) {
             // #[doc(...)]
@@ -1090,28 +1081,6 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
     if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) }
 }
 
-impl AttributesExt for [hir::Attribute] {
-    type Attributes<'a> = impl Iterator<Item = &'a hir::Attribute> + 'a;
-
-    fn iter(&self) -> Self::Attributes<'_> {
-        self.iter()
-    }
-}
-
-impl AttributesExt for [(Cow<'_, hir::Attribute>, Option<DefId>)] {
-    type Attributes<'a>
-        = impl Iterator<Item = &'a hir::Attribute> + 'a
-    where
-        Self: 'a;
-
-    fn iter(&self) -> Self::Attributes<'_> {
-        self.iter().map(move |(attr, _)| match attr {
-            Cow::Borrowed(attr) => *attr,
-            Cow::Owned(attr) => attr,
-        })
-    }
-}
-
 pub(crate) trait NestedAttributesExt {
     /// Returns `true` if the attribute list contains a specific `word`
     fn has_word(self, word: Symbol) -> bool
diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs
index 5986694a257..bd292efeb7e 100644
--- a/src/librustdoc/doctest/rust.rs
+++ b/src/librustdoc/doctest/rust.rs
@@ -96,7 +96,9 @@ impl HirCollector<'_> {
         nested: F,
     ) {
         let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
-        if let Some(ref cfg) = extract_cfg_from_attrs(ast_attrs, self.tcx, &FxHashSet::default()) {
+        if let Some(ref cfg) =
+            extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
+        {
             if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
                 return;
             }