rustdoc: Eliminate AttributesExt
The new code is more explicit and avoids trait magic that added needless complexity to this part of rustdoc.
This commit is contained in:
parent
5ccd6c04e5
commit
f92b32c5f6
4 changed files with 23 additions and 46 deletions
|
@ -408,12 +408,12 @@ pub(crate) fn merge_attrs(
|
||||||
} else {
|
} else {
|
||||||
Attributes::from_hir(&both)
|
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 {
|
} else {
|
||||||
(
|
(
|
||||||
Attributes::from_hir(old_attrs),
|
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),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,8 +198,14 @@ fn generate_item_with_correct_attrs(
|
||||||
// We only keep the item's attributes.
|
// We only keep the item's attributes.
|
||||||
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
|
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
|
||||||
};
|
};
|
||||||
|
let cfg = extract_cfg_from_attrs(
|
||||||
let cfg = extract_cfg_from_attrs(&attrs[..], cx.tcx, &cx.cache.hidden_cfg);
|
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 attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
|
||||||
|
|
||||||
let name = renamed.or(Some(name));
|
let name = renamed.or(Some(name));
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::borrow::Cow;
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, OnceLock as OnceCell};
|
use std::sync::{Arc, OnceLock as OnceCell};
|
||||||
|
@ -479,7 +478,7 @@ impl Item {
|
||||||
name,
|
name,
|
||||||
kind,
|
kind,
|
||||||
Attributes::from_hir(hir_attrs),
|
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) span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait AttributesExt {
|
pub(crate) fn hir_attr_lists<'a, I: IntoIterator<Item = &'a hir::Attribute>>(
|
||||||
type Attributes<'a>: Iterator<Item = &'a hir::Attribute>
|
attrs: I,
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
|
|
||||||
fn iter(&self) -> Self::Attributes<'_>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hir_attr_lists<A: AttributesExt + ?Sized>(
|
|
||||||
attrs: &A,
|
|
||||||
name: Symbol,
|
name: Symbol,
|
||||||
) -> impl Iterator<Item = ast::MetaItemInner> + use<'_, A> {
|
) -> impl Iterator<Item = ast::MetaItemInner> + use<'a, I> {
|
||||||
attrs
|
attrs
|
||||||
.iter()
|
.into_iter()
|
||||||
.filter(move |attr| attr.has_name(name))
|
.filter(move |attr| attr.has_name(name))
|
||||||
.filter_map(ast::attr::AttributeExt::meta_item_list)
|
.filter_map(ast::attr::AttributeExt::meta_item_list)
|
||||||
.flatten()
|
.flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
|
pub(crate) fn extract_cfg_from_attrs<'a, I: Iterator<Item = &'a hir::Attribute> + Clone>(
|
||||||
attrs: &A,
|
attrs: I,
|
||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
hidden_cfg: &FxHashSet<Cfg>,
|
hidden_cfg: &FxHashSet<Cfg>,
|
||||||
) -> Option<Arc<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 cfg = if doc_cfg_active || doc_auto_cfg_active {
|
||||||
let mut doc_cfg = attrs
|
let mut doc_cfg = attrs
|
||||||
.iter()
|
.clone()
|
||||||
.filter(|attr| attr.has_name(sym::doc))
|
.filter(|attr| attr.has_name(sym::doc))
|
||||||
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
|
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
|
||||||
.filter(|attr| attr.has_name(sym::cfg))
|
.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
|
// If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
|
||||||
// `doc(cfg())` overrides `cfg()`).
|
// `doc(cfg())` overrides `cfg()`).
|
||||||
attrs
|
attrs
|
||||||
.iter()
|
.clone()
|
||||||
.filter(|attr| attr.has_name(sym::cfg))
|
.filter(|attr| attr.has_name(sym::cfg))
|
||||||
.filter_map(|attr| single(attr.meta_item_list()?))
|
.filter_map(|attr| single(attr.meta_item_list()?))
|
||||||
.filter_map(|attr| Cfg::parse_without(attr.meta_item()?, hidden_cfg).ok().flatten())
|
.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
|
Cfg::True
|
||||||
};
|
};
|
||||||
|
|
||||||
for attr in attrs.iter() {
|
for attr in attrs.clone() {
|
||||||
// #[doc]
|
// #[doc]
|
||||||
if attr.doc_str().is_none() && attr.has_name(sym::doc) {
|
if attr.doc_str().is_none() && attr.has_name(sym::doc) {
|
||||||
// #[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)) }
|
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 {
|
pub(crate) trait NestedAttributesExt {
|
||||||
/// Returns `true` if the attribute list contains a specific `word`
|
/// Returns `true` if the attribute list contains a specific `word`
|
||||||
fn has_word(self, word: Symbol) -> bool
|
fn has_word(self, word: Symbol) -> bool
|
||||||
|
|
|
@ -96,7 +96,9 @@ impl HirCollector<'_> {
|
||||||
nested: F,
|
nested: F,
|
||||||
) {
|
) {
|
||||||
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
|
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())) {
|
if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue