From f92b32c5f64ea900f2cd6936f648f407daee0d25 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sun, 12 Jan 2025 20:31:53 -0800 Subject: [PATCH] rustdoc: Eliminate `AttributesExt` The new code is more explicit and avoids trait magic that added needless complexity to this part of rustdoc. --- src/librustdoc/clean/inline.rs | 4 +-- src/librustdoc/clean/mod.rs | 10 +++++-- src/librustdoc/clean/types.rs | 51 +++++++--------------------------- src/librustdoc/doctest/rust.rs | 4 ++- 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 - where - Self: 'a; - - fn iter(&self) -> Self::Attributes<'_>; -} - -pub fn hir_attr_lists( - attrs: &A, +pub(crate) fn hir_attr_lists<'a, I: IntoIterator>( + attrs: I, name: Symbol, -) -> impl Iterator + use<'_, A> { +) -> impl Iterator + 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( - attrs: &A, +pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator + Clone>( + attrs: I, tcx: TyCtxt<'_>, hidden_cfg: &FxHashSet, ) -> Option> { @@ -1018,7 +1009,7 @@ pub fn extract_cfg_from_attrs( 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( // 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( 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( if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) } } -impl AttributesExt for [hir::Attribute] { - type Attributes<'a> = impl Iterator + 'a; - - fn iter(&self) -> Self::Attributes<'_> { - self.iter() - } -} - -impl AttributesExt for [(Cow<'_, hir::Attribute>, Option)] { - type Attributes<'a> - = impl Iterator + '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; }