2018-11-27 02:59:49 +00:00
|
|
|
// Detecting lib features (i.e., features that are not lang features).
|
2018-07-23 01:20:33 +01:00
|
|
|
//
|
2018-11-27 02:59:49 +00:00
|
|
|
// These are declared using stability attributes (e.g., `#[stable (..)]`
|
2018-07-23 02:20:50 +01:00
|
|
|
// and `#[unstable (..)]`), but are not declared in one single location
|
2018-07-23 01:20:33 +01:00
|
|
|
// (unlike lang features), which means we need to collect them instead.
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{Attribute, MetaItem, MetaItemKind};
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::struct_span_err;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::middle::lib_features::LibFeatures;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{sym, Span};
|
2019-11-11 23:34:57 +01:00
|
|
|
|
2019-12-27 18:52:36 +01:00
|
|
|
fn new_lib_features() -> LibFeatures {
|
|
|
|
LibFeatures { stable: Default::default(), unstable: Default::default() }
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
pub struct LibFeatureCollector<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-07-23 01:20:33 +01:00
|
|
|
lib_features: LibFeatures,
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl LibFeatureCollector<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
|
2019-12-27 18:52:36 +01:00
|
|
|
LibFeatureCollector { tcx, lib_features: new_lib_features() }
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 17:34:56 +01:00
|
|
|
fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
|
2019-05-10 09:57:08 +10:00
|
|
|
let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable];
|
2018-08-15 17:34:56 +01:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
|
2018-08-15 17:34:56 +01:00
|
|
|
// `#[rustc_const_unstable (..)]`).
|
2020-07-30 11:27:50 +10:00
|
|
|
if let Some(stab_attr) =
|
|
|
|
stab_attrs.iter().find(|stab_attr| self.tcx.sess.check_name(attr, **stab_attr))
|
|
|
|
{
|
2018-08-15 17:34:56 +01:00
|
|
|
let meta_item = attr.meta();
|
2019-09-26 18:04:05 +01:00
|
|
|
if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
|
2018-08-15 17:34:56 +01:00
|
|
|
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-05-08 14:33:06 +10:00
|
|
|
match (mi.name_or_empty(), mi.value_str()) {
|
|
|
|
(sym::feature, val) => feature = val,
|
|
|
|
(sym::since, val) => since = val,
|
2018-08-15 17:34:56 +01:00
|
|
|
_ => {}
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 17:34:56 +01: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.
|
2019-05-07 16:03:44 +10:00
|
|
|
if *stab_attr != sym::stable || since.is_some() {
|
2018-08-15 17:34:56 +01:00
|
|
|
return Some((feature, since, attr.span));
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 17:34:56 +01: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-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 17:34:56 +01:00
|
|
|
None
|
2018-07-23 01:20:33 +01: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-23 01:21:03 +01:00
|
|
|
if let Some(prev_since) = self.lib_features.stable.get(&feature) {
|
|
|
|
if *prev_since != since {
|
2019-11-11 23:34:57 +01:00
|
|
|
self.span_feature_error(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"feature `{}` is declared stable since {}, \
|
|
|
|
but was previously declared stable since {}",
|
2019-12-22 17:42:04 -05:00
|
|
|
feature, since, prev_since,
|
2019-11-11 23:34:57 +01:00
|
|
|
),
|
2018-07-23 01:21:03 +01:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 01:20:33 +01:00
|
|
|
self.lib_features.stable.insert(feature, since);
|
|
|
|
}
|
|
|
|
(None, false, _) => {
|
|
|
|
self.lib_features.unstable.insert(feature);
|
|
|
|
}
|
|
|
|
(Some(_), _, true) | (None, true, _) => {
|
2019-11-11 23:34:57 +01:00
|
|
|
self.span_feature_error(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"feature `{}` is declared {}, but was previously declared {}",
|
|
|
|
feature,
|
|
|
|
if since.is_some() { "stable" } else { "unstable" },
|
|
|
|
if since.is_none() { "stable" } else { "unstable" },
|
|
|
|
),
|
2018-07-23 01:20:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 23:34:57 +01:00
|
|
|
|
|
|
|
fn span_feature_error(&self, span: Span, msg: &str) {
|
2020-04-23 11:15:04 -07:00
|
|
|
struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
|
2019-11-11 23:34:57 +01:00
|
|
|
}
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
|
2020-01-07 17:25:33 +01:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2020-02-09 15:32:00 +01:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::All(self.tcx.hir())
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 17:40:19 +01:00
|
|
|
fn visit_attribute(&mut self, _: rustc_hir::HirId, attr: &'tcx Attribute) {
|
2018-08-15 17:34:56 +01:00
|
|
|
if let Some((feature, stable, span)) = self.extract(attr) {
|
|
|
|
self.collect_feature(feature, stable, span);
|
|
|
|
}
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 18:52:36 +01:00
|
|
|
fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
|
2018-07-23 01:20:33 +01:00
|
|
|
let mut collector = LibFeatureCollector::new(tcx);
|
2019-06-21 01:55:40 +03:00
|
|
|
let krate = tcx.hir().krate();
|
2019-11-28 11:49:29 +01:00
|
|
|
for attr in krate.non_exported_macro_attrs {
|
2020-12-05 17:40:19 +01:00
|
|
|
collector.visit_attribute(rustc_hir::CRATE_HIR_ID, attr);
|
2019-06-21 01:55:40 +03:00
|
|
|
}
|
|
|
|
intravisit::walk_crate(&mut collector, krate);
|
2018-07-23 01:20:33 +01:00
|
|
|
collector.lib_features
|
|
|
|
}
|
2019-12-27 18:52:36 +01:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-27 18:52:36 +01:00
|
|
|
providers.get_lib_features = |tcx, id| {
|
|
|
|
assert_eq!(id, LOCAL_CRATE);
|
2020-03-27 20:26:20 +01:00
|
|
|
collect(tcx)
|
2019-12-27 18:52:36 +01:00
|
|
|
};
|
|
|
|
}
|