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