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