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_attr::{rust_version_symbol, VERSION_PLACEHOLDER};
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 use crate::errors::{FeaturePreviouslyDeclared, FeatureStableTwice};
19 fn new_lib_features() -> LibFeatures {
20 LibFeatures { stable: Default::default(), unstable: Default::default() }
23 pub struct LibFeatureCollector<'tcx> {
25 lib_features: LibFeatures,
28 impl<'tcx> LibFeatureCollector<'tcx> {
29 fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
30 LibFeatureCollector { tcx, lib_features: new_lib_features() }
33 fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
37 sym::rustc_const_stable,
38 sym::rustc_const_unstable,
39 sym::rustc_default_body_unstable,
42 // Find a stability attribute: one of #[stable(…)], #[unstable(…)],
43 // #[rustc_const_stable(…)], #[rustc_const_unstable(…)] or #[rustc_default_body_unstable].
44 if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| attr.has_name(**stab_attr)) {
45 let meta_kind = attr.meta_kind();
46 if let Some(MetaItemKind::List(ref metas)) = meta_kind {
47 let mut feature = None;
50 if let Some(mi) = meta.meta_item() {
51 // Find the `feature = ".."` meta-item.
52 match (mi.name_or_empty(), mi.value_str()) {
53 (sym::feature, val) => feature = val,
54 (sym::since, val) => since = val,
60 if let Some(s) = since && s.as_str() == VERSION_PLACEHOLDER {
61 since = Some(rust_version_symbol());
64 if let Some(feature) = feature {
65 // This additional check for stability is to make sure we
66 // don't emit additional, irrelevant errors for malformed
68 let is_unstable = matches!(
71 | sym::rustc_const_unstable
72 | sym::rustc_default_body_unstable
74 if since.is_some() || is_unstable {
75 return Some((feature, since, attr.span));
78 // We need to iterate over the other attributes, because
79 // `rustc_const_unstable` is not mutually exclusive with
80 // the other stability attributes, so we can't just `break`
88 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
89 let already_in_stable = self.lib_features.stable.contains_key(&feature);
90 let already_in_unstable = self.lib_features.unstable.contains_key(&feature);
92 match (since, already_in_stable, already_in_unstable) {
93 (Some(since), _, false) => {
94 if let Some((prev_since, _)) = self.lib_features.stable.get(&feature) {
95 if *prev_since != since {
96 self.tcx.sess.emit_err(FeatureStableTwice {
100 prev_since: *prev_since,
106 self.lib_features.stable.insert(feature, (since, span));
108 (None, false, _) => {
109 self.lib_features.unstable.insert(feature, span);
111 (Some(_), _, true) | (None, true, _) => {
112 let declared = if since.is_some() { "stable" } else { "unstable" };
113 let prev_declared = if since.is_none() { "stable" } else { "unstable" };
114 self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
125 impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {
126 type NestedFilter = nested_filter::All;
128 fn nested_visit_map(&mut self) -> Self::Map {
132 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
133 if let Some((feature, stable, span)) = self.extract(attr) {
134 self.collect_feature(feature, stable, span);
139 fn lib_features(tcx: TyCtxt<'_>, (): ()) -> LibFeatures {
140 // If `staged_api` is not enabled then we aren't allowed to define lib
141 // features; there is no point collecting them.
142 if !tcx.features().staged_api {
143 return new_lib_features();
146 let mut collector = LibFeatureCollector::new(tcx);
147 tcx.hir().walk_attributes(&mut collector);
148 collector.lib_features
151 pub fn provide(providers: &mut Providers) {
152 providers.lib_features = lib_features;