rustc_passes/
lib_features.rs1use rustc_attr_ir::{Attribute, AttributeKind, StabilityLevel, StableSince};
8use rustc_hir::intravisit::Visitor;
9use rustc_middle::hir::nested_filter;
10use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures};
11use rustc_middle::query::{LocalCrate, Providers};
12use rustc_middle::ty::TyCtxt;
13use rustc_span::{Span, Symbol, sym};
14
15use crate::diagnostics::{FeaturePreviouslyDeclared, FeatureStableTwice};
16
17struct LibFeatureCollector<'tcx> {
18 tcx: TyCtxt<'tcx>,
19 lib_features: LibFeatures,
20}
21
22impl<'tcx> LibFeatureCollector<'tcx> {
23 fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
24 LibFeatureCollector { tcx, lib_features: LibFeatures::default() }
25 }
26
27 fn extract(&self, attr: &Attribute) -> Option<(Symbol, FeatureStability, Span)> {
28 let (feature, level, span) = match attr {
29 Attribute::Parsed(AttributeKind::Stability { stability, span }) => {
30 (stability.feature, stability.level, *span)
31 }
32 Attribute::Parsed(AttributeKind::RustcConstStability { stability, span }) => {
33 (stability.feature, stability.level, *span)
34 }
35 Attribute::Parsed(AttributeKind::RustcBodyStability { stability, span }) => {
36 (stability.feature, stability.level, *span)
37 }
38 _ => return None,
39 };
40
41 let feature_stability = match level {
42 StabilityLevel::Unstable { old_name, .. } => FeatureStability::Unstable { old_name },
43 StabilityLevel::Stable { since, .. } => FeatureStability::AcceptedSince(match since {
44 StableSince::Version(v) => Symbol::intern(&v.to_string()),
45 StableSince::Current => sym::env_CFG_RELEASE,
46 StableSince::Err(_) => return None,
47 }),
48 };
49
50 Some((feature, feature_stability, span))
51 }
52
53 fn collect_feature(&mut self, feature: Symbol, stability: FeatureStability, span: Span) {
54 let existing_stability = self.lib_features.stability.get(&feature).cloned();
55
56 match (stability, existing_stability) {
57 (_, None) => {
58 self.lib_features.stability.insert(feature, (stability, span));
59 }
60 (
61 FeatureStability::AcceptedSince(since),
62 Some((FeatureStability::AcceptedSince(prev_since), _)),
63 ) => {
64 if prev_since != since {
65 self.tcx.dcx().emit_err(FeatureStableTwice {
66 span,
67 feature,
68 since,
69 prev_since,
70 });
71 }
72 }
73 (FeatureStability::AcceptedSince(_), Some((FeatureStability::Unstable { .. }, _))) => {
74 self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
75 span,
76 feature,
77 declared: "stable",
78 prev_declared: "unstable",
79 });
80 }
81 (FeatureStability::Unstable { .. }, Some((FeatureStability::AcceptedSince(_), _))) => {
82 self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
83 span,
84 feature,
85 declared: "unstable",
86 prev_declared: "stable",
87 });
88 }
89 (FeatureStability::Unstable { .. }, Some((FeatureStability::Unstable { .. }, _))) => {}
91 }
92 }
93}
94
95impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {
96 type NestedFilter = nested_filter::All;
97
98 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
99 self.tcx
100 }
101
102 fn visit_attribute(&mut self, attr: &'tcx Attribute) {
103 if let Some((feature, stable, span)) = self.extract(attr) {
104 self.collect_feature(feature, stable, span);
105 }
106 }
107}
108
109fn lib_features(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> LibFeatures {
110 if !tcx.features().staged_api() {
113 return LibFeatures::default();
114 }
115
116 let mut collector = LibFeatureCollector::new(tcx);
117 tcx.hir_walk_attributes(&mut collector);
118 collector.lib_features
119}
120
121pub(crate) fn provide(providers: &mut Providers) {
122 providers.lib_features = lib_features;
123}