Skip to content

Commit e2fcdb8

Browse files
committed
rustdoc: Extract AttributesExt::cfg trait method as function
It's never overridden, so it shouldn't be on the trait.
1 parent e7ad3ae commit e2fcdb8

4 files changed

Lines changed: 88 additions & 83 deletions

File tree

‎src/librustdoc/clean/inline.rs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use rustc_span::symbol::{Symbol, sym};
1717
use thin_vec::{ThinVec, thin_vec};
1818
use tracing::{debug, trace};
1919

20-
usesuper::Item;
20+
usesuper::{Item, extract_cfg_from_attrs};
2121
usecrate::clean::{
22-
self,Attributes,AttributesExt,ImplKind,ItemId,Type, clean_bound_vars, clean_generics,
23-
clean_impl_item,clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
24-
clean_poly_fn_sig,clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type,
25-
clean_ty_generics,clean_variant_def, utils,
22+
self,Attributes,ImplKind,ItemId,Type, clean_bound_vars, clean_generics, clean_impl_item,
23+
clean_middle_assoc_item, clean_middle_field, clean_middle_ty, clean_poly_fn_sig,
24+
clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type, clean_ty_generics,
25+
clean_variant_def, utils,
2626
};
2727
usecrate::core::DocContext;
2828
usecrate::formats::item_type::ItemType;
@@ -408,10 +408,13 @@ pub(crate) fn merge_attrs(
408408
}else{
409409
Attributes::from_hir(&both)
410410
},
411-
both.cfg(cx.tcx,&cx.cache.hidden_cfg),
411+
extract_cfg_from_attrs(&both[..],cx.tcx,&cx.cache.hidden_cfg),
412412
)
413413
}else{
414-
(Attributes::from_hir(old_attrs), old_attrs.cfg(cx.tcx,&cx.cache.hidden_cfg))
414+
(
415+
Attributes::from_hir(old_attrs),
416+
extract_cfg_from_attrs(&old_attrs[..], cx.tcx,&cx.cache.hidden_cfg),
417+
)
415418
}
416419
}
417420

‎src/librustdoc/clean/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn generate_item_with_correct_attrs(
200200
target_attrs.iter().map(|attr| (Cow::Borrowed(attr),None)).collect()
201201
};
202202

203-
let cfg = attrs.cfg(cx.tcx,&cx.cache.hidden_cfg);
203+
let cfg = extract_cfg_from_attrs(&attrs[..],cx.tcx,&cx.cache.hidden_cfg);
204204
let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr,*did)),false);
205205

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

‎src/librustdoc/clean/types.rs‎

Lines changed: 75 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ impl Item {
479479
name,
480480
kind,
481481
Attributes::from_hir(hir_attrs),
482-
hir_attrs.cfg(cx.tcx,&cx.cache.hidden_cfg),
482+
extract_cfg_from_attrs(hir_attrs,cx.tcx,&cx.cache.hidden_cfg),
483483
)
484484
}
485485

@@ -990,95 +990,98 @@ pub(crate) trait AttributesExt {
990990
fnlists(&self,name:Symbol) -> Self::AttributeIterator<'_>;
991991

992992
fniter(&self) -> Self::Attributes<'_>;
993+
}
993994

