@@ -6,7 +6,7 @@ use std::lazy::SyncOnceCell as OnceCell;
66use std:: path:: PathBuf ;
77use std:: rc:: Rc ;
88use std:: sync:: Arc ;
9- use std:: { slice , vec} ;
9+ use std:: vec;
1010
1111use arrayvec:: ArrayVec ;
1212
@@ -215,8 +215,7 @@ impl ExternalCrate {
215215 // Failing that, see if there's an attribute specifying where to find this
216216 // external crate
217217 let did = DefId { krate : self . crate_num , index : CRATE_DEF_INDEX } ;
218- tcx. get_attrs ( did)
219- . lists ( sym:: doc)
218+ attr_items ( tcx. get_attrs ( did) , sym:: doc)
220219 . filter ( |a| a. has_name ( sym:: html_root_url) )
221220 . filter_map ( |a| a. value_str ( ) )
222221 . map ( to_remote)
@@ -232,7 +231,7 @@ impl ExternalCrate {
232231 if let Res :: Def ( DefKind :: Mod , def_id) = res {
233232 let attrs = tcx. get_attrs ( def_id) ;
234233 let mut keyword = None ;
235- for attr in attrs . lists ( sym:: doc) {
234+ for attr in attr_items ( attrs , sym:: doc) {
236235 if attr. has_name ( sym:: keyword) {
237236 if let Some ( v) = attr. value_str ( ) {
238237 keyword = Some ( v) ;
@@ -294,7 +293,7 @@ impl ExternalCrate {
294293 if let Res :: Def ( DefKind :: Mod , def_id) = res {
295294 let attrs = tcx. get_attrs ( def_id) ;
296295 let mut prim = None ;
297- for attr in attrs . lists ( sym:: doc) {
296+ for attr in attr_items ( attrs , sym:: doc) {
298297 if let Some ( v) = attr. value_str ( ) {
299298 if attr. has_name ( sym:: primitive) {
300299 prim = PrimitiveType :: from_symbol ( v) ;
@@ -725,44 +724,19 @@ crate struct Module {
725724 crate span : Span ,
726725}
727726
728- crate struct ListAttributesIter < ' a > {
729- attrs : slice :: Iter < ' a , ast :: Attribute > ,
730- current_list : vec :: IntoIter < ast:: NestedMetaItem > ,
727+ /// Finds attributes with the given name and returns their nested items.
728+ crate fn attr_items (
729+ attrs : & [ ast:: Attribute ] ,
731730 name : Symbol ,
732- }
733-
734- impl < ' a > Iterator for ListAttributesIter < ' a > {
735- type Item = ast:: NestedMetaItem ;
736-
737- fn next ( & mut self ) -> Option < Self :: Item > {
738- if let Some ( nested) = self . current_list . next ( ) {
739- return Some ( nested) ;
740- }
741-
742- for attr in & mut self . attrs {
743- if let Some ( list) = attr. meta_item_list ( ) {
744- if attr. has_name ( self . name ) {
745- self . current_list = list. into_iter ( ) ;
746- if let Some ( nested) = self . current_list . next ( ) {
747- return Some ( nested) ;
748- }
749- }
750- }
751- }
752-
753- None
754- }
755-
756- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
757- let lower = self . current_list . len ( ) ;
758- ( lower, None )
759- }
731+ ) -> impl Iterator < Item = ast:: NestedMetaItem > + ' _ {
732+ attrs
733+ . iter ( )
734+ . filter ( move |attr| attr. has_name ( name) )
735+ . filter_map ( ast:: Attribute :: meta_item_list)
736+ . flatten ( )
760737}
761738
762739crate trait AttributesExt {
763- /// Finds an attribute as List and returns the list of attributes nested inside.
764- fn lists ( & self , name : Symbol ) -> ListAttributesIter < ' _ > ;
765-
766740 fn span ( & self ) -> Option < rustc_span:: Span > ;
767741
768742 fn inner_docs ( & self ) -> bool ;
@@ -773,10 +747,6 @@ crate trait AttributesExt {
773747}
774748
775749impl AttributesExt for [ ast:: Attribute ] {
776- fn lists ( & self , name : Symbol ) -> ListAttributesIter < ' _ > {
777- ListAttributesIter { attrs : self . iter ( ) , current_list : Vec :: new ( ) . into_iter ( ) , name }
778- }
779-
780750 /// Return the span of the first doc-comment, if it exists.
781751 fn span ( & self ) -> Option < rustc_span:: Span > {
782752 self . iter ( ) . find ( |attr| attr. doc_str ( ) . is_some ( ) ) . map ( |attr| attr. span )
@@ -860,7 +830,7 @@ impl AttributesExt for [ast::Attribute] {
860830
861831 // treat #[target_feature(enable = "feat")] attributes as if they were
862832 // #[doc(cfg(target_feature = "feat"))] attributes as well
863- for attr in self . lists ( sym:: target_feature) {
833+ for attr in attr_items ( self , sym:: target_feature) {
864834 if attr. has_name ( sym:: enable) {
865835 if let Some ( feat) = attr. value_str ( ) {
866836 let meta = attr:: mk_name_value_item_str (
@@ -882,18 +852,11 @@ impl AttributesExt for [ast::Attribute] {
882852crate trait NestedAttributesExt {
883853 /// Returns `true` if the attribute list contains a specific `Word`
884854 fn has_word ( self , word : Symbol ) -> bool ;
885- fn get_word_attr ( self , word : Symbol ) -> Option < ast:: NestedMetaItem > ;
886855}
887856
888- impl < I : Iterator < Item = ast:: NestedMetaItem > + IntoIterator < Item = ast:: NestedMetaItem > >
889- NestedAttributesExt for I
890- {
891- fn has_word ( self , word : Symbol ) -> bool {
892- self . into_iter ( ) . any ( |attr| attr. is_word ( ) && attr. has_name ( word) )
893- }
894-
895- fn get_word_attr ( mut self , word : Symbol ) -> Option < ast:: NestedMetaItem > {
896- self . find ( |attr| attr. is_word ( ) && attr. has_name ( word) )
857+ impl < I : Iterator < Item = ast:: NestedMetaItem > > NestedAttributesExt for I {
858+ fn has_word ( mut self , word : Symbol ) -> bool {
859+ self . any ( |attr| attr. is_word ( ) && attr. has_name ( word) )
897860 }
898861}
899862
@@ -1001,10 +964,6 @@ crate struct Attributes {
1001964}
1002965
1003966impl Attributes {
1004- crate fn lists ( & self , name : Symbol ) -> ListAttributesIter < ' _ > {
1005- self . other_attrs . lists ( name)
1006- }
1007-
1008967 crate fn has_doc_flag ( & self , flag : Symbol ) -> bool {
1009968 for attr in & self . other_attrs {
1010969 if !attr. has_name ( sym:: doc) {
@@ -1110,7 +1069,7 @@ impl Attributes {
11101069 crate fn get_doc_aliases ( & self ) -> Box < [ Symbol ] > {
11111070 let mut aliases = FxHashSet :: default ( ) ;
11121071
1113- for attr in self . other_attrs . lists ( sym:: doc) . filter ( |a| a. has_name ( sym:: alias) ) {
1072+ for attr in attr_items ( & self . other_attrs , sym:: doc) . filter ( |a| a. has_name ( sym:: alias) ) {
11141073 if let Some ( values) = attr. meta_item_list ( ) {
11151074 for l in values {
11161075 match l. literal ( ) . unwrap ( ) . kind {
0 commit comments