Skip to content

Commit ee15bd5

Browse files
committed
Emit an error for unstable attributes that reference already stable features
1 parent 363ae41 commit ee15bd5

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

Diff for: compiler/rustc_passes/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,12 @@ passes_unrecognized_repr_hint =
745745
unrecognized representation hint
746746
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
747747
748+
passes_unstable_attr_for_already_stable_feature =
749+
can't mark as unstable using an already stable feature
750+
.label = this feature is already stable
751+
.item = the stability attribute annotates this item
752+
.help = Consider removing the attribute
753+
748754
passes_unused =
749755
unused attribute
750756
.suggestion = remove this attribute

Diff for: compiler/rustc_passes/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,17 @@ pub(crate) struct CannotStabilizeDeprecated {
14961496
pub item_sp: Span,
14971497
}
14981498

1499+
#[derive(Diagnostic)]
1500+
#[diag(passes_unstable_attr_for_already_stable_feature)]
1501+
pub(crate) struct UnstableAttrForAlreadyStableFeature {
1502+
#[primary_span]
1503+
#[label]
1504+
#[help]
1505+
pub span: Span,
1506+
#[label(passes_item)]
1507+
pub item_sp: Span,
1508+
}
1509+
14991510
#[derive(Diagnostic)]
15001511
#[diag(passes_missing_stability_attr)]
15011512
pub(crate) struct MissingStabilityAttr<'a> {

Diff for: compiler/rustc_passes/src/stability.rs

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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_FEATURES;
1314
use rustc_hir as hir;
1415
use rustc_hir::def::{DefKind, Res};
1516
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
@@ -246,12 +247,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
246247
}
247248
}
248249

250+
if let Stability { level: Unstable { .. }, feature } = stab {
251+
if ACCEPTED_FEATURES.iter().find(|f| f.name == feature).is_some() {
252+
self.tcx
253+
.dcx()
254+
.emit_err(errors::UnstableAttrForAlreadyStableFeature { span, item_sp });
255+
}
256+
}
249257
if let Stability { level: Unstable { implied_by: Some(implied_by), .. }, feature } =
250258
stab
251259
{
252260
self.index.implications.insert(implied_by, feature);
253261
}
254262

263+
if let Some(ConstStability { level: Unstable { .. }, feature, .. }) = const_stab {
264+
if ACCEPTED_FEATURES.iter().find(|f| f.name == feature).is_some() {
265+
self.tcx.dcx().emit_err(errors::UnstableAttrForAlreadyStableFeature {
266+
span: const_span.unwrap(), // If const_stab contains Some(..), same is true for const_span
267+
item_sp,
268+
});
269+
}
270+
}
255271
if let Some(ConstStability {
256272
level: Unstable { implied_by: Some(implied_by), .. },
257273
feature,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Ensure #[unstable] doesn't accept already stable features
2+
//@ check-fail
3+
4+
#![feature(staged_api)]
5+
#![stable(feature = "rust_test", since = "1.0.0")]
6+
7+
#[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
8+
#[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
9+
const fn my_fun() {}
10+
11+
fn main() {}

0 commit comments

Comments
 (0)