Skip to content

Commit

Permalink
Emit error when marking as unstable specifying an stable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
CastilloDel committed Oct 11, 2024
1 parent 363ae41 commit ec46505
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ passes_unrecognized_repr_hint =
unrecognized representation hint
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
passes_unstable_attr_for_already_stable_feature =
can't mark as unstable using an already stable feature
.label = this feature is already stable
.item = the stability attribute annotates this item
.help = Consider removing the attribute
passes_unused =
unused attribute
.suggestion = remove this attribute
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,17 @@ pub(crate) struct CannotStabilizeDeprecated {
pub item_sp: Span,
}

#[derive(Diagnostic)]
#[diag(passes_unstable_attr_for_already_stable_feature)]
pub(crate) struct UnstableAttrForAlreadyStableFeature {
#[primary_span]
#[label]
#[help]
pub span: Span,
#[label(passes_item)]
pub item_sp: Span,
}

#[derive(Diagnostic)]
#[diag(passes_missing_stability_attr)]
pub(crate) struct MissingStabilityAttr<'a> {
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_attr::{
};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
use rustc_feature::ACCEPTED_FEATURES;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
Expand Down Expand Up @@ -246,12 +247,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}
}

if let Stability { level: Unstable { .. }, feature } = stab {
if ACCEPTED_FEATURES.iter().find(|f| f.name == feature).is_some() {
self.tcx
.dcx()
.emit_err(errors::UnstableAttrForAlreadyStableFeature { span, item_sp });
}
}
if let Stability { level: Unstable { implied_by: Some(implied_by), .. }, feature } =
stab
{
self.index.implications.insert(implied_by, feature);
}

if let Some(ConstStability { level: Unstable { .. }, feature, .. }) = const_stab {
if ACCEPTED_FEATURES.iter().find(|f| f.name == feature).is_some() {
self.tcx.dcx().emit_err(errors::UnstableAttrForAlreadyStableFeature {
span: const_span.unwrap(), // If const_stab contains Some(..), same is true for const_span
item_sp,
});
}
}
if let Some(ConstStability {
level: Unstable { implied_by: Some(implied_by), .. },
feature,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//! Ensure #[unstable] doesn't accept already stable features
//@ check-fail

#![feature(staged_api)]
#![stable(feature = "rust_test", since = "1.0.0")]

#[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
#[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
const fn my_fun() {}

fn main() {}

0 comments on commit ec46505

Please sign in to comment.