@@ -6,7 +6,7 @@ use std::lazy::SyncOnceCell as OnceCell;
6
6
use std:: path:: PathBuf ;
7
7
use std:: rc:: Rc ;
8
8
use std:: sync:: Arc ;
9
- use std:: { slice , vec} ;
9
+ use std:: vec;
10
10
11
11
use arrayvec:: ArrayVec ;
12
12
@@ -215,8 +215,7 @@ impl ExternalCrate {
215
215
// Failing that, see if there's an attribute specifying where to find this
216
216
// external crate
217
217
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)
220
219
. filter ( |a| a. has_name ( sym:: html_root_url) )
221
220
. filter_map ( |a| a. value_str ( ) )
222
221
. map ( to_remote)
@@ -232,7 +231,7 @@ impl ExternalCrate {
232
231
if let Res :: Def ( DefKind :: Mod , def_id) = res {
233
232
let attrs = tcx. get_attrs ( def_id) ;
234
233
let mut keyword = None ;
235
- for attr in attrs . lists ( sym:: doc) {
234
+ for attr in attr_items ( attrs , sym:: doc) {
236
235
if attr. has_name ( sym:: keyword) {
237
236
if let Some ( v) = attr. value_str ( ) {
238
237
keyword = Some ( v) ;
@@ -294,7 +293,7 @@ impl ExternalCrate {
294
293
if let Res :: Def ( DefKind :: Mod , def_id) = res {
295
294
let attrs = tcx. get_attrs ( def_id) ;
296
295
let mut prim = None ;
297
- for attr in attrs . lists ( sym:: doc) {
296
+ for attr in attr_items ( attrs , sym:: doc) {
298
297
if let Some ( v) = attr. value_str ( ) {
299
298
if attr. has_name ( sym:: primitive) {
300
299
prim = PrimitiveType :: from_symbol ( v) ;
@@ -725,44 +724,19 @@ crate struct Module {
725
724
crate span : Span ,
726
725
}
727
726
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 ] ,
731
730
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 ( )
760
737
}
761
738
762
739
crate trait AttributesExt {
763
- /// Finds an attribute as List and returns the list of attributes nested inside.
764
- fn lists ( & self , name : Symbol ) -> ListAttributesIter < ' _ > ;
765
-
766
740
fn span ( & self ) -> Option < rustc_span:: Span > ;
767
741
768
742
fn inner_docs ( & self ) -> bool ;
@@ -773,10 +747,6 @@ crate trait AttributesExt {
773
747
}
774
748
775
749
impl 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
-
780
750
/// Return the span of the first doc-comment, if it exists.
781
751
fn span ( & self ) -> Option < rustc_span:: Span > {
782
752
self . iter ( ) . find ( |attr| attr. doc_str ( ) . is_some ( ) ) . map ( |attr| attr. span )
@@ -860,7 +830,7 @@ impl AttributesExt for [ast::Attribute] {
860
830
861
831
// treat #[target_feature(enable = "feat")] attributes as if they were
862
832
// #[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) {
864
834
if attr. has_name ( sym:: enable) {
865
835
if let Some ( feat) = attr. value_str ( ) {
866
836
let meta = attr:: mk_name_value_item_str (
@@ -882,18 +852,11 @@ impl AttributesExt for [ast::Attribute] {
882
852
crate trait NestedAttributesExt {
883
853
/// Returns `true` if the attribute list contains a specific `Word`
884
854
fn has_word ( self , word : Symbol ) -> bool ;
885
- fn get_word_attr ( self , word : Symbol ) -> Option < ast:: NestedMetaItem > ;
886
855
}
887
856
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) )
897
860
}
898
861
}
899
862
@@ -1001,10 +964,6 @@ crate struct Attributes {
1001
964
}
1002
965
1003
966
impl Attributes {
1004
- crate fn lists ( & self , name : Symbol ) -> ListAttributesIter < ' _ > {
1005
- self . other_attrs . lists ( name)
1006
- }
1007
-
1008
967
crate fn has_doc_flag ( & self , flag : Symbol ) -> bool {
1009
968
for attr in & self . other_attrs {
1010
969
if !attr. has_name ( sym:: doc) {
@@ -1110,7 +1069,7 @@ impl Attributes {
1110
1069
crate fn get_doc_aliases ( & self ) -> Box < [ Symbol ] > {
1111
1070
let mut aliases = FxHashSet :: default ( ) ;
1112
1071
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) ) {
1114
1073
if let Some ( values) = attr. meta_item_list ( ) {
1115
1074
for l in values {
1116
1075
match l. literal ( ) . unwrap ( ) . kind {
0 commit comments