1 //! Detecting lib features (i.e., features that are not lang features).
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.
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};
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<'tcx> 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)> {
33 [sym::stable, sym::unstable, sym::rustc_const_stable, sym::rustc_const_unstable];
35 // Find a stability attribute: one of #[stable(…)], #[unstable(…)],
36 // #[rustc_const_stable(…)], or #[rustc_const_unstable(…)].
37 if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| attr.has_name(**stab_attr)) {
38 let meta_kind = attr.meta_kind();
39 if let Some(MetaItemKind::List(ref metas)) = meta_kind {
40 let mut feature = None;
43 if let Some(mi) = meta.meta_item() {
44 // Find the `feature = ".."` meta-item.
45 match (mi.name_or_empty(), mi.value_str()) {
46 (sym::feature, val) => feature = val,
47 (sym::since, val) => since = val,
52 if let Some(feature) = feature {
53 // This additional check for stability is to make sure we
54 // don't emit additional, irrelevant errors for malformed
57 matches!(*stab_attr, sym::unstable | sym::rustc_const_unstable);
58 if since.is_some() || is_unstable {
59 return Some((feature, since, attr.span));
62 // We need to iterate over the other attributes, because
63 // `rustc_const_unstable` is not mutually exclusive with
64 // the other stability attributes, so we can't just `break`
72 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
73 let already_in_stable = self.lib_features.stable.contains_key(&feature);
74 let already_in_unstable = self.lib_features.unstable.contains_key(&feature);
76 match (since, already_in_stable, already_in_unstable) {
77 (Some(since), _, false) => {
78 if let Some((prev_since, _)) = self.lib_features.stable.get(&feature) {
79 if *prev_since != since {
80 self.span_feature_error(
83 "feature `{}` is declared stable since {}, \
84 but was previously declared stable since {}",
85 feature, since, prev_since,
92 self.lib_features.stable.insert(feature, (since, span));
95 self.lib_features.unstable.insert(feature, span);
97 (Some(_), _, true) | (None, true, _) => {
98 self.span_feature_error(
101 "feature `{}` is declared {}, but was previously declared {}",
103 if since.is_some() { "stable" } else { "unstable" },
104 if since.is_none() { "stable" } else { "unstable" },
111 fn span_feature_error(&self, span: Span, msg: &str) {
112 struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
116 impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {
117 type NestedFilter = nested_filter::All;
119 fn nested_visit_map(&mut self) -> Self::Map {
123 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
124 if let Some((feature, stable, span)) = self.extract(attr) {
125 self.collect_feature(feature, stable, span);
130 fn lib_features(tcx: TyCtxt<'_>, (): ()) -> LibFeatures {
131 let mut collector = LibFeatureCollector::new(tcx);
132 tcx.hir().walk_attributes(&mut collector);
133 collector.lib_features
136 pub fn provide(providers: &mut Providers) {
137 providers.lib_features = lib_features;