@@ -219,11 +219,13 @@ crate struct Item {
219
219
/// E.g., struct vs enum vs function.
220
220
crate kind : Box < ItemKind > ,
221
221
crate def_id : DefId ,
222
+
223
+ crate cfg : Option < Arc < Cfg > > ,
222
224
}
223
225
224
226
// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
225
227
#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
226
- rustc_data_structures:: static_assert_size!( Item , 40 ) ;
228
+ rustc_data_structures:: static_assert_size!( Item , 48 ) ;
227
229
228
230
impl fmt:: Debug for Item {
229
231
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -235,6 +237,7 @@ impl fmt::Debug for Item {
235
237
. field ( "kind" , & self . kind )
236
238
. field ( "visibility" , & self . visibility )
237
239
. field ( "def_id" , def_id)
240
+ . field ( "cfg" , & self . cfg )
238
241
. finish ( )
239
242
}
240
243
}
@@ -262,6 +265,10 @@ impl Item {
262
265
if self . is_fake ( ) { None } else { tcx. lookup_deprecation ( self . def_id ) }
263
266
}
264
267
268
+ crate fn inner_docs ( & self , tcx : TyCtxt < ' _ > ) -> bool {
269
+ if self . is_fake ( ) { false } else { tcx. get_attrs ( self . def_id ) . inner_docs ( ) }
270
+ }
271
+
265
272
crate fn span ( & self , tcx : TyCtxt < ' _ > ) -> Span {
266
273
let kind = match & * self . kind {
267
274
ItemKind :: StrippedItem ( k) => k,
@@ -305,12 +312,15 @@ impl Item {
305
312
kind : ItemKind ,
306
313
cx : & mut DocContext < ' _ > ,
307
314
) -> Item {
315
+ let ast_attrs = cx. tcx . get_attrs ( def_id) ;
316
+
308
317
Self :: from_def_id_and_attrs_and_parts (
309
318
def_id,
310
319
name,
311
320
kind,
312
- box cx . tcx . get_attrs ( def_id ) . clean ( cx) ,
321
+ box ast_attrs . clean ( cx) ,
313
322
cx,
323
+ ast_attrs. cfg ( cx. sess ( ) . diagnostic ( ) ) ,
314
324
)
315
325
}
316
326
@@ -320,6 +330,7 @@ impl Item {
320
330
kind : ItemKind ,
321
331
attrs : Box < Attributes > ,
322
332
cx : & mut DocContext < ' _ > ,
333
+ cfg : Option < Arc < Cfg > > ,
323
334
) -> Item {
324
335
debug ! ( "name={:?}, def_id={:?}" , name, def_id) ;
325
336
@@ -329,6 +340,7 @@ impl Item {
329
340
name,
330
341
attrs,
331
342
visibility : cx. tcx . visibility ( def_id) . clean ( cx) ,
343
+ cfg,
332
344
}
333
345
}
334
346
@@ -668,6 +680,8 @@ crate trait AttributesExt {
668
680
fn inner_docs ( & self ) -> bool ;
669
681
670
682
fn other_attrs ( & self ) -> Vec < ast:: Attribute > ;
683
+
684
+ fn cfg ( & self , diagnostic : & :: rustc_errors:: Handler ) -> Option < Arc < Cfg > > ;
671
685
}
672
686
673
687
impl AttributesExt for [ ast:: Attribute ] {
@@ -691,6 +705,41 @@ impl AttributesExt for [ast::Attribute] {
691
705
fn other_attrs ( & self ) -> Vec < ast:: Attribute > {
692
706
self . iter ( ) . filter ( |attr| attr. doc_str ( ) . is_none ( ) ) . cloned ( ) . collect ( )
693
707
}
708
+
709
+ fn cfg ( & self , diagnostic : & :: rustc_errors:: Handler ) -> Option < Arc < Cfg > > {
710
+ let mut cfg = Cfg :: True ;
711
+
712
+ for attr in self . iter ( ) {
713
+ if attr. doc_str ( ) . is_none ( ) && attr. has_name ( sym:: doc) {
714
+ if let Some ( mi) = attr. meta ( ) {
715
+ if let Some ( cfg_mi) = Attributes :: extract_cfg ( & mi) {
716
+ // Extracted #[doc(cfg(...))]
717
+ match Cfg :: parse ( cfg_mi) {
718
+ Ok ( new_cfg) => cfg &= new_cfg,
719
+ Err ( e) => diagnostic. span_err ( e. span , e. msg ) ,
720
+ }
721
+ }
722
+ }
723
+ }
724
+ }
725
+
726
+ for attr in self . lists ( sym:: target_feature) {
727
+ if attr. has_name ( sym:: enable) {
728
+ if let Some ( feat) = attr. value_str ( ) {
729
+ let meta = attr:: mk_name_value_item_str (
730
+ Ident :: with_dummy_span ( sym:: target_feature) ,
731
+ feat,
732
+ DUMMY_SP ,
733
+ ) ;
734
+ if let Ok ( feat_cfg) = Cfg :: parse ( & meta) {
735
+ cfg &= feat_cfg;
736
+ }
737
+ }
738
+ }
739
+ }
740
+
741
+ if cfg == Cfg :: True { None } else { Some ( Arc :: new ( cfg) ) }
742
+ }
694
743
}
695
744
696
745
crate trait NestedAttributesExt {
@@ -799,7 +848,6 @@ impl<'a> FromIterator<&'a DocFragment> for String {
799
848
crate struct Attributes {
800
849
crate doc_strings : Vec < DocFragment > ,
801
850
crate other_attrs : Vec < ast:: Attribute > ,
802
- crate cfg : Option < Arc < Cfg > > ,
803
851
}
804
852
805
853
#[ derive( Clone , Debug , Default , PartialEq , Eq , Hash ) ]
@@ -914,12 +962,10 @@ impl Attributes {
914
962
}
915
963
916
964
crate fn from_ast (
917
- diagnostic : & :: rustc_errors:: Handler ,
918
965
attrs : & [ ast:: Attribute ] ,
919
966
additional_attrs : Option < ( & [ ast:: Attribute ] , DefId ) > ,
920
967
) -> Attributes {
921
968
let mut doc_strings: Vec < DocFragment > = vec ! [ ] ;
922
- let mut cfg = Cfg :: True ;
923
969
let mut doc_line = 0 ;
924
970
925
971
fn update_need_backline ( doc_strings : & mut Vec < DocFragment > , frag : & DocFragment ) {
@@ -967,14 +1013,7 @@ impl Attributes {
967
1013
} else {
968
1014
if attr. has_name ( sym:: doc) {
969
1015
if let Some ( mi) = attr. meta ( ) {
970
- if let Some ( cfg_mi) = Attributes :: extract_cfg ( & mi) {
971
- // Extracted #[doc(cfg(...))]
972
- match Cfg :: parse ( cfg_mi) {
973
- Ok ( new_cfg) => cfg &= new_cfg,
974
- Err ( e) => diagnostic. span_err ( e. span , e. msg ) ,
975
- }
976
- } else if let Some ( ( filename, contents) ) = Attributes :: extract_include ( & mi)
977
- {
1016
+ if let Some ( ( filename, contents) ) = Attributes :: extract_include ( & mi) {
978
1017
let line = doc_line;
979
1018
doc_line += contents. as_str ( ) . lines ( ) . count ( ) ;
980
1019
let frag = DocFragment {
@@ -1004,28 +1043,7 @@ impl Attributes {
1004
1043
. filter_map ( clean_attr)
1005
1044
. collect ( ) ;
1006
1045
1007
- // treat #[target_feature(enable = "feat")] attributes as if they were
1008
- // #[doc(cfg(target_feature = "feat"))] attributes as well
1009
- for attr in attrs. lists ( sym:: target_feature) {
1010
- if attr. has_name ( sym:: enable) {
1011
- if let Some ( feat) = attr. value_str ( ) {
1012
- let meta = attr:: mk_name_value_item_str (
1013
- Ident :: with_dummy_span ( sym:: target_feature) ,
1014
- feat,
1015
- DUMMY_SP ,
1016
- ) ;
1017
- if let Ok ( feat_cfg) = Cfg :: parse ( & meta) {
1018
- cfg &= feat_cfg;
1019
- }
1020
- }
1021
- }
1022
- }
1023
-
1024
- Attributes {
1025
- doc_strings,
1026
- other_attrs,
1027
- cfg : if cfg == Cfg :: True { None } else { Some ( Arc :: new ( cfg) ) } ,
1028
- }
1046
+ Attributes { doc_strings, other_attrs }
1029
1047
}
1030
1048
1031
1049
/// Finds the `doc` attribute as a NameValue and returns the corresponding
@@ -1091,7 +1109,6 @@ impl Attributes {
1091
1109
impl PartialEq for Attributes {
1092
1110
fn eq ( & self , rhs : & Self ) -> bool {
1093
1111
self . doc_strings == rhs. doc_strings
1094
- && self . cfg == rhs. cfg
1095
1112
&& self
1096
1113
. other_attrs
1097
1114
. iter ( )
@@ -1105,7 +1122,6 @@ impl Eq for Attributes {}
1105
1122
impl Hash for Attributes {
1106
1123
fn hash < H : Hasher > ( & self , hasher : & mut H ) {
1107
1124
self . doc_strings . hash ( hasher) ;
1108
- self . cfg . hash ( hasher) ;
1109
1125
for attr in & self . other_attrs {
1110
1126
attr. id . hash ( hasher) ;
1111
1127
}
0 commit comments