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