]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/lib_features.rs
review
[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 (..)]` and `#[unstable (..)]`),
4 //! but are not declared in one single location (unlike lang features), which means we need to
5 //! collect them instead.
6
7 use rustc_ast::{Attribute, MetaItemKind};
8 use rustc_attr::VERSION_PLACEHOLDER;
9 use rustc_errors::struct_span_err;
10 use rustc_hir::intravisit::Visitor;
11 use rustc_middle::hir::nested_filter;
12 use rustc_middle::middle::lib_features::LibFeatures;
13 use rustc_middle::ty::query::Providers;
14 use rustc_middle::ty::TyCtxt;
15 use rustc_span::symbol::Symbol;
16 use rustc_span::{sym, Span};
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<'tcx> 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 = [
34             sym::stable,
35             sym::unstable,
36             sym::rustc_const_stable,
37             sym::rustc_const_unstable,
38             sym::rustc_default_body_unstable,
39         ];
40
41         // Find a stability attribute: one of #[stable(…)], #[unstable(…)],
42         // #[rustc_const_stable(…)], #[rustc_const_unstable(…)] or #[rustc_default_body_unstable].
43         if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| attr.has_name(**stab_attr)) {
44             let meta_kind = attr.meta_kind();
45             if let Some(MetaItemKind::List(ref metas)) = meta_kind {
46                 let mut feature = None;
47                 let mut since = None;
48                 for meta in metas {
49                     if let Some(mi) = meta.meta_item() {
50                         // Find the `feature = ".."` meta-item.
51                         match (mi.name_or_empty(), mi.value_str()) {
52                             (sym::feature, val) => feature = val,
53                             (sym::since, val) => since = val,
54                             _ => {}
55                         }
56                     }
57                 }
58
59                 if let Some(s) = since && s.as_str() == VERSION_PLACEHOLDER {
60                     let version = option_env!("CFG_VERSION").unwrap_or("<current>");
61                     let version = version.split(' ').next().unwrap();
62                     since = Some(Symbol::intern(&version));
63                 }
64
65                 if let Some(feature) = feature {
66                     // This additional check for stability is to make sure we
67                     // don't emit additional, irrelevant errors for malformed
68                     // attributes.
69                     let is_unstable = matches!(
70                         *stab_attr,
71                         sym::unstable
72                             | sym::rustc_const_unstable
73                             | sym::rustc_default_body_unstable
74                     );
75                     if since.is_some() || is_unstable {
76                         return Some((feature, since, attr.span));
77                     }
78                 }
79                 // We need to iterate over the other attributes, because
80                 // `rustc_const_unstable` is not mutually exclusive with
81                 // the other stability attributes, so we can't just `break`
82                 // here.
83             }
84         }
85
86         None
87     }
88
89     fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
90         let already_in_stable = self.lib_features.stable.contains_key(&feature);
91         let already_in_unstable = self.lib_features.unstable.contains_key(&feature);
92
93         match (since, already_in_stable, already_in_unstable) {
94             (Some(since), _, false) => {
95                 if let Some((prev_since, _)) = self.lib_features.stable.get(&feature) {
96                     if *prev_since != since {
97                         self.span_feature_error(
98                             span,
99                             &format!(
100                                 "feature `{}` is declared stable since {}, \
101                                  but was previously declared stable since {}",
102                                 feature, since, prev_since,
103                             ),
104                         );
105                         return;
106                     }
107                 }
108
109                 self.lib_features.stable.insert(feature, (since, span));
110             }
111             (None, false, _) => {
112                 self.lib_features.unstable.insert(feature, span);
113             }
114             (Some(_), _, true) | (None, true, _) => {
115                 self.span_feature_error(
116                     span,
117                     &format!(
118                         "feature `{}` is declared {}, but was previously declared {}",
119                         feature,
120                         if since.is_some() { "stable" } else { "unstable" },
121                         if since.is_none() { "stable" } else { "unstable" },
122                     ),
123                 );
124             }
125         }
126     }
127
128     fn span_feature_error(&self, span: Span, msg: &str) {
129         struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
130     }
131 }
132
133 impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {
134     type NestedFilter = nested_filter::All;
135
136     fn nested_visit_map(&mut self) -> Self::Map {
137         self.tcx.hir()
138     }
139
140     fn visit_attribute(&mut self, attr: &'tcx Attribute) {
141         if let Some((feature, stable, span)) = self.extract(attr) {
142             self.collect_feature(feature, stable, span);
143         }
144     }
145 }
146
147 fn lib_features(tcx: TyCtxt<'_>, (): ()) -> LibFeatures {
148     let mut collector = LibFeatureCollector::new(tcx);
149     tcx.hir().walk_attributes(&mut collector);
150     collector.lib_features
151 }
152
153 pub fn provide(providers: &mut Providers) {
154     providers.lib_features = lib_features;
155 }