@@ -219,11 +219,13 @@ crate struct Item {
219219 /// E.g., struct vs enum vs function.
220220 crate kind : Box < ItemKind > ,
221221 crate def_id : DefId ,
222+
223+ crate cfg : Option < Arc < Cfg > > ,
222224}
223225
224226// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
225227#[ 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 ) ;
227229
228230impl fmt:: Debug for Item {
229231 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -235,6 +237,7 @@ impl fmt::Debug for Item {
235237 . field ( "kind" , & self . kind )
236238 . field ( "visibility" , & self . visibility )
237239 . field ( "def_id" , def_id)
240+ . field ( "cfg" , & self . cfg )
238241 . finish ( )
239242 }
240243}
@@ -262,6 +265,10 @@ impl Item {
262265 if self . is_fake ( ) { None } else { tcx. lookup_deprecation ( self . def_id ) }
263266 }
264267
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+
265272 crate fn span ( & self , tcx : TyCtxt < ' _ > ) -> Span {
266273 let kind = match & * self . kind {
267274 ItemKind :: StrippedItem ( k) => k,
@@ -305,12 +312,15 @@ impl Item {
305312 kind : ItemKind ,
306313 cx : & mut DocContext < ' _ > ,
307314 ) -> Item {
315+ let ast_attrs = cx. tcx . get_attrs ( def_id) ;
316+
308317 Self :: from_def_id_and_attrs_and_parts (
309318 def_id,
310319 name,
311320 kind,
312- box cx . tcx . get_attrs ( def_id ) . clean ( cx) ,
321+ box ast_attrs . clean ( cx) ,
313322 cx,
323+ ast_attrs. cfg ( cx. sess ( ) . diagnostic ( ) ) ,
314324 )
315325 }
316326
@@ -320,6 +330,7 @@ impl Item {
320330 kind : ItemKind ,
321331 attrs : Box < Attributes > ,
322332 cx : & mut DocContext < ' _ > ,
333+ cfg : Option < Arc < Cfg > > ,
323334 ) -> Item {
324335 debug ! ( "name={:?}, def_id={:?}" , name, def_id) ;
325336
@@ -329,6 +340,7 @@ impl Item {
329340 name,
330341 attrs,
331342 visibility : cx. tcx . visibility ( def_id) . clean ( cx) ,
343+ cfg,
332344 }
333345 }
334346
@@ -668,6 +680,8 @@ crate trait AttributesExt {
668680 fn inner_docs ( & self ) -> bool ;
669681
670682 fn other_attrs ( & self ) -> Vec < ast:: Attribute > ;
683+
684+ fn cfg ( & self , diagnostic : & :: rustc_errors:: Handler ) -> Option < Arc < Cfg > > ;
671685}
672686
673687impl AttributesExt for [ ast:: Attribute ] {
@@ -691,6 +705,41 @@ impl AttributesExt for [ast::Attribute] {
691705 fn other_attrs ( & self ) -> Vec < ast:: Attribute > {
692706 self . iter ( ) . filter ( |attr| attr. doc_str ( ) . is_none ( ) ) . cloned ( ) . collect ( )
693707 }
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+ }
694743}
695744
696745crate trait NestedAttributesExt {
@@ -799,7 +848,6 @@ impl<'a> FromIterator<&'a DocFragment> for String {
799848crate struct Attributes {
800849 crate doc_strings : Vec < DocFragment > ,
801850 crate other_attrs : Vec < ast:: Attribute > ,
802- crate cfg : Option < Arc < Cfg > > ,
803851}
804852
805853#[ derive( Clone , Debug , Default , PartialEq , Eq , Hash ) ]
@@ -914,12 +962,10 @@ impl Attributes {
914962 }
915963
916964 crate fn from_ast (
917- diagnostic : & :: rustc_errors:: Handler ,
918965 attrs : & [ ast:: Attribute ] ,
919966 additional_attrs : Option < ( & [ ast:: Attribute ] , DefId ) > ,
920967 ) -> Attributes {
921968 let mut doc_strings: Vec < DocFragment > = vec ! [ ] ;
922- let mut cfg = Cfg :: True ;
923969 let mut doc_line = 0 ;
924970
925971 fn update_need_backline ( doc_strings : & mut Vec < DocFragment > , frag : & DocFragment ) {
@@ -967,14 +1013,7 @@ impl Attributes {
9671013 } else {
9681014 if attr. has_name ( sym:: doc) {
9691015 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) {
9781017 let line = doc_line;
9791018 doc_line += contents. as_str ( ) . lines ( ) . count ( ) ;
9801019 let frag = DocFragment {
@@ -1004,28 +1043,7 @@ impl Attributes {
10041043 . filter_map ( clean_attr)
10051044 . collect ( ) ;
10061045
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 }
10291047 }
10301048
10311049 /// Finds the `doc` attribute as a NameValue and returns the corresponding
@@ -1091,7 +1109,6 @@ impl Attributes {
10911109impl PartialEq for Attributes {
10921110 fn eq ( & self , rhs : & Self ) -> bool {
10931111 self . doc_strings == rhs. doc_strings
1094- && self . cfg == rhs. cfg
10951112 && self
10961113 . other_attrs
10971114 . iter ( )
@@ -1105,7 +1122,6 @@ impl Eq for Attributes {}
11051122impl Hash for Attributes {
11061123 fn hash < H : Hasher > ( & self , hasher : & mut H ) {
11071124 self . doc_strings . hash ( hasher) ;
1108- self . cfg . hash ( hasher) ;
11091125 for attr in & self . other_attrs {
11101126 attr. id . hash ( hasher) ;
11111127 }
0 commit comments