994-
fncfg(&self,tcx:TyCtxt<'_>,hidden_cfg:&FxHashSet<Cfg>) -> Option<Arc<Cfg>>{
995-
let sess = tcx.sess;
996-
let doc_cfg_active = tcx.features().doc_cfg();
997-
let doc_auto_cfg_active = tcx.features().doc_auto_cfg();
998-
999-
fnsingle<T:IntoIterator>(it:T) -> Option<T::Item>{
1000-
letmut iter = it.into_iter();
1001-
let item = iter.next()?;
1002-
if iter.next().is_some(){
1003-
returnNone;
1004-
}
1005-
Some(item)
995+
pubfnextract_cfg_from_attrs<A:AttributesExt + ?Sized>(
996+
attrs:&A,
997+
tcx:TyCtxt<'_>,
998+
hidden_cfg:&FxHashSet<Cfg>,
999+
) -> Option<Arc<Cfg>>{
1000+
let sess = tcx.sess;
1001+
let doc_cfg_active = tcx.features().doc_cfg();
1002+
let doc_auto_cfg_active = tcx.features().doc_auto_cfg();
1003+
1004+
fnsingle<T:IntoIterator>(it:T) -> Option<T::Item>{
1005+
letmut iter = it.into_iter();
1006+
let item = iter.next()?;
1007+
if iter.next().is_some(){
1008+
returnNone;
10061009
}
1010+
Some(item)
1011+
}
10071012

1008-
letmut cfg = if doc_cfg_active || doc_auto_cfg_active {
1009-
letmut doc_cfg = self
1013+
letmut cfg = if doc_cfg_active || doc_auto_cfg_active {
1014+
letmut doc_cfg = attrs
1015+
.iter()
1016+
.filter(|attr| attr.has_name(sym::doc))
1017+
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
1018+
.filter(|attr| attr.has_name(sym::cfg))
1019+
.peekable();
1020+
if doc_cfg.peek().is_some() && doc_cfg_active {
1021+
doc_cfg
1022+
.filter_map(|attr| Cfg::parse(&attr).ok())
1023+
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
1024+
}elseif doc_auto_cfg_active {
1025+
// If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
1026+
// `doc(cfg())` overrides `cfg()`).
1027+
attrs
10101028
.iter()
1011-
.filter(|attr| attr.has_name(sym::doc))
1012-
.flat_map(|attr| attr.meta_item_list().unwrap_or_default())
10131029
.filter(|attr| attr.has_name(sym::cfg))
1014-
.peekable();
1015-
if doc_cfg.peek().is_some() && doc_cfg_active {
1016-
doc_cfg
1017-
.filter_map(|attr| Cfg::parse(&attr).ok())
1018-
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
1019-
}elseif doc_auto_cfg_active {
1020-
// If there is no `doc(cfg())`, then we retrieve the `cfg()` attributes (because
1021-
// `doc(cfg())` overrides `cfg()`).
1022-
self.iter()
1023-
.filter(|attr| attr.has_name(sym::cfg))
1024-
.filter_map(|attr| single(attr.meta_item_list()?))
1025-
.filter_map(|attr| {
1026-
Cfg::parse_without(attr.meta_item()?, hidden_cfg).ok().flatten()
1027-
})
1028-
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
1029-
}else{
1030-
Cfg::True
1031-
}
1030+
.filter_map(|attr| single(attr.meta_item_list()?))
1031+
.filter_map(|attr| Cfg::parse_without(attr.meta_item()?, hidden_cfg).ok().flatten())
1032+
.fold(Cfg::True, |cfg, new_cfg| cfg & new_cfg)
10321033
}else{
10331034
Cfg::True
1034-
};
1035-
1036-
for attr inself.iter(){
1037-
// #[doc]
1038-
if attr.doc_str().is_none() && attr.has_name(sym::doc){
1039-
// #[doc(...)]
1040-
ifletSome(list) = attr.meta_item_list(){
1041-
for item in list {
1042-
// #[doc(hidden)]
1043-
if !item.has_name(sym::cfg){
1044-
continue;
1045-
}
1046-
// #[doc(cfg(...))]
1047-
ifletSome(cfg_mi) = item
1048-
.meta_item()
1049-
.and_then(|item| rustc_expand::config::parse_cfg(item, sess))
1050-
{
1051-
matchCfg::parse(cfg_mi){
1052-
Ok(new_cfg) => cfg &= new_cfg,
1053-
Err(e) => {
1054-
sess.dcx().span_err(e.span, e.msg);
1055-
}
1035+
}
1036+
}else{
1037+
Cfg::True
1038+
};
1039+
1040+
for attr in attrs.iter(){
1041+
// #[doc]
1042+
if attr.doc_str().is_none() && attr.has_name(sym::doc){
1043+
// #[doc(...)]
1044+
ifletSome(list) = attr.meta_item_list(){
1045+
for item in list {
1046+
// #[doc(hidden)]
1047+
if !item.has_name(sym::cfg){
1048+
continue;
1049+
}
1050+
// #[doc(cfg(...))]
1051+
ifletSome(cfg_mi) = item
1052+
.meta_item()
1053+
.and_then(|item| rustc_expand::config::parse_cfg(item, sess))
1054+
{
1055+
matchCfg::parse(cfg_mi){
1056+
Ok(new_cfg) => cfg &= new_cfg,
1057+
Err(e) => {
1058+
sess.dcx().span_err(e.span, e.msg);
10561059
}
10571060
}
10581061
}
10591062
}
10601063
}
10611064
}
1065+
}
10621066

1063-
// treat #[target_feature(enable = "feat")] attributes as if they were
1064-
// #[doc(cfg(target_feature = "feat"))] attributes as well
1065-
for attr inself.lists(sym::target_feature){
1066-
if attr.has_name(sym::enable){
1067-
if attr.value_str().is_some(){
1068-
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
1069-
// Unwrap is safe because `value_str` succeeded above.
1070-
letmut meta = attr.meta_item().unwrap().clone();
1071-
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
1072-
1073-
ifletOk(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)){
1074-
cfg &= feat_cfg;
1075-
}
1067+
// treat #[target_feature(enable = "feat")] attributes as if they were
1068+
// #[doc(cfg(target_feature = "feat"))] attributes as well
1069+
for attr in attrs.lists(sym::target_feature){
1070+
if attr.has_name(sym::enable){
1071+
if attr.value_str().is_some(){
1072+
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
1073+
// Unwrap is safe because `value_str` succeeded above.
1074+
letmut meta = attr.meta_item().unwrap().clone();
1075+
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
1076+
1077+
ifletOk(feat_cfg) = Cfg::parse(&ast::MetaItemInner::MetaItem(meta)){
1078+
cfg &= feat_cfg;
10761079
}
10771080
}
10781081
}
1079-
1080-
if cfg == Cfg::True{None}else{Some(Arc::new(cfg))}
10811082
}
1083+
1084+
if cfg == Cfg::True{None}else{Some(Arc::new(cfg))}
10821085
}
10831086

10841087
implAttributesExtfor[hir::Attribute]{

‎src/librustdoc/doctest/rust.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_span::source_map::SourceMap;
1313
use rustc_span::{BytePos,DUMMY_SP,FileName,Pos,Span};
1414

1515
usesuper::{DocTestVisitor,ScrapedDocTest};
16-
usecrate::clean::Attributes;
17-
usecrate::clean::types::AttributesExt;
16+
usecrate::clean::{Attributes, extract_cfg_from_attrs};
1817
usecrate::html::markdown::{self,ErrorCodes,LangString,MdRelLine};
1918

2019
structRustCollector{
@@ -97,7 +96,7 @@ impl HirCollector<'_> {
9796
nested:F,
9897
){
9998
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
100-
ifletSome(ref cfg) = ast_attrs.cfg(self.tcx,&FxHashSet::default()){
99+
ifletSome(ref cfg) = extract_cfg_from_attrs(ast_attrs,self.tcx,&FxHashSet::default()){
101100
if !cfg.matches(&self.tcx.sess.psess,Some(self.tcx.features())){
102101
return;
103102
}

0 commit comments

Comments
 (0)