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 rustc_ast::{Attribute, MetaItem, MetaItemKind};
8 use rustc_errors::struct_span_err;
9 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
10 use rustc_middle::hir::map::Map;
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};
17 fn new_lib_features() -> LibFeatures {
18 LibFeatures { stable: Default::default(), unstable: Default::default() }
21 pub struct LibFeatureCollector<'tcx> {
23 lib_features: LibFeatures,
26 impl LibFeatureCollector<'tcx> {
27 fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
28 LibFeatureCollector { tcx, lib_features: new_lib_features() }
31 fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
32 let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable];
34 // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
35 // `#[rustc_const_unstable (..)]`).
36 if let Some(stab_attr) =
37 stab_attrs.iter().find(|stab_attr| self.tcx.sess.check_name(attr, **stab_attr))
39 let meta_item = attr.meta();
40 if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
41 let mut feature = None;
44 if let Some(mi) = meta.meta_item() {
45 // Find the `feature = ".."` meta-item.
46 match (mi.name_or_empty(), mi.value_str()) {
47 (sym::feature, val) => feature = val,
48 (sym::since, val) => since = val,
53 if let Some(feature) = feature {
54 // This additional check for stability is to make sure we
55 // don't emit additional, irrelevant errors for malformed
57 if *stab_attr != sym::stable || since.is_some() {
58 return Some((feature, since, attr.span));
61 // We need to iterate over the other attributes, because
62 // `rustc_const_unstable` is not mutually exclusive with
63 // the other stability attributes, so we can't just `break`
71 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
72 let already_in_stable = self.lib_features.stable.contains_key(&feature);
73 let already_in_unstable = self.lib_features.unstable.contains(&feature);
75 match (since, already_in_stable, already_in_unstable) {
76 (Some(since), _, false) => {
77 if let Some(prev_since) = self.lib_features.stable.get(&feature) {
78 if *prev_since != since {
79 self.span_feature_error(
82 "feature `{}` is declared stable since {}, \
83 but was previously declared stable since {}",
84 feature, since, prev_since,
91 self.lib_features.stable.insert(feature, since);
94 self.lib_features.unstable.insert(feature);
96 (Some(_), _, true) | (None, true, _) => {
97 self.span_feature_error(
100 "feature `{}` is declared {}, but was previously declared {}",
102 if since.is_some() { "stable" } else { "unstable" },
103 if since.is_none() { "stable" } else { "unstable" },
110 fn span_feature_error(&self, span: Span, msg: &str) {
111 struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
115 impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
116 type Map = Map<'tcx>;
118 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
119 NestedVisitorMap::All(self.tcx.hir())
122 fn visit_attribute(&mut self, _: rustc_hir::HirId, attr: &'tcx Attribute) {
123 if let Some((feature, stable, span)) = self.extract(attr) {
124 self.collect_feature(feature, stable, span);
129 fn get_lib_features(tcx: TyCtxt<'_>, (): ()) -> LibFeatures {
130 let mut collector = LibFeatureCollector::new(tcx);
131 let krate = tcx.hir().krate();
132 for attr in krate.non_exported_macro_attrs {
133 collector.visit_attribute(rustc_hir::CRATE_HIR_ID, attr);
135 intravisit::walk_crate(&mut collector, krate);
136 collector.lib_features
139 pub fn provide(providers: &mut Providers) {
140 providers.get_lib_features = get_lib_features;