@@ -421,7 +421,7 @@ impl Item {
421421 kind,
422422 box ast_attrs. clean ( cx) ,
423423 cx,
424- ast_attrs. cfg ( cx. sess ( ) ) ,
424+ ast_attrs. cfg ( cx. tcx ) ,
425425 )
426426 }
427427
@@ -747,7 +747,7 @@ crate trait AttributesExt {
747747
748748 fn other_attrs ( & self ) -> Vec < ast:: Attribute > ;
749749
750- fn cfg ( & self , sess : & Session ) -> Option < Arc < Cfg > > ;
750+ fn cfg ( & self , tcx : TyCtxt < ' _ > ) -> Option < Arc < Cfg > > ;
751751}
752752
753753impl AttributesExt for [ ast:: Attribute ] {
@@ -772,8 +772,52 @@ impl AttributesExt for [ast::Attribute] {
772772 self . iter ( ) . filter ( |attr| attr. doc_str ( ) . is_none ( ) ) . cloned ( ) . collect ( )
773773 }
774774
775- fn cfg ( & self , sess : & Session ) -> Option < Arc < Cfg > > {
776- let mut cfg = Cfg :: True ;
775+ fn cfg ( & self , tcx : TyCtxt < ' _ > ) -> Option < Arc < Cfg > > {
776+ let sess = tcx. sess ;
777+ let doc_cfg_active = tcx. features ( ) . doc_cfg ;
778+
779+ trait SingleExt {
780+ type Item ;
781+ fn single ( self ) -> Option < Self :: Item > ;
782+ }
783+
784+ impl < T : IntoIterator > SingleExt for T {
785+ type Item = T :: Item ;
786+ fn single ( self ) -> Option < Self :: Item > {
787+ let mut iter = self . into_iter ( ) ;
788+ let item = iter. next ( ) ?;
789+ iter. next ( ) . is_none ( ) . then_some ( ( ) ) ?;
790+ Some ( item)
791+ }
792+ }
793+
794+ let mut cfg = if doc_cfg_active {
795+ let mut doc_cfg = self
796+ . iter ( )
797+ . filter ( |attr| attr. has_name ( sym:: doc) )
798+ . filter_map ( |attr| Some ( attr. meta_item_list ( ) ?. single ( ) ?) )
799+ . filter ( |attr| attr. has_name ( sym:: cfg) )
800+ . filter_map ( |attr| Some ( attr. meta_item_list ( ) ?. single ( ) ?. meta_item ( ) ?. clone ( ) ) )
801+ . peekable ( ) ;
802+ if doc_cfg. peek ( ) . is_some ( ) {
803+ doc_cfg
804+ . filter_map ( |attr| {
805+ Cfg :: parse ( & attr) . map_err ( |e| sess. diagnostic ( ) . span_err ( e. span , e. msg ) ) . ok ( )
806+ } )
807+ . fold ( Cfg :: True , |cfg, new_cfg| cfg & new_cfg)
808+ } else {
809+ self
810+ . iter ( )
811+ . filter ( |attr| attr. has_name ( sym:: cfg) )
812+ . filter_map ( |attr| Some ( attr. meta_item_list ( ) ?. single ( ) ?. meta_item ( ) ?. clone ( ) ) )
813+ . filter_map ( |attr| {
814+ Cfg :: parse ( & attr) . map_err ( |e| sess. diagnostic ( ) . span_err ( e. span , e. msg ) ) . ok ( )
815+ } )
816+ . fold ( Cfg :: True , |cfg, new_cfg| cfg & new_cfg)
817+ }
818+ } else {
819+ Cfg :: True
820+ } ;
777821
778822 for attr in self . iter ( ) {
779823 // #[doc]
0 commit comments