Skip to content

Commit 7d060b8

Browse files
committed
Auto merge of rust-lang#116550 - nnethercote:rustc-features-more, r=Nilstrieb
Cleanup `rustc_features` some more The sequel to rust-lang#116437. r? `@Nilstrieb`
2 parents 9ace9da + d284c8a commit 7d060b8

File tree

14 files changed

+254
-292
lines changed

14 files changed

+254
-292
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ fn check_incompatible_features(sess: &Session, features: &Features) {
658658

659659
for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
660660
.iter()
661-
.filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
661+
.filter(|&&(f1, f2)| features.active(f1) && features.active(f2))
662662
{
663663
if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
664664
if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
323323
let gate = match op.status_in_item(self.ccx) {
324324
Status::Allowed => return,
325325

326-
Status::Unstable(gate) if self.tcx.features().enabled(gate) => {
326+
Status::Unstable(gate) if self.tcx.features().active(gate) => {
327327
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
328328
&& !super::rustc_allow_const_fn_unstable(self.tcx, self.def_id(), gate);
329329
if unstable_in_stable {

compiler/rustc_expand/src/config.rs

+20-30
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem
1414
use rustc_attr as attr;
1515
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1616
use rustc_data_structures::fx::FxHashSet;
17-
use rustc_feature::{Feature, Features, State as FeatureState};
18-
use rustc_feature::{ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES};
17+
use rustc_feature::Features;
18+
use rustc_feature::{ACCEPTED_FEATURES, REMOVED_FEATURES, UNSTABLE_FEATURES};
1919
use rustc_parse::validate_attr;
2020
use rustc_session::parse::feature_err;
2121
use rustc_session::Session;
22-
use rustc_span::edition::{Edition, ALL_EDITIONS};
22+
use rustc_span::edition::ALL_EDITIONS;
2323
use rustc_span::symbol::{sym, Symbol};
2424
use rustc_span::Span;
2525
use thin_vec::ThinVec;
@@ -36,16 +36,6 @@ pub struct StripUnconfigured<'a> {
3636
}
3737

3838
pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
39-
fn active_features_up_to(edition: Edition) -> impl Iterator<Item = &'static Feature> {
40-
ACTIVE_FEATURES.iter().filter(move |feature| {
41-
if let Some(feature_edition) = feature.edition {
42-
feature_edition <= edition
43-
} else {
44-
false
45-
}
46-
})
47-
}
48-
4939
fn feature_list(attr: &Attribute) -> ThinVec<ast::NestedMetaItem> {
5040
if attr.has_name(sym::feature)
5141
&& let Some(list) = attr.meta_item_list()
@@ -83,11 +73,13 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
8373
// Enable edition-dependent features based on `features_edition`.
8474
// - E.g. enable `test_2018_feature` if `features_edition` is 2018 or higher
8575
let mut edition_enabled_features = FxHashSet::default();
86-
for feature in active_features_up_to(features_edition) {
87-
// FIXME(Manishearth) there is currently no way to set lib features by
88-
// edition.
89-
edition_enabled_features.insert(feature.name);
90-
feature.set(&mut features);
76+
for f in UNSTABLE_FEATURES {
77+
if let Some(edition) = f.feature.edition && edition <= features_edition {
78+
// FIXME(Manishearth) there is currently no way to set lib features by
79+
// edition.
80+
edition_enabled_features.insert(f.feature.name);
81+
(f.set_enabled)(&mut features);
82+
}
9183
}
9284

9385
// Process all features declared in the code.
@@ -147,19 +139,17 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
147139
}
148140

149141
// If the declared feature has been removed, issue an error.
150-
if let Some(Feature { state, .. }) = REMOVED_FEATURES.iter().find(|f| name == f.name) {
151-
if let FeatureState::Removed { reason } = state {
152-
sess.emit_err(FeatureRemoved {
153-
span: mi.span(),
154-
reason: reason.map(|reason| FeatureRemovedReason { reason }),
155-
});
156-
continue;
157-
}
142+
if let Some(f) = REMOVED_FEATURES.iter().find(|f| name == f.feature.name) {
143+
sess.emit_err(FeatureRemoved {
144+
span: mi.span(),
145+
reason: f.reason.map(|reason| FeatureRemovedReason { reason }),
146+
});
147+
continue;
158148
}
159149

160150
// If the declared feature is stable, record it.
161-
if let Some(Feature { since, .. }) = ACCEPTED_FEATURES.iter().find(|f| name == f.name) {
162-
let since = Some(Symbol::intern(since));
151+
if let Some(f) = ACCEPTED_FEATURES.iter().find(|f| name == f.name) {
152+
let since = Some(Symbol::intern(f.since));
163153
features.set_declared_lang_feature(name, mi.span(), since);
164154
continue;
165155
}
@@ -175,8 +165,8 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
175165
}
176166

177167
// If the declared feature is unstable, record it.
178-
if let Some(f) = ACTIVE_FEATURES.iter().find(|f| name == f.name) {
179-
f.set(&mut features);
168+
if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| name == f.feature.name) {
169+
(f.set_enabled)(&mut features);
180170
features.set_declared_lang_feature(name, mi.span(), None);
181171
continue;
182172
}

compiler/rustc_feature/src/accepted.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
//! List of the accepted feature gates.
22
3-
use super::{to_nonzero, Feature, State};
3+
use super::{to_nonzero, Feature};
44
use rustc_span::symbol::sym;
55

66
macro_rules! declare_features {
77
($(
88
$(#[doc = $doc:tt])* (accepted, $feature:ident, $ver:expr, $issue:expr, None),
99
)+) => {
10-
/// Those language feature has since been Accepted (it was once Active)
10+
/// Formerly unstable features that have now been accepted (stabilized).
1111
pub const ACCEPTED_FEATURES: &[Feature] = &[
12-
$(
13-
Feature {
14-
state: State::Accepted,
15-
name: sym::$feature,
16-
since: $ver,
17-
issue: to_nonzero($issue),
18-
edition: None,
19-
}
20-
),+
12+
$(Feature {
13+
name: sym::$feature,
14+
since: $ver,
15+
issue: to_nonzero($issue),
16+
edition: None,
17+
}),+
2118
];
2219
}
2320
}

compiler/rustc_feature/src/lib.rs

+13-33
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,18 @@
1616
#![deny(rustc::diagnostic_outside_of_impl)]
1717

1818
mod accepted;
19-
mod active;
2019
mod builtin_attrs;
2120
mod removed;
21+
mod unstable;
2222

2323
#[cfg(test)]
2424
mod tests;
2525

2626
use rustc_span::{edition::Edition, symbol::Symbol};
27-
use std::fmt;
2827
use std::num::NonZeroU32;
2928

30-
#[derive(Clone, Copy)]
31-
pub enum State {
32-
Accepted,
33-
Active { set: fn(&mut Features) },
34-
Removed { reason: Option<&'static str> },
35-
}
36-
37-
impl fmt::Debug for State {
38-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39-
match self {
40-
State::Accepted { .. } => write!(f, "accepted"),
41-
State::Active { .. } => write!(f, "active"),
42-
State::Removed { .. } => write!(f, "removed"),
43-
}
44-
}
45-
}
46-
4729
#[derive(Debug, Clone)]
4830
pub struct Feature {
49-
pub state: State,
5031
pub name: Symbol,
5132
pub since: &'static str,
5233
issue: Option<NonZeroU32>,
@@ -63,9 +44,9 @@ pub enum Stability {
6344

6445
#[derive(Clone, Copy, Debug, Hash)]
6546
pub enum UnstableFeatures {
66-
/// Hard errors for unstable features are active, as on beta/stable channels.
47+
/// Disallow use of unstable features, as on beta/stable channels.
6748
Disallow,
68-
/// Allow features to be activated, as on nightly.
49+
/// Allow use of unstable features, as on nightly.
6950
Allow,
7051
/// Errors are bypassed for bootstrapping. This is required any time
7152
/// during the build that feature-related lints are set to warn or above
@@ -106,17 +87,16 @@ impl UnstableFeatures {
10687

10788
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
10889
// Search in all the feature lists.
109-
let found = []
110-
.iter()
111-
.chain(ACTIVE_FEATURES)
112-
.chain(ACCEPTED_FEATURES)
113-
.chain(REMOVED_FEATURES)
114-
.find(|t| t.name == feature);
115-
116-
match found {
117-
Some(found) => found.issue,
118-
None => panic!("feature `{feature}` is not declared anywhere"),
90+
if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| f.feature.name == feature) {
91+
return f.feature.issue;
92+
}
93+
if let Some(f) = ACCEPTED_FEATURES.iter().find(|f| f.name == feature) {
94+
return f.issue;
95+
}
96+
if let Some(f) = REMOVED_FEATURES.iter().find(|f| f.feature.name == feature) {
97+
return f.feature.issue;
11998
}
99+
panic!("feature `{feature}` is not declared anywhere");
120100
}
121101

122102
const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
@@ -141,11 +121,11 @@ pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU3
141121
}
142122

143123
pub use accepted::ACCEPTED_FEATURES;
144-
pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
145124
pub use builtin_attrs::AttributeDuplicates;
146125
pub use builtin_attrs::{
147126
deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local,
148127
is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute,
149128
GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
150129
};
151130
pub use removed::REMOVED_FEATURES;
131+
pub use unstable::{Features, INCOMPATIBLE_FEATURES, UNSTABLE_FEATURES};

compiler/rustc_feature/src/removed.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
//! List of the removed feature gates.
22
3-
use super::{to_nonzero, Feature, State};
3+
use super::{to_nonzero, Feature};
44
use rustc_span::symbol::sym;
55

6+
pub struct RemovedFeature {
7+
pub feature: Feature,
8+
pub reason: Option<&'static str>,
9+
}
10+
611
macro_rules! declare_features {
712
($(
813
$(#[doc = $doc:tt])* (removed, $feature:ident, $ver:expr, $issue:expr, None, $reason:expr),
914
)+) => {
10-
/// Represents unstable features which have since been removed (it was once Active)
11-
pub const REMOVED_FEATURES: &[Feature] = &[
12-
$(
13-
Feature {
14-
state: State::Removed { reason: $reason },
15+
/// Formerly unstable features that have now been removed.
16+
pub const REMOVED_FEATURES: &[RemovedFeature] = &[
17+
$(RemovedFeature {
18+
feature: Feature {
1519
name: sym::$feature,
1620
since: $ver,
1721
issue: to_nonzero($issue),
1822
edition: None,
19-
}
20-
),+
23+
},
24+
reason: $reason
25+
}),+
2126
];
2227
};
2328
}

0 commit comments

Comments
 (0)