Skip to content

Commit

Permalink
Auto merge of #131321 - RalfJung:feature-activation, r=<try>
Browse files Browse the repository at this point in the history
terminology: #[feature] *enables* a feature (instead of "declaring" or "activating" it)

Mostly, we currently call a feature that has a corresponding `#[feature(name)]` attribute in the current crate a "declared" feature. I think that is confusing as it does not align with what "declaring" usually means. Furthermore, we *also* refer to `#[stable]`/`#[unstable]` as *declaring* a feature (e.g. in [these diagnostics](https://github.com/rust-lang/rust/blob/f25e5abea229a6b6aa77b45e21cb784e785c6040/compiler/rustc_passes/messages.ftl#L297-L301)), which aligns better with what "declaring" usually means. To make things worse, the functions  `tcx.features().active(...)` and  `tcx.features().declared(...)` both exist and they are doing almost the same thing (testing whether a corresponding `#[feature(name)]`  exists) except that `active` would ICE if the feature is not an unstable lang feature. On top of this, the callback when a feature is activated/declared is called `set_enabled`, and many comments also talk about "enabling" a feature.

So really, our terminology is just a mess.

I would suggest we use "declaring a feature" for saying that something is/was guarded by a feature (e.g. `#[stable]`/`#[unstable]`), and "enabling a feature" for  `#[feature(name)]`. This PR implements that.
  • Loading branch information
bors committed Oct 9, 2024
2 parents 883f9a2 + 6c38ef0 commit ded5475
Show file tree
Hide file tree
Showing 121 changed files with 389 additions and 406 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
| asm::InlineAsmArch::RiscV64
| asm::InlineAsmArch::LoongArch64
);
if !is_stable && !self.tcx.features().asm_experimental_arch {
if !is_stable && !self.tcx.features().asm_experimental_arch() {
feature_err(
&self.tcx.sess,
sym::asm_experimental_arch,
Expand All @@ -65,7 +65,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
{
self.dcx().emit_err(AttSyntaxOnlyX86 { span: sp });
}
if asm.options.contains(InlineAsmOptions::MAY_UNWIND) && !self.tcx.features().asm_unwind {
if asm.options.contains(InlineAsmOptions::MAY_UNWIND) && !self.tcx.features().asm_unwind() {
feature_err(
&self.tcx.sess,
sym::asm_unwind,
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}
InlineAsmOperand::Label { block } => {
if !self.tcx.features().asm_goto {
if !self.tcx.features().asm_goto() {
feature_err(
sess,
sym::asm_goto,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
// Either `body.is_none()` or `is_never_pattern` here.
if !is_never_pattern {
if self.tcx.features().never_patterns {
if self.tcx.features().never_patterns() {
// If the feature is off we already emitted the error after parsing.
let suggestion = span.shrink_to_hi();
self.dcx().emit_err(MatchArmWithNoBody { span, suggestion });
Expand Down Expand Up @@ -716,7 +716,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
outer_hir_id: HirId,
inner_hir_id: HirId,
) {
if self.tcx.features().async_fn_track_caller
if self.tcx.features().async_fn_track_caller()
&& let Some(attrs) = self.attrs.get(&outer_hir_id.local_id)
&& attrs.into_iter().any(|attr| attr.has_name(sym::track_caller))
{
Expand Down Expand Up @@ -1571,7 +1571,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
}
Some(hir::CoroutineKind::Coroutine(_)) => {
if !self.tcx.features().coroutines {
if !self.tcx.features().coroutines() {
rustc_session::parse::feature_err(
&self.tcx.sess,
sym::coroutines,
Expand All @@ -1583,7 +1583,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
false
}
None => {
if !self.tcx.features().coroutines {
if !self.tcx.features().coroutines() {
rustc_session::parse::feature_err(
&self.tcx.sess,
sym::coroutines,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
continue;
}
let is_param = *is_param.get_or_insert_with(compute_is_param);
if !is_param && !self.tcx.features().more_maybe_bounds {
if !is_param && !self.tcx.features().more_maybe_bounds() {
self.tcx
.sess
.create_feature_err(
Expand All @@ -1530,7 +1530,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let host_param_parts = if let Const::Yes(span) = constness
// if this comes from implementing a `const` trait, we must force constness to be appended
// to the impl item, no matter whether effects is enabled.
&& (self.tcx.features().effects || force_append_constness)
&& (self.tcx.features().effects() || force_append_constness)
{
let span = self.lower_span(span);
let param_node_id = self.next_node_id();
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl_trait_defs: Vec::new(),
impl_trait_bounds: Vec::new(),
allow_try_trait: [sym::try_trait_v2, sym::yeet_desugar_details].into(),
allow_gen_future: if tcx.features().async_fn_track_caller {
allow_gen_future: if tcx.features().async_fn_track_caller() {
[sym::gen_future, sym::closure_track_caller].into()
} else {
[sym::gen_future].into()
Expand Down Expand Up @@ -1030,7 +1030,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span: data.inputs_span,
})
};
if !self.tcx.features().return_type_notation
if !self.tcx.features().return_type_notation()
&& self.tcx.sess.is_nightly_build()
{
add_feature_diagnostics(
Expand Down Expand Up @@ -1155,7 +1155,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
ast::GenericArg::Type(ty) => {
match &ty.kind {
TyKind::Infer if self.tcx.features().generic_arg_infer => {
TyKind::Infer if self.tcx.features().generic_arg_infer() => {
return GenericArg::Infer(hir::InferArg {
hir_id: self.lower_node_id(ty.id),
span: self.lower_span(ty.span),
Expand Down Expand Up @@ -1546,7 +1546,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl, .. } => {
if in_trait_or_impl.is_some()
|| self.tcx.features().lifetime_capture_rules_2024
|| self.tcx.features().lifetime_capture_rules_2024()
|| span.at_least_rust_2024()
{
// return-position impl trait in trait was decided to capture all
Expand Down Expand Up @@ -2315,7 +2315,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
match c.value.kind {
ExprKind::Underscore => {
if self.tcx.features().generic_arg_infer {
if self.tcx.features().generic_arg_infer() {
hir::ArrayLen::Infer(hir::InferArg {
hir_id: self.lower_node_id(c.id),
span: self.lower_span(c.value.span),
Expand Down Expand Up @@ -2497,7 +2497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
(BoundConstness::Never, BoundPolarity::Positive) => hir::TraitBoundModifier::None,
(_, BoundPolarity::Maybe(_)) => hir::TraitBoundModifier::Maybe,
(BoundConstness::Never, BoundPolarity::Negative(_)) => {
if self.tcx.features().negative_bounds {
if self.tcx.features().negative_bounds() {
hir::TraitBoundModifier::Negative
} else {
hir::TraitBoundModifier::None
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span: data.inputs_span,
})
};
if !self.tcx.features().return_type_notation
if !self.tcx.features().return_type_notation()
&& self.tcx.sess.is_nightly_build()
{
add_feature_diagnostics(
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
// ```
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
if self.tcx.features().impl_trait_in_fn_trait_return {
if self.tcx.features().impl_trait_in_fn_trait_return() {
self.lower_ty(ty, itctx)
} else {
self.lower_ty(
Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ impl<'a> AstValidator<'a> {
return;
};

let make_impl_const_sugg = if self.features.const_trait_impl
let const_trait_impl = self.features.const_trait_impl();
let make_impl_const_sugg = if const_trait_impl
&& let TraitOrTraitImpl::TraitImpl {
constness: Const::No,
polarity: ImplPolarity::Positive,
Expand All @@ -345,13 +346,12 @@ impl<'a> AstValidator<'a> {
None
};

let make_trait_const_sugg = if self.features.const_trait_impl
&& let TraitOrTraitImpl::Trait { span, constness: None } = parent
{
Some(span.shrink_to_lo())
} else {
None
};
let make_trait_const_sugg =
if const_trait_impl && let TraitOrTraitImpl::Trait { span, constness: None } = parent {
Some(span.shrink_to_lo())
} else {
None
};

let parent_constness = parent.constness();
self.dcx().emit_err(errors::TraitFnConst {
Expand Down Expand Up @@ -1185,7 +1185,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
self.check_type_no_bounds(bounds, "this context");

if self.features.lazy_type_alias {
if self.features.lazy_type_alias() {
if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
self.dcx().emit_err(err);
}
Expand Down Expand Up @@ -1326,7 +1326,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
GenericBound::Trait(trait_ref, modifiers) => {
match (ctxt, modifiers.constness, modifiers.polarity) {
(BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_))
if !self.features.more_maybe_bounds =>
if !self.features.more_maybe_bounds() =>
{
self.sess
.create_feature_err(
Expand All @@ -1339,7 +1339,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
.emit();
}
(BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_))
if !self.features.more_maybe_bounds =>
if !self.features.more_maybe_bounds() =>
{
self.sess
.create_feature_err(
Expand Down
102 changes: 52 additions & 50 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use crate::errors;
/// The common case.
macro_rules! gate {
($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
feature_err(&$visitor.sess, sym::$feature, $span, $explain).emit();
}
}};
($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
// FIXME: make this translatable
#[allow(rustc::diagnostic_outside_of_impl)]
#[allow(rustc::untranslatable_diagnostic)]
Expand All @@ -43,7 +43,7 @@ macro_rules! gate_alt {
/// The case involving a multispan.
macro_rules! gate_multi {
($visitor:expr, $feature:ident, $spans:expr, $explain:expr) => {{
if !$visitor.features.$feature {
if !$visitor.features.$feature() {
let spans: Vec<_> =
$spans.filter(|span| !span.allows_unstable(sym::$feature)).collect();
if !spans.is_empty() {
Expand All @@ -56,7 +56,7 @@ macro_rules! gate_multi {
/// The legacy case.
macro_rules! gate_legacy {
($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
if !$visitor.features.$feature() && !$span.allows_unstable(sym::$feature) {
feature_warn(&$visitor.sess, sym::$feature, $span, $explain);
}
}};
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a> PostExpansionVisitor<'a> {

// FIXME(non_lifetime_binders): Const bound params are pretty broken.
// Let's keep users from using this feature accidentally.
if self.features.non_lifetime_binders {
if self.features.non_lifetime_binders() {
let const_param_spans: Vec<_> = params
.iter()
.filter_map(|param| match param.kind {
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

// Emit errors for non-staged-api crates.
if !self.features.staged_api {
if !self.features.staged_api() {
if attr.has_name(sym::unstable)
|| attr.has_name(sym::stable)
|| attr.has_name(sym::rustc_const_unstable)
Expand Down Expand Up @@ -470,7 +470,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
// Limit `min_specialization` to only specializing functions.
gate_alt!(
&self,
self.features.specialization || (is_fn && self.features.min_specialization),
self.features.specialization() || (is_fn && self.features.min_specialization()),
sym::specialization,
i.span,
"specialization is unstable"
Expand Down Expand Up @@ -548,7 +548,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(global_registration, "global registration is experimental");
gate_all!(return_type_notation, "return type notation is experimental");

if !visitor.features.never_patterns {
if !visitor.features.never_patterns() {
if let Some(spans) = spans.get(&sym::never_patterns) {
for &span in spans {
if span.allows_unstable(sym::never_patterns) {
Expand All @@ -572,7 +572,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
}
}

if !visitor.features.negative_bounds {
if !visitor.features.negative_bounds() {
for &span in spans.get(&sym::negative_bounds).iter().copied().flatten() {
sess.dcx().emit_err(errors::NegativeBoundUnsupported { span });
}
Expand Down Expand Up @@ -600,59 +600,61 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
}

fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate) {
// checks if `#![feature]` has been used to enable any lang feature
// does not check the same for lib features unless there's at least one
// declared lang feature
if !sess.opts.unstable_features.is_nightly_build() {
if features.declared_features.is_empty() {
return;
}
for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
let mut err = errors::FeatureOnNonNightly {
span: attr.span,
channel: option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"),
stable_features: vec![],
sugg: None,
};

let mut all_stable = true;
for ident in
attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident())
{
let name = ident.name;
let stable_since = features
.declared_lang_features
.iter()
.flat_map(|&(feature, _, since)| if feature == name { since } else { None })
.next();
if let Some(since) = stable_since {
err.stable_features.push(errors::StableFeature { name, since });
} else {
all_stable = false;
}
}
if all_stable {
err.sugg = Some(attr.span);
// checks if `#![feature]` has been used to enable any feature.
if sess.opts.unstable_features.is_nightly_build() {
return;
}
if features.enabled_features().is_empty() {
return;
}
let mut errored = false;
for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
// `feature(...)` used on non-nightly. This is definitely an error.
let mut err = errors::FeatureOnNonNightly {
span: attr.span,
channel: option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"),
stable_features: vec![],
sugg: None,
};

let mut all_stable = true;
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()
.iter()
.flat_map(|&(feature, _, since)| if feature == name { since } else { None })
.next();
if let Some(since) = stable_since {
err.stable_features.push(errors::StableFeature { name, since });
} else {
all_stable = false;
}
sess.dcx().emit_err(err);
}
if all_stable {
err.sugg = Some(attr.span);
}
sess.dcx().emit_err(err);
errored = true;
}
// Just make sure we actually error if anything is listed in `enabled_features`.
assert!(errored);
}

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

for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
.iter()
.filter(|&&(f1, f2)| features.active(f1) && features.active(f2))
.filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
{
if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
if let Some((f1_name, f1_span)) = enabled_features.clone().find(|(name, _)| name == f1) {
if let Some((f2_name, f2_span)) = enabled_features.clone().find(|(name, _)| name == f2)
{
let spans = vec![f1_span, f2_span];
sess.dcx().emit_err(errors::IncompatibleFeatures {
Expand All @@ -672,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
.declared_lang_features
.enabled_lang_features()
.iter()
.find(|&&(feat, _, _)| feat == sym::generic_const_exprs)
{
Expand Down
Loading

0 comments on commit ded5475

Please sign in to comment.