44// and `#[unstable (..)]`), but are not declared in one single location
55// (unlike lang features), which means we need to collect them instead.
66
7- use crate :: hir:: intravisit:: { self , NestedVisitorMap , Visitor } ;
8- use crate :: ty:: TyCtxt ;
9- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
10- use rustc_macros:: HashStable ;
7+ use rustc:: hir:: def_id:: LOCAL_CRATE ;
8+ use rustc:: hir:: intravisit:: { self , NestedVisitorMap , Visitor } ;
9+ use rustc:: middle:: lib_features:: LibFeatures ;
10+ use rustc:: ty:: query:: Providers ;
11+ use rustc:: ty:: TyCtxt ;
1112use syntax:: ast:: { Attribute , MetaItem , MetaItemKind } ;
1213use syntax:: symbol:: Symbol ;
1314use syntax_pos:: { sym, Span } ;
1415
1516use rustc_error_codes:: * ;
1617
17- #[ derive( HashStable ) ]
18- pub struct LibFeatures {
19- // A map from feature to stabilisation version.
20- pub stable : FxHashMap < Symbol , Symbol > ,
21- pub unstable : FxHashSet < Symbol > ,
22- }
23-
24- impl LibFeatures {
25- fn new ( ) -> LibFeatures {
26- LibFeatures { stable : Default :: default ( ) , unstable : Default :: default ( ) }
27- }
28-
29- pub fn to_vec ( & self ) -> Vec < ( Symbol , Option < Symbol > ) > {
30- let mut all_features: Vec < _ > = self
31- . stable
32- . iter ( )
33- . map ( |( f, s) | ( * f, Some ( * s) ) )
34- . chain ( self . unstable . iter ( ) . map ( |f| ( * f, None ) ) )
35- . collect ( ) ;
36- all_features. sort_unstable_by_key ( |f| f. 0 . as_str ( ) ) ;
37- all_features
38- }
18+ fn new_lib_features ( ) -> LibFeatures {
19+ LibFeatures { stable : Default :: default ( ) , unstable : Default :: default ( ) }
3920}
4021
4122pub struct LibFeatureCollector < ' tcx > {
@@ -45,7 +26,7 @@ pub struct LibFeatureCollector<'tcx> {
4526
4627impl LibFeatureCollector < ' tcx > {
4728 fn new ( tcx : TyCtxt < ' tcx > ) -> LibFeatureCollector < ' tcx > {
48- LibFeatureCollector { tcx, lib_features : LibFeatures :: new ( ) }
29+ LibFeatureCollector { tcx, lib_features : new_lib_features ( ) }
4930 }
5031
5132 fn extract ( & self , attr : & Attribute ) -> Option < ( Symbol , Option < Symbol > , Span ) > {
@@ -142,7 +123,7 @@ impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
142123 }
143124}
144125
145- pub fn collect ( tcx : TyCtxt < ' _ > ) -> LibFeatures {
126+ fn collect ( tcx : TyCtxt < ' _ > ) -> LibFeatures {
146127 let mut collector = LibFeatureCollector :: new ( tcx) ;
147128 let krate = tcx. hir ( ) . krate ( ) ;
148129 for attr in krate. non_exported_macro_attrs {
@@ -151,3 +132,10 @@ pub fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
151132 intravisit:: walk_crate ( & mut collector, krate) ;
152133 collector. lib_features
153134}
135+
136+ pub fn provide ( providers : & mut Providers < ' _ > ) {
137+ providers. get_lib_features = |tcx, id| {
138+ assert_eq ! ( id, LOCAL_CRATE ) ;
139+ tcx. arena . alloc ( collect ( tcx) )
140+ } ;
141+ }
0 commit comments