2018-11-26 20:59:49 -06:00
|
|
|
// Detecting lib features (i.e., features that are not lang features).
|
2018-07-22 19:20:33 -05:00
|
|
|
//
|
2018-11-26 20:59:49 -06:00
|
|
|
// These are declared using stability attributes (e.g., `#[stable (..)]`
|
2018-07-22 20:20:50 -05:00
|
|
|
// and `#[unstable (..)]`), but are not declared in one single location
|
2018-07-22 19:20:33 -05:00
|
|
|
// (unlike lang features), which means we need to collect them instead.
|
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ty::TyCtxt;
|
2019-02-08 20:24:02 -06:00
|
|
|
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2018-07-22 19:20:33 -05:00
|
|
|
use syntax::symbol::Symbol;
|
|
|
|
use syntax::ast::{Attribute, MetaItem, MetaItemKind};
|
2019-05-09 18:57:08 -05:00
|
|
|
use syntax_pos::{Span, sym};
|
2018-07-22 19:20:33 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
|
2018-12-02 18:14:35 -06:00
|
|
|
use rustc_macros::HashStable;
|
2019-02-07 09:56:05 -06:00
|
|
|
use errors::DiagnosticId;
|
2018-07-22 19:20:33 -05:00
|
|
|
|
2018-12-02 18:14:35 -06:00
|
|
|
#[derive(HashStable)]
|
2018-07-22 19:20:33 -05:00
|
|
|
pub struct LibFeatures {
|
|
|
|
// A map from feature to stabilisation version.
|
|
|
|
pub stable: FxHashMap<Symbol, Symbol>,
|
|
|
|
pub unstable: FxHashSet<Symbol>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LibFeatures {
|
|
|
|
fn new() -> LibFeatures {
|
|
|
|
LibFeatures {
|
2018-10-16 09:57:53 -05:00
|
|
|
stable: Default::default(),
|
|
|
|
unstable: Default::default(),
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 18:06:57 -05:00
|
|
|
pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
|
|
|
|
let mut all_features: Vec<_> = self.stable.iter().map(|(f, s)| (*f, Some(*s)))
|
2018-07-22 19:20:33 -05:00
|
|
|
.chain(self.unstable.iter().map(|f| (*f, None)))
|
2018-07-26 18:06:57 -05:00
|
|
|
.collect();
|
|
|
|
all_features.sort_unstable_by_key(|f| f.0.as_str());
|
|
|
|
all_features
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LibFeatureCollector<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
lib_features: LibFeatures,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatureCollector<'a, 'tcx> {
|
|
|
|
LibFeatureCollector {
|
|
|
|
tcx,
|
|
|
|
lib_features: LibFeatures::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:34:56 -05:00
|
|
|
fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
|
2019-05-09 18:57:08 -05:00
|
|
|
let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable];
|
2018-08-15 11:34:56 -05:00
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
|
2018-08-15 11:34:56 -05:00
|
|
|
// `#[rustc_const_unstable (..)]`).
|
|
|
|
if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
|
2019-04-04 20:05:30 -05:00
|
|
|
attr.check_name(**stab_attr)
|
2018-08-15 11:34:56 -05:00
|
|
|
}) {
|
|
|
|
let meta_item = attr.meta();
|
|
|
|
if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {
|
|
|
|
let mut feature = None;
|
|
|
|
let mut since = None;
|
|
|
|
for meta in metas {
|
|
|
|
if let Some(mi) = meta.meta_item() {
|
|
|
|
// Find the `feature = ".."` meta-item.
|
2019-03-17 06:17:47 -05:00
|
|
|
match (mi.name_or_empty().get(), mi.value_str()) {
|
|
|
|
("feature", val) => feature = val,
|
|
|
|
("since", val) => since = val,
|
2018-08-15 11:34:56 -05:00
|
|
|
_ => {}
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 11:34:56 -05:00
|
|
|
}
|
|
|
|
if let Some(feature) = feature {
|
|
|
|
// This additional check for stability is to make sure we
|
|
|
|
// don't emit additional, irrelevant errors for malformed
|
|
|
|
// attributes.
|
|
|
|
if *stab_attr != "stable" || since.is_some() {
|
|
|
|
return Some((feature, since, attr.span));
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 11:34:56 -05:00
|
|
|
// We need to iterate over the other attributes, because
|
|
|
|
// `rustc_const_unstable` is not mutually exclusive with
|
|
|
|
// the other stability attributes, so we can't just `break`
|
|
|
|
// here.
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:34:56 -05:00
|
|
|
None
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) {
|
|
|
|
let already_in_stable = self.lib_features.stable.contains_key(&feature);
|
|
|
|
let already_in_unstable = self.lib_features.unstable.contains(&feature);
|
|
|
|
|
|
|
|
match (since, already_in_stable, already_in_unstable) {
|
|
|
|
(Some(since), _, false) => {
|
2018-07-22 19:21:03 -05:00
|
|
|
if let Some(prev_since) = self.lib_features.stable.get(&feature) {
|
|
|
|
if *prev_since != since {
|
|
|
|
let msg = format!(
|
|
|
|
"feature `{}` is declared stable since {}, \
|
|
|
|
but was previously declared stable since {}",
|
|
|
|
feature,
|
|
|
|
since,
|
|
|
|
prev_since,
|
|
|
|
);
|
|
|
|
self.tcx.sess.struct_span_err_with_code(span, &msg,
|
|
|
|
DiagnosticId::Error("E0711".into())).emit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 19:20:33 -05:00
|
|
|
self.lib_features.stable.insert(feature, since);
|
|
|
|
}
|
|
|
|
(None, false, _) => {
|
|
|
|
self.lib_features.unstable.insert(feature);
|
|
|
|
}
|
|
|
|
(Some(_), _, true) | (None, true, _) => {
|
|
|
|
let msg = format!(
|
|
|
|
"feature `{}` is declared {}, but was previously declared {}",
|
|
|
|
feature,
|
2018-10-02 11:29:48 -05:00
|
|
|
if since.is_some() { "stable" } else { "unstable" },
|
|
|
|
if since.is_none() { "stable" } else { "unstable" },
|
2018-07-22 19:20:33 -05:00
|
|
|
);
|
|
|
|
self.tcx.sess.struct_span_err_with_code(span, &msg,
|
|
|
|
DiagnosticId::Error("E0711".into())).emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:34:56 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for LibFeatureCollector<'a, 'tcx> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 06:45:36 -06:00
|
|
|
NestedVisitorMap::All(&self.tcx.hir())
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 11:34:56 -05:00
|
|
|
fn visit_attribute(&mut self, attr: &'tcx Attribute) {
|
|
|
|
if let Some((feature, stable, span)) = self.extract(attr) {
|
|
|
|
self.collect_feature(feature, stable, span);
|
|
|
|
}
|
2018-07-22 19:20:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatures {
|
|
|
|
let mut collector = LibFeatureCollector::new(tcx);
|
2018-12-04 06:45:36 -06:00
|
|
|
intravisit::walk_crate(&mut collector, tcx.hir().krate());
|
2018-07-22 19:20:33 -05:00
|
|
|
collector.lib_features
|
|
|
|
}
|