]>
Commit | Line | Data |
---|---|---|
0731742a | 1 | // Detecting lib features (i.e., features that are not lang features). |
b7449926 | 2 | // |
0731742a | 3 | // These are declared using stability attributes (e.g., `#[stable (..)]` |
b7449926 XL |
4 | // and `#[unstable (..)]`), but are not declared in one single location |
5 | // (unlike lang features), which means we need to collect them instead. | |
6 | ||
3dfed10e | 7 | use rustc_ast::{Attribute, MetaItem, MetaItemKind}; |
dfeec247 XL |
8 | use rustc_errors::struct_span_err; |
9 | use rustc_hir::def_id::LOCAL_CRATE; | |
10 | use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; | |
ba9703b0 XL |
11 | use rustc_middle::hir::map::Map; |
12 | use rustc_middle::middle::lib_features::LibFeatures; | |
13 | use rustc_middle::ty::query::Providers; | |
14 | use rustc_middle::ty::TyCtxt; | |
dfeec247 XL |
15 | use rustc_span::symbol::Symbol; |
16 | use rustc_span::{sym, Span}; | |
60c5eb7d | 17 | |
dfeec247 XL |
18 | fn new_lib_features() -> LibFeatures { |
19 | LibFeatures { stable: Default::default(), unstable: Default::default() } | |
b7449926 XL |
20 | } |
21 | ||
dc9dc135 XL |
22 | pub struct LibFeatureCollector<'tcx> { |
23 | tcx: TyCtxt<'tcx>, | |
b7449926 XL |
24 | lib_features: LibFeatures, |
25 | } | |
26 | ||
dc9dc135 XL |
27 | impl LibFeatureCollector<'tcx> { |
28 | fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> { | |
dfeec247 | 29 | LibFeatureCollector { tcx, lib_features: new_lib_features() } |
b7449926 XL |
30 | } |
31 | ||
32 | fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> { | |
48663c56 | 33 | let stab_attrs = [sym::stable, sym::unstable, sym::rustc_const_unstable]; |
b7449926 | 34 | |
0731742a | 35 | // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`, |
b7449926 | 36 | // `#[rustc_const_unstable (..)]`). |
3dfed10e XL |
37 | if let Some(stab_attr) = |
38 | stab_attrs.iter().find(|stab_attr| self.tcx.sess.check_name(attr, **stab_attr)) | |
39 | { | |
b7449926 | 40 | let meta_item = attr.meta(); |
e74abb32 | 41 | if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item { |
b7449926 XL |
42 | let mut feature = None; |
43 | let mut since = None; | |
44 | for meta in metas { | |
45 | if let Some(mi) = meta.meta_item() { | |
46 | // Find the `feature = ".."` meta-item. | |
48663c56 XL |
47 | match (mi.name_or_empty(), mi.value_str()) { |
48 | (sym::feature, val) => feature = val, | |
49 | (sym::since, val) => since = val, | |
b7449926 XL |
50 | _ => {} |
51 | } | |
52 | } | |
53 | } | |
54 | if let Some(feature) = feature { | |
55 | // This additional check for stability is to make sure we | |
56 | // don't emit additional, irrelevant errors for malformed | |
57 | // attributes. | |
48663c56 | 58 | if *stab_attr != sym::stable || since.is_some() { |
b7449926 XL |
59 | return Some((feature, since, attr.span)); |
60 | } | |
61 | } | |
62 | // We need to iterate over the other attributes, because | |
63 | // `rustc_const_unstable` is not mutually exclusive with | |
64 | // the other stability attributes, so we can't just `break` | |
65 | // here. | |
66 | } | |
67 | } | |
68 | ||
69 | None | |
70 | } | |
71 | ||
72 | fn collect_feature(&mut self, feature: Symbol, since: Option<Symbol>, span: Span) { | |
73 | let already_in_stable = self.lib_features.stable.contains_key(&feature); | |
74 | let already_in_unstable = self.lib_features.unstable.contains(&feature); | |
75 | ||
76 | match (since, already_in_stable, already_in_unstable) { | |
77 | (Some(since), _, false) => { | |
78 | if let Some(prev_since) = self.lib_features.stable.get(&feature) { | |
79 | if *prev_since != since { | |
60c5eb7d XL |
80 | self.span_feature_error( |
81 | span, | |
82 | &format!( | |
83 | "feature `{}` is declared stable since {}, \ | |
84 | but was previously declared stable since {}", | |
dfeec247 | 85 | feature, since, prev_since, |
60c5eb7d | 86 | ), |
b7449926 | 87 | ); |
b7449926 XL |
88 | return; |
89 | } | |
90 | } | |
91 | ||
92 | self.lib_features.stable.insert(feature, since); | |
93 | } | |
94 | (None, false, _) => { | |
95 | self.lib_features.unstable.insert(feature); | |
96 | } | |
97 | (Some(_), _, true) | (None, true, _) => { | |
60c5eb7d XL |
98 | self.span_feature_error( |
99 | span, | |
100 | &format!( | |
101 | "feature `{}` is declared {}, but was previously declared {}", | |
102 | feature, | |
103 | if since.is_some() { "stable" } else { "unstable" }, | |
104 | if since.is_none() { "stable" } else { "unstable" }, | |
105 | ), | |
b7449926 | 106 | ); |
b7449926 XL |
107 | } |
108 | } | |
109 | } | |
60c5eb7d XL |
110 | |
111 | fn span_feature_error(&self, span: Span, msg: &str) { | |
6a06907d | 112 | struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit(); |
60c5eb7d | 113 | } |
b7449926 XL |
114 | } |
115 | ||
dc9dc135 | 116 | impl Visitor<'tcx> for LibFeatureCollector<'tcx> { |
dfeec247 XL |
117 | type Map = Map<'tcx>; |
118 | ||
ba9703b0 XL |
119 | fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> { |
120 | NestedVisitorMap::All(self.tcx.hir()) | |
b7449926 XL |
121 | } |
122 | ||
6a06907d | 123 | fn visit_attribute(&mut self, _: rustc_hir::HirId, attr: &'tcx Attribute) { |
b7449926 XL |
124 | if let Some((feature, stable, span)) = self.extract(attr) { |
125 | self.collect_feature(feature, stable, span); | |
126 | } | |
127 | } | |
128 | } | |
129 | ||
dfeec247 | 130 | fn collect(tcx: TyCtxt<'_>) -> LibFeatures { |
b7449926 | 131 | let mut collector = LibFeatureCollector::new(tcx); |
416331ca | 132 | let krate = tcx.hir().krate(); |
dfeec247 | 133 | for attr in krate.non_exported_macro_attrs { |
6a06907d | 134 | collector.visit_attribute(rustc_hir::CRATE_HIR_ID, attr); |
416331ca XL |
135 | } |
136 | intravisit::walk_crate(&mut collector, krate); | |
b7449926 XL |
137 | collector.lib_features |
138 | } | |
dfeec247 | 139 | |
f035d41b | 140 | pub fn provide(providers: &mut Providers) { |
dfeec247 XL |
141 | providers.get_lib_features = |tcx, id| { |
142 | assert_eq!(id, LOCAL_CRATE); | |
f9f354fc | 143 | collect(tcx) |
dfeec247 XL |
144 | }; |
145 | } |