Skip to content

Commit

Permalink
make some rustc_feature internals private, and ensure invariants with…
Browse files Browse the repository at this point in the history
… debug assertions
  • Loading branch information
RalfJung committed Oct 9, 2024
1 parent 3c374a1 commit 338ec91
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate)
if sess.opts.unstable_features.is_nightly_build() {
return;
}
if features.enabled_features.is_empty() {
if features.enabled_features().is_empty() {
return;
}
let mut errored = false;
Expand All @@ -621,7 +621,7 @@ fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate)
for ident in attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident()) {
let name = ident.name;
let stable_since = features
.enabled_lang_features
.enabled_lang_features()
.iter()
.flat_map(|&(feature, _, since)| if feature == name { since } else { None })
.next();
Expand All @@ -643,11 +643,11 @@ fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate)

fn check_incompatible_features(sess: &Session, features: &Features) {
let enabled_features = features
.enabled_lang_features
.enabled_lang_features()
.iter()
.copied()
.map(|(name, span, _)| (name, span))
.chain(features.enabled_lib_features.iter().copied());
.chain(features.enabled_lib_features().iter().copied());

for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
.iter()
Expand All @@ -674,7 +674,7 @@ fn check_new_solver_banned_features(sess: &Session, features: &Features) {

// Ban GCE with the new solver, because it does not implement GCE correctly.
if let Some(&(_, gce_span, _)) = features
.enabled_lang_features
.enabled_lang_features()
.iter()
.find(|&&(feat, _, _)| feat == sym::generic_const_exprs)
{
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
// If the enabled feature is stable, record it.
if let Some(f) = ACCEPTED_FEATURES.iter().find(|f| name == f.name) {
let since = Some(Symbol::intern(f.since));
features.set_enabled_lang_feature(name, mi.span(), since);
features.set_enabled_lang_feature(name, mi.span(), since, None);
continue;
}

Expand All @@ -103,7 +103,6 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -

// If the enabled feature is unstable, record it.
if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| name == f.feature.name) {
(f.set_enabled)(&mut features);
// When the ICE comes from core, alloc or std (approximation of the standard
// library), there's a chance that the person hitting the ICE may be using
// -Zbuild-std or similar with an untested target. The bug is probably in the
Expand All @@ -114,7 +113,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
{
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
}
features.set_enabled_lang_feature(name, mi.span(), None);
features.set_enabled_lang_feature(name, mi.span(), None, Some(f));
continue;
}

Expand Down
61 changes: 49 additions & 12 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{Feature, to_nonzero};

pub struct UnstableFeature {
pub feature: Feature,
pub set_enabled: fn(&mut Features),
set_enabled: fn(&mut Features),
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -54,11 +54,11 @@ macro_rules! declare_features {
#[derive(Clone, Default, Debug)]
pub struct Features {
/// `#![feature]` attrs for language features, for error reporting.
pub enabled_lang_features: Vec<(Symbol, Span, Option<Symbol>)>,
enabled_lang_features: Vec<(Symbol, Span, Option<Symbol>)>,
/// `#![feature]` attrs for non-language (library) features.
pub enabled_lib_features: Vec<(Symbol, Span)>,
enabled_lib_features: Vec<(Symbol, Span)>,
/// `enabled_lang_features` + `enabled_lib_features`.
pub enabled_features: FxHashSet<Symbol>,
enabled_features: FxHashSet<Symbol>,
/// State of individual features (unstable lang features only).
/// This is `true` if and only if the corresponding feature is listed in `enabled_lang_features`.
$(
Expand All @@ -70,17 +70,27 @@ macro_rules! declare_features {
impl Features {
pub fn set_enabled_lang_feature(
&mut self,
symbol: Symbol,
name: Symbol,
span: Span,
since: Option<Symbol>
since: Option<Symbol>,
feature: Option<&UnstableFeature>,
) {
self.enabled_lang_features.push((symbol, span, since));
self.enabled_features.insert(symbol);
self.enabled_lang_features.push((name, span, since));
self.enabled_features.insert(name);
if let Some(feature) = feature {
assert_eq!(feature.feature.name, name);
(feature.set_enabled)(self);
} else {
// Ensure we don't skip a `set_enabled` call.
debug_assert!(UNSTABLE_FEATURES.iter().find(|f| name == f.feature.name).is_none());
}
}

pub fn set_enabled_lib_feature(&mut self, symbol: Symbol, span: Span) {
self.enabled_lib_features.push((symbol, span));
self.enabled_features.insert(symbol);
pub fn set_enabled_lib_feature(&mut self, name: Symbol, span: Span) {
self.enabled_lib_features.push((name, span));
self.enabled_features.insert(name);
// Ensure we don't skip a `set_enabled` call.
debug_assert!(UNSTABLE_FEATURES.iter().find(|f| name == f.feature.name).is_none());
}

/// This is intended for hashing the set of enabled language features.
Expand All @@ -93,9 +103,36 @@ macro_rules! declare_features {
[$(self.$feature as u8),+]
}

pub fn enabled_lang_features(&self) -> &Vec<(Symbol, Span, Option<Symbol>)> {
&self.enabled_lang_features
}

pub fn enabled_lib_features(&self) -> &Vec<(Symbol, Span)> {
&self.enabled_lib_features
}

pub fn enabled_features(&self) -> &FxHashSet<Symbol> {
&self.enabled_features
}

/// Is the given feature enabled (via `#[feature(...)]`)?
pub fn enabled(&self, feature: Symbol) -> bool {
self.enabled_features.contains(&feature)
let e = self.enabled_features.contains(&feature);
if cfg!(debug_assertions) {
// Ensure this matches `self.$feature`, if that exists.
let e2 = match feature {
$( sym::$feature => Some(self.$feature), )*
_ => None,
};
if let Some(e2) = e2 {
assert_eq!(
e, e2,
"mismatch in feature state for `{feature}`: \
`enabled_features` says {e} but `self.{feature}` says {e2}"
);
}
}
e
}

/// Some features are known to be incomplete and using them is likely to have
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2288,10 +2288,10 @@ impl EarlyLintPass for IncompleteInternalFeatures {
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
let features = cx.builder.features();
features
.enabled_lang_features
.enabled_lang_features()
.iter()
.map(|(name, span, _)| (name, span))
.chain(features.enabled_lib_features.iter().map(|(name, span)| (name, span)))
.chain(features.enabled_lib_features().iter().map(|(name, span)| (name, span)))
.filter(|(&name, _)| features.incomplete(name) || features.internal(name))
.for_each(|(&name, &span)| {
if features.incomplete(name) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
tcx.hir().visit_all_item_likes_in_crate(&mut missing);
}

let enabled_lang_features = &tcx.features().enabled_lang_features;
let enabled_lang_features = tcx.features().enabled_lang_features();
let mut lang_features = UnordSet::default();
for &(feature, span, since) in enabled_lang_features {
if let Some(since) = since {
Expand All @@ -932,7 +932,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
}
}

let enabled_lib_features = &tcx.features().enabled_lib_features;
let enabled_lib_features = tcx.features().enabled_lib_features();
let mut remaining_lib_features = FxIndexMap::default();
for (feature, span) in enabled_lib_features {
if remaining_lib_features.contains_key(&feature) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
// Unfortunately we cannot exhaustively list fields here, since the
// struct is macro generated.
self.enabled_lang_features.hash_stable(hcx, hasher);
self.enabled_lib_features.hash_stable(hcx, hasher);
self.enabled_lang_features().hash_stable(hcx, hasher);
self.enabled_lib_features().hash_stable(hcx, hasher);

self.all_lang_features()[..].hash_stable(hcx, hasher);
for feature in rustc_feature::UNSTABLE_FEATURES.iter() {
Expand Down

0 comments on commit 338ec91

Please sign in to comment.