Skip to content

Commit

Permalink
Store feature stability un-split
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 20, 2023
1 parent 86299a1 commit 2d187d5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
20 changes: 8 additions & 12 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,19 +1177,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

/// Iterates over all the stability attributes in the given crate.
fn get_lib_features(self, _tcx: TyCtxt<'tcx>) -> LibFeatures {
let mut features = LibFeatures::default();
for (symbol, stability) in self.root.lib_features.decode(self) {
match stability {
FeatureStability::AcceptedSince(since) => {
features.stable.insert(symbol, (since, DUMMY_SP));
}
FeatureStability::Unstable => {
features.unstable.insert(symbol, DUMMY_SP);
}
}
fn get_lib_features(self) -> LibFeatures {
LibFeatures {
stability: self
.root
.lib_features
.decode(self)
.map(|(sym, stab)| (sym, (stab, DUMMY_SP)))
.collect(),
}
features
}

/// Iterates over the stability implications in the given crate (when a `#[unstable]` attribute
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ provide! { tcx, def_id, other, cdata,
module_children => {
tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess))
}
lib_features => { cdata.get_lib_features(tcx) }
lib_features => { cdata.get_lib_features() }
stability_implications => {
cdata.get_stability_implications(tcx).iter().copied().collect()
}
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_middle/src/middle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ pub mod lib_features {

#[derive(HashStable, Debug, Default)]
pub struct LibFeatures {
/// A map from feature to stabilisation version.
pub stable: FxHashMap<Symbol, (Symbol, Span)>,
pub unstable: FxHashMap<Symbol, Span>,
pub stability: FxHashMap<Symbol, (FeatureStability, Span)>,
}

impl LibFeatures {
pub fn to_vec(&self) -> Vec<(Symbol, FeatureStability)> {
let mut all_features: Vec<_> = self
.stable
.iter()
.map(|(f, (s, _))| (*f, FeatureStability::AcceptedSince(*s)))
.chain(self.unstable.iter().map(|(f, _)| (*f, FeatureStability::Unstable)))
.collect();
all_features.sort_unstable_by(|a, b| a.0.as_str().partial_cmp(b.0.as_str()).unwrap());
let mut all_features: Vec<_> =
self.stability.iter().map(|(&sym, &(stab, _))| (sym, stab)).collect();
all_features.sort_unstable_by(|(a, _), (b, _)| a.as_str().cmp(b.as_str()));
all_features
}
}
Expand Down
36 changes: 15 additions & 21 deletions compiler/rustc_passes/src/lib_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,44 +86,38 @@ 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;
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 {
self.tcx.sess.emit_err(FeatureStableTwice { span, feature, since, prev_since });
}

self.lib_features.stable.insert(feature, (since, span));
}
(FeatureStability::AcceptedSince(_), _, true) => {
(FeatureStability::AcceptedSince(_), Some((FeatureStability::Unstable, _))) => {
self.tcx.sess.emit_err(FeaturePreviouslyDeclared {
span,
feature,
declared: "stable",
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,
declared: "unstable",
prev_declared: "stable",
});
}
// duplicate `unstable` feature is ok.
(FeatureStability::Unstable, Some((FeatureStability::Unstable, _))) => {}
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 });
}

Expand Down

0 comments on commit 2d187d5

Please sign in to comment.