2022-07-13 09:09:37 +00:00
|
|
|
//! Detecting lib features (i.e., features that are not lang features).
|
|
|
|
//!
|
|
|
|
//! These are declared using stability attributes (e.g., `#[stable (..)]` and `#[unstable (..)]`),
|
|
|
|
//! but are not declared in one single location (unlike lang features), which means we need to
|
|
|
|
//! collect them instead.
|
2018-07-23 00:20:33 +00:00
|
|
|
|
2025-02-09 21:49:33 +00:00
|
|
|
use rustc_attr_parsing::{AttributeKind, StabilityLevel, StableSince};
|
2024-10-16 23:14:01 +00:00
|
|
|
use rustc_hir::Attribute;
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
|
|
|
use rustc_middle::hir::nested_filter;
|
2023-09-23 05:38:49 +00:00
|
|
|
use rustc_middle::middle::lib_features::{FeatureStability, LibFeatures};
|
|
|
|
use rustc_middle::query::{LocalCrate, Providers};
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{Span, Symbol, sym};
|
2019-11-11 22:34:57 +00:00
|
|
|
|
2022-09-24 18:21:58 +00:00
|
|
|
use crate::errors::{FeaturePreviouslyDeclared, FeatureStableTwice};
|
|
|
|
|
2024-08-29 05:17:14 +00:00
|
|
|
struct LibFeatureCollector<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-07-23 00:20:33 +00:00
|
|
|
lib_features: LibFeatures,
|
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> LibFeatureCollector<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
|
2023-09-23 05:38:49 +00:00
|
|
|
LibFeatureCollector { tcx, lib_features: LibFeatures::default() }
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 05:38:49 +00:00
|
|
|
fn extract(&self, attr: &Attribute) -> Option<(Symbol, FeatureStability, Span)> {
|
2025-02-09 21:49:33 +00:00
|
|
|
let (feature, level, span) = match attr {
|
|
|
|
Attribute::Parsed(AttributeKind::Stability { stability, span }) => {
|
|
|
|
(stability.feature, stability.level, *span)
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
2025-02-09 21:49:33 +00:00
|
|
|
Attribute::Parsed(AttributeKind::ConstStability { stability, span }) => {
|
|
|
|
(stability.feature, stability.level, *span)
|
|
|
|
}
|
|
|
|
Attribute::Parsed(AttributeKind::BodyStability { stability, span }) => {
|
|
|
|
(stability.feature, stability.level, *span)
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let feature_stability = match level {
|
|
|
|
StabilityLevel::Unstable { .. } => FeatureStability::Unstable,
|
|
|
|
StabilityLevel::Stable { since, .. } => FeatureStability::AcceptedSince(match since {
|
|
|
|
StableSince::Version(v) => Symbol::intern(&v.to_string()),
|
|
|
|
StableSince::Current => sym::env_CFG_RELEASE,
|
|
|
|
StableSince::Err => return None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some((feature, feature_stability, span))
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 05:38:49 +00:00
|
|
|
fn collect_feature(&mut self, feature: Symbol, stability: FeatureStability, span: Span) {
|
2023-11-17 23:40:04 +00:00
|
|
|
let existing_stability = self.lib_features.stability.get(&feature).cloned();
|
2018-07-23 00:20:33 +00:00
|
|
|
|
2023-11-17 23:40:04 +00:00
|
|
|
match (stability, existing_stability) {
|
|
|
|
(_, None) => {
|
|
|
|
self.lib_features.stability.insert(feature, (stability, span));
|
|
|
|
}
|
|
|
|
(
|
|
|
|
FeatureStability::AcceptedSince(since),
|
|
|
|
Some((FeatureStability::AcceptedSince(prev_since), _)),
|
|
|
|
) => {
|
|
|
|
if prev_since != since {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_err(FeatureStableTwice {
|
|
|
|
span,
|
|
|
|
feature,
|
|
|
|
since,
|
|
|
|
prev_since,
|
|
|
|
});
|
2018-07-23 00:21:03 +00:00
|
|
|
}
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
2023-11-17 23:40:04 +00:00
|
|
|
(FeatureStability::AcceptedSince(_), Some((FeatureStability::Unstable, _))) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
|
2023-09-23 05:38:49 +00:00
|
|
|
span,
|
|
|
|
feature,
|
|
|
|
declared: "stable",
|
|
|
|
prev_declared: "unstable",
|
|
|
|
});
|
|
|
|
}
|
2023-11-17 23:40:04 +00:00
|
|
|
(FeatureStability::Unstable, Some((FeatureStability::AcceptedSince(_), _))) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_err(FeaturePreviouslyDeclared {
|
2019-11-11 22:34:57 +00:00
|
|
|
span,
|
2022-09-24 18:21:58 +00:00
|
|
|
feature,
|
2023-09-23 05:38:49 +00:00
|
|
|
declared: "unstable",
|
|
|
|
prev_declared: "stable",
|
2022-09-24 18:21:58 +00:00
|
|
|
});
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
2023-11-17 23:40:04 +00:00
|
|
|
// duplicate `unstable` feature is ok.
|
|
|
|
(FeatureStability::Unstable, Some((FeatureStability::Unstable, _))) => {}
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> {
|
2021-11-03 23:03:12 +00:00
|
|
|
type NestedFilter = nested_filter::All;
|
2020-01-07 16:25:33 +00:00
|
|
|
|
2025-02-03 03:42:01 +00:00
|
|
|
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
|
|
|
|
self.tcx
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 21:54:03 +00:00
|
|
|
fn visit_attribute(&mut self, attr: &'tcx Attribute) {
|
2018-08-15 16:34:56 +00:00
|
|
|
if let Some((feature, stable, span)) = self.extract(attr) {
|
|
|
|
self.collect_feature(feature, stable, span);
|
|
|
|
}
|
2018-07-23 00:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 05:38:49 +00:00
|
|
|
fn lib_features(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> LibFeatures {
|
2023-01-09 10:21:31 +00:00
|
|
|
// If `staged_api` is not enabled then we aren't allowed to define lib
|
|
|
|
// features; there is no point collecting them.
|
2024-10-09 07:01:57 +00:00
|
|
|
if !tcx.features().staged_api() {
|
2023-09-23 05:38:49 +00:00
|
|
|
return LibFeatures::default();
|
2023-01-09 10:21:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 00:20:33 +00:00
|
|
|
let mut collector = LibFeatureCollector::new(tcx);
|
2025-02-17 03:17:57 +00:00
|
|
|
tcx.hir_walk_attributes(&mut collector);
|
2018-07-23 00:20:33 +00:00
|
|
|
collector.lib_features
|
|
|
|
}
|
2019-12-27 17:52:36 +00:00
|
|
|
|
2024-08-29 05:17:14 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2021-05-20 18:57:09 +00:00
|
|
|
providers.lib_features = lib_features;
|
2019-12-27 17:52:36 +00:00
|
|
|
}
|