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