]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/lib_features.rs
Rollup merge of #68378 - billyrieger:btreemap-remove-entry, r=KodrAus
[rust.git] / src / librustc_passes / lib_features.rs
1 // Detecting lib features (i.e., features that are not lang features).
2 //
3 // These are declared using stability attributes (e.g., `#[stable (..)]`
4 // and `#[unstable (..)]`), but are not declared in one single location
5 // (unlike lang features), which means we need to collect them instead.
6
7 use rustc::hir::map::Map;
8 use rustc::middle::lib_features::LibFeatures;
9 use rustc::ty::query::Providers;
10 use rustc::ty::TyCtxt;
11 use rustc_errors::struct_span_err;
12 use rustc_hir::def_id::LOCAL_CRATE;
13 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
14 use rustc_span::symbol::Symbol;
15 use rustc_span::{sym, Span};
16 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
17
18 fn new_lib_features() -> LibFeatures {
19     LibFeatures { stable: Default::default(), unstable: Default::default() }
20 }
21
22 pub struct LibFeatureCollector<'tcx> {
23     tcx: TyCtxt<'tcx>,
24     lib_features: LibFeatures,
25 }
26
27 impl LibFeatureCollector<'tcx> {
28     fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
29         LibFeatureCollector { tcx, lib_features: new_lib_features() }
30     }
31
32     fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
33         let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable];
34
35         // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
36         // `#[rustc_const_unstable (..)]`).
37         if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| attr.check_name(**stab_attr)) {
38             let meta_item = attr.meta();
39             if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
40                 let mut feature = None;
41                 let mut since = None;
42                 for meta in metas {
43                     if let Some(mi) = meta.meta_item() {
44                         // Find the `feature = ".."` meta-item.
45                         match (mi.name_or_empty(), mi.value_str()) {
46                             (sym::feature, val) => feature = val,
47                             (sym::since, val) => since = val,
48                             _ => {}
49                         }
50                     }
51                 }
52                 if let Some(feature) = feature {
53                     // This additional check for stability is to make sure we
54                     // don't emit additional, irrelevant errors for malformed
55                     // attributes.
56                     if *stab_attr != sym::stable || since.is_some() {
57                         return Some((feature, since, attr.span));
58                     }
59                 }
60                 // We need to iterate over the other attributes, because
61                 // `rustc_const_unstable` is not mutually exclusive with
62                 // the other stability attributes, so we can't just `break`
63                 // here.
64             }
65         }
66
67         None
68     }
69
70     fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
71         let already_in_stable = self.lib_features.stable.contains_key(&feature);
72         let already_in_unstable = self.lib_features.unstable.contains(&feature);
73
74         match (since, already_in_stable, already_in_unstable) {
75             (Some(since), _, false) => {
76                 if let Some(prev_since) = self.lib_features.stable.get(&feature) {
77                     if *prev_since != since {
78                         self.span_feature_error(
79                             span,
80                             &format!(
81                                 "feature `{}` is declared stable since {}, \
82                                  but was previously declared stable since {}",
83                                 feature, since, prev_since,
84                             ),
85                         );
86                         return;
87                     }
88                 }
89
90                 self.lib_features.stable.insert(feature, since);
91             }
92             (None, false, _) => {
93                 self.lib_features.unstable.insert(feature);
94             }
95             (Some(_), _, true) | (None, true, _) => {
96                 self.span_feature_error(
97                     span,
98                     &format!(
99                         "feature `{}` is declared {}, but was previously declared {}",
100                         feature,
101                         if since.is_some() { "stable" } else { "unstable" },
102                         if since.is_none() { "stable" } else { "unstable" },
103                     ),
104                 );
105             }
106         }
107     }
108
109     fn span_feature_error(&self, span: Span, msg: &str) {
110         struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg,).emit();
111     }
112 }
113
114 impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
115     type Map = Map<'tcx>;
116
117     fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
118         NestedVisitorMap::All(&self.tcx.hir())
119     }
120
121     fn visit_attribute(&mut self, attr: &'tcx Attribute) {
122         if let Some((feature, stable, span)) = self.extract(attr) {
123             self.collect_feature(feature, stable, span);
124         }
125     }
126 }
127
128 fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
129     let mut collector = LibFeatureCollector::new(tcx);
130     let krate = tcx.hir().krate();
131     for attr in krate.non_exported_macro_attrs {
132         collector.visit_attribute(attr);
133     }
134     intravisit::walk_crate(&mut collector, krate);
135     collector.lib_features
136 }
137
138 pub fn provide(providers: &mut Providers<'_>) {
139     providers.get_lib_features = |tcx, id| {
140         assert_eq!(id, LOCAL_CRATE);
141         tcx.arena.alloc(collect(tcx))
142     };
143 }