Skip to content

Commit 4ab1126

Browse files
committed
Introduce EnabledLibFeature
Instead of passing around 2-tuples of `(gate_name, attr_sp)`.
1 parent cc95e5e commit 4ab1126

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,11 @@ fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate)
649649
}
650650

651651
fn check_incompatible_features(sess: &Session, features: &Features) {
652-
let enabled_features = features
653-
.enabled_lang_features()
654-
.iter()
655-
.map(|feat| (feat.gate_name, feat.attr_sp))
656-
.chain(features.enabled_lib_features().iter().copied());
652+
let enabled_lang_features =
653+
features.enabled_lang_features().iter().map(|feat| (feat.gate_name, feat.attr_sp));
654+
let enabled_lib_features =
655+
features.enabled_lib_features().iter().map(|feat| (feat.gate_name, feat.attr_sp));
656+
let enabled_features = enabled_lang_features.chain(enabled_lib_features);
657657

658658
for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
659659
.iter()

compiler/rustc_expand/src/config.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_ast::{
1111
use rustc_attr as attr;
1212
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1313
use rustc_feature::{
14-
ACCEPTED_LANG_FEATURES, AttributeSafety, EnabledLangFeature, Features, LangFeatureStability,
15-
REMOVED_LANG_FEATURES, UNSTABLE_LANG_FEATURES,
14+
ACCEPTED_LANG_FEATURES, AttributeSafety, EnabledLangFeature, EnabledLibFeature, Features,
15+
LangFeatureStability, REMOVED_LANG_FEATURES, UNSTABLE_LANG_FEATURES,
1616
};
1717
use rustc_lint_defs::BuiltinLintDiag;
1818
use rustc_parse::validate_attr;
@@ -131,7 +131,8 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
131131

132132
// Otherwise, the feature is unknown. Enable it as a lib feature.
133133
// It will be checked later whether the feature really exists.
134-
features.set_enabled_lib_feature(name, mi.span());
134+
features
135+
.set_enabled_lib_feature(EnabledLibFeature { gate_name: name, attr_sp: mi.span() });
135136

136137
// Similar to above, detect internal lib features to suppress
137138
// the ICE message that asks for a report.

compiler/rustc_feature/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ pub use builtin_attrs::{
136136
};
137137
pub use removed::REMOVED_LANG_FEATURES;
138138
pub use unstable::{
139-
EnabledLangFeature, Features, INCOMPATIBLE_FEATURES, LangFeatureStability,
139+
EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES, LangFeatureStability,
140140
UNSTABLE_LANG_FEATURES,
141141
};

compiler/rustc_feature/src/unstable.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Features {
3838
/// `#![feature]` attrs for language features, for error reporting.
3939
enabled_lang_features: Vec<EnabledLangFeature>,
4040
/// `#![feature]` attrs for non-language (library) features.
41-
enabled_lib_features: Vec<(Symbol, Span)>,
41+
enabled_lib_features: Vec<EnabledLibFeature>,
4242
/// `enabled_lang_features` + `enabled_lib_features`.
4343
enabled_features: FxHashSet<Symbol>,
4444
}
@@ -63,17 +63,24 @@ pub enum LangFeatureStability {
6363
StableSince { version: Symbol },
6464
}
6565

66+
/// Information abhout an enabled library feature.
67+
#[derive(Debug, Copy, Clone)]
68+
pub struct EnabledLibFeature {
69+
pub gate_name: Symbol,
70+
pub attr_sp: Span,
71+
}
72+
6673
impl Features {
6774
/// `since` should be set for stable features that are nevertheless enabled with a `#[feature]`
6875
/// attribute, indicating since when they are stable.
69-
pub fn set_enabled_lang_feature(&mut self, feat: EnabledLangFeature) {
70-
self.enabled_lang_features.push(feat);
71-
self.enabled_features.insert(feat.gate_name);
76+
pub fn set_enabled_lang_feature(&mut self, lang_feat: EnabledLangFeature) {
77+
self.enabled_lang_features.push(lang_feat);
78+
self.enabled_features.insert(lang_feat.gate_name);
7279
}
7380

74-
pub fn set_enabled_lib_feature(&mut self, name: Symbol, span: Span) {
75-
self.enabled_lib_features.push((name, span));
76-
self.enabled_features.insert(name);
81+
pub fn set_enabled_lib_feature(&mut self, lib_feat: EnabledLibFeature) {
82+
self.enabled_lib_features.push(lib_feat);
83+
self.enabled_features.insert(lib_feat.gate_name);
7784
}
7885

7986
/// Returns a list of [`EnabledLangFeature`] with info about:
@@ -86,7 +93,7 @@ impl Features {
8693
&self.enabled_lang_features
8794
}
8895

89-
pub fn enabled_lib_features(&self) -> &Vec<(Symbol, Span)> {
96+
pub fn enabled_lib_features(&self) -> &Vec<EnabledLibFeature> {
9097
&self.enabled_lib_features
9198
}
9299

compiler/rustc_lint/src/builtin.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2287,11 +2287,13 @@ declare_lint_pass!(
22872287
impl EarlyLintPass for IncompleteInternalFeatures {
22882288
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
22892289
let features = cx.builder.features();
2290-
features
2291-
.enabled_lang_features()
2292-
.iter()
2293-
.map(|feat| (feat.gate_name, feat.attr_sp))
2294-
.chain(features.enabled_lib_features().into_iter().copied())
2290+
let lang_features =
2291+
features.enabled_lang_features().iter().map(|feat| (feat.gate_name, feat.attr_sp));
2292+
let lib_features =
2293+
features.enabled_lib_features().iter().map(|feat| (feat.gate_name, feat.attr_sp));
2294+
2295+
lang_features
2296+
.chain(lib_features)
22952297
.filter(|(name, _)| features.incomplete(*name) || features.internal(*name))
22962298
.for_each(|(name, span)| {
22972299
if features.incomplete(name) {

compiler/rustc_passes/src/stability.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use rustc_attr::{
1010
};
1111
use rustc_data_structures::fx::FxIndexMap;
1212
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
13-
use rustc_feature::{ACCEPTED_LANG_FEATURES, EnabledLangFeature, LangFeatureStability};
13+
use rustc_feature::{
14+
ACCEPTED_LANG_FEATURES, EnabledLangFeature, EnabledLibFeature, LangFeatureStability,
15+
};
1416
use rustc_hir as hir;
1517
use rustc_hir::def::{DefKind, Res};
1618
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
@@ -950,12 +952,12 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
950952

951953
let enabled_lib_features = tcx.features().enabled_lib_features();
952954
let mut remaining_lib_features = FxIndexMap::default();
953-
for (feature, span) in enabled_lib_features {
954-
if remaining_lib_features.contains_key(&feature) {
955+
for EnabledLibFeature { gate_name, attr_sp } in enabled_lib_features {
956+
if remaining_lib_features.contains_key(gate_name) {
955957
// Warn if the user enables a lib feature multiple times.
956-
tcx.dcx().emit_err(errors::DuplicateFeatureErr { span: *span, feature: *feature });
958+
tcx.dcx().emit_err(errors::DuplicateFeatureErr { span: *attr_sp, feature: *gate_name });
957959
}
958-
remaining_lib_features.insert(feature, *span);
960+
remaining_lib_features.insert(*gate_name, *attr_sp);
959961
}
960962
// `stdbuild` has special handling for `libc`, so we need to
961963
// recognise the feature when building std.
@@ -987,7 +989,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
987989
/// time, less loading from metadata is performed and thus compiler performance is improved.
988990
fn check_features<'tcx>(
989991
tcx: TyCtxt<'tcx>,
990-
remaining_lib_features: &mut FxIndexMap<&Symbol, Span>,
992+
remaining_lib_features: &mut FxIndexMap<Symbol, Span>,
991993
remaining_implications: &mut UnordMap<Symbol, Symbol>,
992994
defined_features: &LibFeatures,
993995
all_implications: &UnordMap<Symbol, Symbol>,
@@ -1057,7 +1059,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10571059
}
10581060

10591061
for (feature, span) in remaining_lib_features {
1060-
tcx.dcx().emit_err(errors::UnknownFeature { span, feature: *feature });
1062+
tcx.dcx().emit_err(errors::UnknownFeature { span, feature });
10611063
}
10621064

10631065
for (&implied_by, &feature) in remaining_implications.to_sorted_stable_ord() {

compiler/rustc_query_system/src/ich/impls_syntax.rs

+8
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::LangFeature
137137
}
138138
}
139139
}
140+
141+
impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::EnabledLibFeature {
142+
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
143+
let rustc_feature::EnabledLibFeature { gate_name, attr_sp } = self;
144+
gate_name.hash_stable(hcx, hasher);
145+
attr_sp.hash_stable(hcx, hasher);
146+
}
147+
}

0 commit comments

Comments
 (0)