1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Detecting lib features (i.e. features that are not lang features).
13 // These are declared using stability attributes (e.g. `#[stable(..)]`
14 // and `#[unstable(..)]`), but are not declared in one single location
15 // (unlike lang features), which means we need to collect them instead.
18 use syntax::symbol::Symbol;
19 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
20 use syntax_pos::{Span, DUMMY_SP};
22 use hir::itemlikevisit::ItemLikeVisitor;
23 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
24 use errors::DiagnosticId;
26 pub struct LibFeatures {
27 // A map from feature to stabilisation version.
28 pub stable: FxHashMap<Symbol, Symbol>,
29 pub unstable: FxHashSet<Symbol>,
33 fn new() -> LibFeatures {
36 unstable: FxHashSet(),
40 pub fn iter(&self) -> Vec<(Symbol, Option<Symbol>)> {
41 self.stable.iter().map(|(f, s)| (*f, Some(*s)))
42 .chain(self.unstable.iter().map(|f| (*f, None)))
47 pub struct LibFeatureCollector<'a, 'tcx: 'a> {
48 tcx: TyCtxt<'a, 'tcx, 'tcx>,
49 lib_features: LibFeatures,
52 impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
53 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatureCollector<'a, 'tcx> {
56 lib_features: LibFeatures::new(),
60 fn extract(&self, attrs: &[Attribute]) -> Vec<(Symbol, Option<Symbol>, Span)> {
61 let stab_attrs = vec!["stable", "unstable", "rustc_const_unstable"];
62 let mut features = vec![];
65 // FIXME(varkor): the stability attribute might be behind a `#[cfg]` attribute.
67 // Find a stability attribute (i.e. `#[stable(..)]`, `#[unstable(..)]`,
68 // `#[rustc_const_unstable(..)]`).
69 if stab_attrs.iter().any(|stab_attr| attr.check_name(stab_attr)) {
70 let meta_item = attr.meta();
71 if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {
72 let mut feature = None;
75 if let Some(mi) = meta.meta_item() {
76 // Find the `feature = ".."` meta-item.
77 match (&*mi.name().as_str(), mi.value_str()) {
78 ("feature", val) => feature = val,
79 ("since", val) => since = val,
84 if let Some(feature) = feature {
85 features.push((feature, since, attr.span));
87 // We need to iterate over the other attributes, because
88 // `rustc_const_unstable` is not mutually exclusive with
89 // the other stability attributes, so we can't just `break`
98 fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
99 let already_in_stable = self.lib_features.stable.contains_key(&feature);
100 let already_in_unstable = self.lib_features.unstable.contains(&feature);
102 match (since, already_in_stable, already_in_unstable) {
103 (Some(since), _, false) => {
104 self.lib_features.stable.insert(feature, since);
106 (None, false, _) => {
107 self.lib_features.unstable.insert(feature);
109 (Some(_), _, true) | (None, true, _) => {
111 "feature `{}` is declared {}, but was previously declared {}",
113 if since.is_some() { "stable"} else { "unstable" },
114 if since.is_none() { "stable"} else { "unstable" },
116 self.tcx.sess.struct_span_err_with_code(span, &msg,
117 DiagnosticId::Error("E0711".into())).emit();
122 fn collect_from_attrs(&mut self, attrs: &[Attribute]) {
123 for (feature, stable, span) in self.extract(attrs) {
124 self.collect_feature(feature, stable, span);
129 impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LibFeatureCollector<'a, 'tcx> {
130 fn visit_item(&mut self, item: &hir::Item) {
131 self.collect_from_attrs(&item.attrs);
134 fn visit_trait_item(&mut self, trait_item: &hir::TraitItem) {
135 self.collect_from_attrs(&trait_item.attrs);
138 fn visit_impl_item(&mut self, impl_item: &hir::ImplItem) {
139 self.collect_from_attrs(&impl_item.attrs);
143 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatures {
144 let mut collector = LibFeatureCollector::new(tcx);
145 for &cnum in tcx.crates().iter() {
146 for &(feature, since) in tcx.defined_lib_features(cnum).iter() {
147 collector.collect_feature(feature, since, DUMMY_SP);
150 collector.collect_from_attrs(&tcx.hir.krate().attrs);
151 tcx.hir.krate().visit_all_item_likes(&mut collector);
152 collector.lib_features