]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/lib_features.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc / middle / 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 crate::ty::TyCtxt;
8 use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
9 use syntax::symbol::Symbol;
10 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
11 use syntax_pos::{Span, sym};
12 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
13 use rustc_macros::HashStable;
14
15 use rustc_error_codes::*;
16
17 #[derive(HashStable)]
18 pub struct LibFeatures {
19 // A map from feature to stabilisation version.
20 pub stable: FxHashMap<Symbol, Symbol>,
21 pub unstable: FxHashSet<Symbol>,
22 }
23
24 impl LibFeatures {
25 fn new() -> LibFeatures {
26 LibFeatures {
27 stable: Default::default(),
28 unstable: Default::default(),
29 }
30 }
31
32 pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
33 let mut all_features: Vec<_> = self.stable.iter().map(|(f, s)| (*f, Some(*s)))
34 .chain(self.unstable.iter().map(|f| (*f, None)))
35 .collect();
36 all_features.sort_unstable_by_key(|f| f.0.as_str());
37 all_features
38 }
39 }
40
41 pub struct LibFeatureCollector<'tcx> {
42 tcx: TyCtxt<'tcx>,
43 lib_features: LibFeatures,
44 }
45
46 impl LibFeatureCollector<'tcx> {
47 fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
48 LibFeatureCollector {
49 tcx,
50 lib_features: LibFeatures::new(),
51 }
52 }
53
54 fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
55 let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable];
56
57 // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
58 // `#[rustc_const_unstable (..)]`).
59 if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
60 attr.check_name(**stab_attr)
61 }) {
62 let meta_item = attr.meta();
63 if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
64 let mut feature = None;
65 let mut since = None;
66 for meta in metas {
67 if let Some(mi) = meta.meta_item() {
68 // Find the `feature = ".."` meta-item.
69 match (mi.name_or_empty(), mi.value_str()) {
70 (sym::feature, val) => feature = val,
71 (sym::since, val) => since = val,
72 _ => {}
73 }
74 }
75 }
76 if let Some(feature) = feature {
77 // This additional check for stability is to make sure we
78 // don't emit additional, irrelevant errors for malformed
79 // attributes.
80 if *stab_attr != sym::stable || since.is_some() {
81 return Some((feature, since, attr.span));
82 }
83 }
84 // We need to iterate over the other attributes, because
85 // `rustc_const_unstable` is not mutually exclusive with
86 // the other stability attributes, so we can't just `break`
87 // here.
88 }
89 }
90
91 None
92 }
93
94 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
95 let already_in_stable = self.lib_features.stable.contains_key(&feature);
96 let already_in_unstable = self.lib_features.unstable.contains(&feature);
97
98 match (since, already_in_stable, already_in_unstable) {
99 (Some(since), _, false) => {
100 if let Some(prev_since) = self.lib_features.stable.get(&feature) {
101 if *prev_since != since {
102 self.span_feature_error(
103 span,
104 &format!(
105 "feature `{}` is declared stable since {}, \
106 but was previously declared stable since {}",
107 feature,
108 since,
109 prev_since,
110 ),
111 );
112 return;
113 }
114 }
115
116 self.lib_features.stable.insert(feature, since);
117 }
118 (None, false, _) => {
119 self.lib_features.unstable.insert(feature);
120 }
121 (Some(_), _, true) | (None, true, _) => {
122 self.span_feature_error(
123 span,
124 &format!(
125 "feature `{}` is declared {}, but was previously declared {}",
126 feature,
127 if since.is_some() { "stable" } else { "unstable" },
128 if since.is_none() { "stable" } else { "unstable" },
129 ),
130 );
131 }
132 }
133 }
134
135 fn span_feature_error(&self, span: Span, msg: &str) {
136 struct_span_err!(
137 self.tcx.sess,
138 span,
139 E0711,
140 "{}", &msg,
141 ).emit();
142 }
143 }
144
145 impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
146 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
147 NestedVisitorMap::All(&self.tcx.hir())
148 }
149
150 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
151 if let Some((feature, stable, span)) = self.extract(attr) {
152 self.collect_feature(feature, stable, span);
153 }
154 }
155 }
156
157 pub fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
158 let mut collector = LibFeatureCollector::new(tcx);
159 let krate = tcx.hir().krate();
160 for attr in &krate.non_exported_macro_attrs {
161 collector.visit_attribute(attr);
162 }
163 intravisit::walk_crate(&mut collector, krate);
164 collector.lib_features
165 }