1 // Detecting lib features (i.e., features that are not lang features).
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.
8 use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
9 use syntax::symbol::Symbol;
10 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
12 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
13 use errors::DiagnosticId;
15 pub struct LibFeatures {
16 // A map from feature to stabilisation version.
17 pub stable: FxHashMap<Symbol, Symbol>,
18 pub unstable: FxHashSet<Symbol>,
22 fn new() -> LibFeatures {
24 stable: Default::default(),
25 unstable: Default::default(),
29 pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
30 let mut all_features: Vec<_> = self.stable.iter().map(|(f, s)| (*f, Some(*s)))
31 .chain(self.unstable.iter().map(|f| (*f, None)))
33 all_features.sort_unstable_by_key(|f| f.0.as_str());
38 pub struct LibFeatureCollector<'a, 'tcx: 'a> {
39 tcx: TyCtxt<'a, 'tcx, 'tcx>,
40 lib_features: LibFeatures,
43 impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
44 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatureCollector<'a, 'tcx> {
47 lib_features: LibFeatures::new(),
51 fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
52 let stab_attrs = vec!["stable", "unstable", "rustc_const_unstable"];
54 // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
55 // `#[rustc_const_unstable (..)]`).
56 if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
57 attr.check_name(stab_attr)
59 let meta_item = attr.meta();
60 if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {
61 let mut feature = None;
64 if let Some(mi) = meta.meta_item() {
65 // Find the `feature = ".."` meta-item.
66 match (&*mi.name().as_str(), mi.value_str()) {
67 ("feature", val) => feature = val,
68 ("since", val) => since = val,
73 if let Some(feature) = feature {
74 // This additional check for stability is to make sure we
75 // don't emit additional, irrelevant errors for malformed
77 if *stab_attr != "stable" || since.is_some() {
78 return Some((feature, since, attr.span));
81 // We need to iterate over the other attributes, because
82 // `rustc_const_unstable` is not mutually exclusive with
83 // the other stability attributes, so we can't just `break`
91 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
92 let already_in_stable = self.lib_features.stable.contains_key(&feature);
93 let already_in_unstable = self.lib_features.unstable.contains(&feature);
95 match (since, already_in_stable, already_in_unstable) {
96 (Some(since), _, false) => {
97 if let Some(prev_since) = self.lib_features.stable.get(&feature) {
98 if *prev_since != since {
100 "feature `{}` is declared stable since {}, \
101 but was previously declared stable since {}",
106 self.tcx.sess.struct_span_err_with_code(span, &msg,
107 DiagnosticId::Error("E0711".into())).emit();
112 self.lib_features.stable.insert(feature, since);
114 (None, false, _) => {
115 self.lib_features.unstable.insert(feature);
117 (Some(_), _, true) | (None, true, _) => {
119 "feature `{}` is declared {}, but was previously declared {}",
121 if since.is_some() { "stable" } else { "unstable" },
122 if since.is_none() { "stable" } else { "unstable" },
124 self.tcx.sess.struct_span_err_with_code(span, &msg,
125 DiagnosticId::Error("E0711".into())).emit();
131 impl<'a, 'tcx> Visitor<'tcx> for LibFeatureCollector<'a, 'tcx> {
132 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
133 NestedVisitorMap::All(&self.tcx.hir())
136 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
137 if let Some((feature, stable, span)) = self.extract(attr) {
138 self.collect_feature(feature, stable, span);
143 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatures {
144 let mut collector = LibFeatureCollector::new(tcx);
145 intravisit::walk_crate(&mut collector, tcx.hir().krate());
146 collector.lib_features