1
Fork 0

Store feature stability un-split

This commit is contained in:
Michael Goulet 2023-11-17 23:40:04 +00:00
parent 86299a1247
commit 2d187d54fd
5 changed files with 37 additions and 55 deletions

View file

@ -86,26 +86,21 @@ impl<'tcx> LibFeatureCollector<'tcx> {
}
fn collect_feature(&mut self, feature: Symbol, stability: FeatureStability, span: Span) {
let already_in_stable = self.lib_features.stable.contains_key(&feature);
let already_in_unstable = self.lib_features.unstable.contains_key(&feature);
let existing_stability = self.lib_features.stability.get(&feature).cloned();
match (stability, already_in_stable, already_in_unstable) {
(FeatureStability::AcceptedSince(since), _, false) => {
if let Some((prev_since, _)) = self.lib_features.stable.get(&feature)
&& *prev_since != since
{
self.tcx.sess.emit_err(FeatureStableTwice {
span,
feature,
since,
prev_since: *prev_since,
});
return;
}
self.lib_features.stable.insert(feature, (since, span));
match (stability, existing_stability) {
(_, None) => {
self.lib_features.stability.insert(feature, (stability, span));
}
(FeatureStability::AcceptedSince(_), _, true) => {
(
FeatureStability::AcceptedSince(since),
Some((FeatureStability::AcceptedSince(prev_since), _)),
) => {
if prev_since != since {
self.tcx.sess.emit_err(FeatureStableTwice { span, feature, since, prev_since });
}
}
(FeatureStability::AcceptedSince(_), Some((FeatureStability::Unstable, _))) => {
self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
span,
feature,
@ -113,10 +108,7 @@ impl<'tcx> LibFeatureCollector<'tcx> {
prev_declared: "unstable",
});
}
(FeatureStability::Unstable, false, _) => {
self.lib_features.unstable.insert(feature, span);
}
(FeatureStability::Unstable, true, _) => {
(FeatureStability::Unstable, Some((FeatureStability::AcceptedSince(_), _))) => {
self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
span,
feature,
@ -124,6 +116,8 @@ impl<'tcx> LibFeatureCollector<'tcx> {
prev_declared: "stable",
});
}
// duplicate `unstable` feature is ok.
(FeatureStability::Unstable, Some((FeatureStability::Unstable, _))) => {}
}
}
}

View file

@ -9,7 +9,7 @@ use rustc_attr::{
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, LocalModDefId, CRATE_DEF_ID};
use rustc_hir::def_id::{LocalDefId, LocalModDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::hir_id::CRATE_HIR_ID;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant};
@ -1008,12 +1008,11 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
}
// All local crate implications need to have the feature that implies it confirmed to exist.
let mut remaining_implications =
tcx.stability_implications(rustc_hir::def_id::LOCAL_CRATE).clone();
let mut remaining_implications = tcx.stability_implications(LOCAL_CRATE).clone();
// We always collect the lib features declared in the current crate, even if there are
// no unknown features, because the collection also does feature attribute validation.
let local_defined_features = tcx.lib_features(rustc_hir::def_id::LOCAL_CRATE);
let local_defined_features = tcx.lib_features(LOCAL_CRATE);
if !remaining_lib_features.is_empty() || !remaining_implications.is_empty() {
// Loading the implications of all crates is unavoidable to be able to emit the partial
// stabilization diagnostic, but it can be avoided when there are no
@ -1050,13 +1049,12 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
}
for (implied_by, feature) in remaining_implications {
let local_defined_features = tcx.lib_features(rustc_hir::def_id::LOCAL_CRATE);
let span = *local_defined_features
.stable
let local_defined_features = tcx.lib_features(LOCAL_CRATE);
let span = local_defined_features
.stability
.get(&feature)
.map(|(_, span)| span)
.or_else(|| local_defined_features.unstable.get(&feature))
.expect("feature that implied another does not exist");
.expect("feature that implied another does not exist")
.1;
tcx.sess.emit_err(errors::ImpliedFeatureNotExist { span, feature, implied_by });
}