]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/lib_features.rs
Rollup merge of #58832 - pietroalbini:appveyor-hyperv, r=kennytm
[rust.git] / src / librustc / middle / 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 crate::ty::TyCtxt;
8 use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
9 use syntax::symbol::Symbol;
10 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
11 use syntax_pos::Span;
12 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
13 use errors::DiagnosticId;
14
15 pub struct LibFeatures {
16     // A map from feature to stabilisation version.
17     pub stable: FxHashMap<Symbol, Symbol>,
18     pub unstable: FxHashSet<Symbol>,
19 }
20
21 impl LibFeatures {
22     fn new() -> LibFeatures {
23         LibFeatures {
24             stable: Default::default(),
25             unstable: Default::default(),
26         }
27     }
28
29     pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
30         let mut all_features: Vec<_> = self.stable.iter().map(|(f, s)| (*f, Some(*s)))
31             .chain(self.unstable.iter().map(|f| (*f, None)))
32             .collect();
33         all_features.sort_unstable_by_key(|f| f.0.as_str());
34         all_features
35     }
36 }
37
38 pub struct LibFeatureCollector<'a, 'tcx: 'a> {
39     tcx: TyCtxt<'a, 'tcx, 'tcx>,
40     lib_features: LibFeatures,
41 }
42
43 impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
44     fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatureCollector<'a, 'tcx> {
45         LibFeatureCollector {
46             tcx,
47             lib_features: LibFeatures::new(),
48         }
49     }
50
51     fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
52         let stab_attrs = vec!["stable", "unstable", "rustc_const_unstable"];
53
54         // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
55         // `#[rustc_const_unstable (..)]`).
56         if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
57             attr.check_name(stab_attr)
58         }) {
59             let meta_item = attr.meta();
60             if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {
61                 let mut feature = None;
62                 let mut since = None;
63                 for meta in metas {
64                     if let Some(mi) = meta.meta_item() {
65                         // Find the `feature = ".."` meta-item.
66                         match (&*mi.name().as_str(), mi.value_str()) {
67                             ("feature", val) => feature = val,
68                             ("since", val) => since = val,
69                             _ => {}
70                         }
71                     }
72                 }
73                 if let Some(feature) = feature {
74                     // This additional check for stability is to make sure we
75                     // don't emit additional, irrelevant errors for malformed
76                     // attributes.
77                     if *stab_attr != "stable" || since.is_some() {
78                         return Some((feature, since, attr.span));
79                     }
80                 }
81                 // We need to iterate over the other attributes, because
82                 // `rustc_const_unstable` is not mutually exclusive with
83                 // the other stability attributes, so we can't just `break`
84                 // here.
85             }
86         }
87
88         None
89     }
90
91     fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
92         let already_in_stable = self.lib_features.stable.contains_key(&feature);
93         let already_in_unstable = self.lib_features.unstable.contains(&feature);
94
95         match (since, already_in_stable, already_in_unstable) {
96             (Some(since), _, false) => {
97                 if let Some(prev_since) = self.lib_features.stable.get(&feature) {
98                     if *prev_since != since {
99                         let msg = format!(
100                             "feature `{}` is declared stable since {}, \
101                              but was previously declared stable since {}",
102                             feature,
103                             since,
104                             prev_since,
105                         );
106                         self.tcx.sess.struct_span_err_with_code(span, &msg,
107                             DiagnosticId::Error("E0711".into())).emit();
108                         return;
109                     }
110                 }
111
112                 self.lib_features.stable.insert(feature, since);
113             }
114             (None, false, _) => {
115                 self.lib_features.unstable.insert(feature);
116             }
117             (Some(_), _, true) | (None, true, _) => {
118                 let msg = format!(
119                     "feature `{}` is declared {}, but was previously declared {}",
120                     feature,
121                     if since.is_some() { "stable" } else { "unstable" },
122                     if since.is_none() { "stable" } else { "unstable" },
123                 );
124                 self.tcx.sess.struct_span_err_with_code(span, &msg,
125                     DiagnosticId::Error("E0711".into())).emit();
126             }
127         }
128     }
129 }
130
131 impl<'a, 'tcx> Visitor<'tcx> for LibFeatureCollector<'a, 'tcx> {
132     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
133         NestedVisitorMap::All(&self.tcx.hir())
134     }
135
136     fn visit_attribute(&mut self, attr: &'tcx Attribute) {
137         if let Some((feature, stable, span)) = self.extract(attr) {
138             self.collect_feature(feature, stable, span);
139         }
140     }
141 }
142
143 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatures {
144     let mut collector = LibFeatureCollector::new(tcx);
145     intravisit::walk_crate(&mut collector, tcx.hir().krate());
146     collector.lib_features
147 }