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.
7 use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
9 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
10 use rustc_macros::HashStable;
11 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
12 use syntax::symbol::Symbol;
13 use syntax_pos::{sym, Span};
15 use rustc_error_codes::*;
18 pub struct LibFeatures {
19 // A map from feature to stabilisation version.
20 pub stable: FxHashMap<Symbol, Symbol>,
21 pub unstable: FxHashSet<Symbol>,
25 fn new() -> LibFeatures {
26 LibFeatures { stable: Default::default(), unstable: Default::default() }
29 pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
30 let mut all_features: Vec<_> = self
33 .map(|(f, s)| (*f, Some(*s)))
34 .chain(self.unstable.iter().map(|f| (*f, None)))
36 all_features.sort_unstable_by_key(|f| f.0.as_str());
41 pub struct LibFeatureCollector<'tcx> {
43 lib_features: LibFeatures,
46 impl LibFeatureCollector<'tcx> {
47 fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
48 LibFeatureCollector { tcx, lib_features: LibFeatures::new() }
51 fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
52 let stab_attrs = [sym::stable, sym::unstable, sym::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| attr.check_name(**stab_attr)) {
57 let meta_item = attr.meta();
58 if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
59 let mut feature = None;
62 if let Some(mi) = meta.meta_item() {
63 // Find the `feature = ".."` meta-item.
64 match (mi.name_or_empty(), mi.value_str()) {
65 (sym::feature, val) => feature = val,
66 (sym::since, val) => since = val,
71 if let Some(feature) = feature {
72 // This additional check for stability is to make sure we
73 // don't emit additional, irrelevant errors for malformed
75 if *stab_attr != sym::stable || since.is_some() {
76 return Some((feature, since, attr.span));
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`
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(&feature);
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(
100 "feature `{}` is declared stable since {}, \
101 but was previously declared stable since {}",
102 feature, since, prev_since,
109 self.lib_features.stable.insert(feature, since);
111 (None, false, _) => {
112 self.lib_features.unstable.insert(feature);
114 (Some(_), _, true) | (None, true, _) => {
115 self.span_feature_error(
118 "feature `{}` is declared {}, but was previously declared {}",
120 if since.is_some() { "stable" } else { "unstable" },
121 if since.is_none() { "stable" } else { "unstable" },
128 fn span_feature_error(&self, span: Span, msg: &str) {
129 struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg,).emit();
133 impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
134 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
135 NestedVisitorMap::All(&self.tcx.hir())
138 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
139 if let Some((feature, stable, span)) = self.extract(attr) {
140 self.collect_feature(feature, stable, span);
145 pub fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
146 let mut collector = LibFeatureCollector::new(tcx);
147 let krate = tcx.hir().krate();
148 for attr in krate.non_exported_macro_attrs {
149 collector.visit_attribute(attr);
151 intravisit::walk_crate(&mut collector, krate);
152 collector.lib_features