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}
;
11 use syntax_pos
::{Span, sym}
;
12 use rustc_data_structures
::fx
::{FxHashSet, FxHashMap}
;
13 use rustc_macros
::HashStable
;
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
{
27 stable
: Default
::default(),
28 unstable
: Default
::default(),
32 pub fn to_vec(&self) -> Vec
<(Symbol
, Option
<Symbol
>)> {
33 let mut all_features
: Vec
<_
> = self.stable
.iter().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
> {
50 lib_features
: LibFeatures
::new(),
54 fn extract(&self, attr
: &Attribute
) -> Option
<(Symbol
, Option
<Symbol
>, Span
)> {
55 let stab_attrs
= [sym
::stable
, sym
::unstable
, sym
::rustc_const_unstable
];
57 // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
58 // `#[rustc_const_unstable (..)]`).
59 if let Some(stab_attr
) = stab_attrs
.iter().find(|stab_attr
| {
60 attr
.check_name(**stab_attr
)
62 let meta_item
= attr
.meta();
63 if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }
) = meta_item
{
64 let mut feature
= None
;
67 if let Some(mi
) = meta
.meta_item() {
68 // Find the `feature = ".."` meta-item.
69 match (mi
.name_or_empty(), mi
.value_str()) {
70 (sym
::feature
, val
) => feature
= val
,
71 (sym
::since
, val
) => since
= val
,
76 if let Some(feature
) = feature
{
77 // This additional check for stability is to make sure we
78 // don't emit additional, irrelevant errors for malformed
80 if *stab_attr
!= sym
::stable
|| since
.is_some() {
81 return Some((feature
, since
, attr
.span
));
84 // We need to iterate over the other attributes, because
85 // `rustc_const_unstable` is not mutually exclusive with
86 // the other stability attributes, so we can't just `break`
94 fn collect_feature(&mut self, feature
: Symbol
, since
: Option
<Symbol
>, span
: Span
) {
95 let already_in_stable
= self.lib_features
.stable
.contains_key(&feature
);
96 let already_in_unstable
= self.lib_features
.unstable
.contains(&feature
);
98 match (since
, already_in_stable
, already_in_unstable
) {
99 (Some(since
), _
, false) => {
100 if let Some(prev_since
) = self.lib_features
.stable
.get(&feature
) {
101 if *prev_since
!= since
{
102 self.span_feature_error(
105 "feature `{}` is declared stable since {}, \
106 but was previously declared stable since {}",
116 self.lib_features
.stable
.insert(feature
, since
);
118 (None
, false, _
) => {
119 self.lib_features
.unstable
.insert(feature
);
121 (Some(_
), _
, true) | (None
, true, _
) => {
122 self.span_feature_error(
125 "feature `{}` is declared {}, but was previously declared {}",
127 if since
.is_some() { "stable" }
else { "unstable" }
,
128 if since
.is_none() { "stable" }
else { "unstable" }
,
135 fn span_feature_error(&self, span
: Span
, msg
: &str) {
145 impl Visitor
<'tcx
> for LibFeatureCollector
<'tcx
> {
146 fn nested_visit_map
<'this
>(&'this
mut self) -> NestedVisitorMap
<'this
, 'tcx
> {
147 NestedVisitorMap
::All(&self.tcx
.hir())
150 fn visit_attribute(&mut self, attr
: &'tcx Attribute
) {
151 if let Some((feature
, stable
, span
)) = self.extract(attr
) {
152 self.collect_feature(feature
, stable
, span
);
157 pub fn collect(tcx
: TyCtxt
<'_
>) -> LibFeatures
{
158 let mut collector
= LibFeatureCollector
::new(tcx
);
159 let krate
= tcx
.hir().krate();
160 for attr
in &krate
.non_exported_macro_attrs
{
161 collector
.visit_attribute(attr
);
163 intravisit
::walk_crate(&mut collector
, krate
);
164 collector
.lib_features