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