Skip to content

Commit 497100a

Browse files
committed
Emit an error for unstable attributes that reference already stable features
Add missing error annotations and .stderr file Acknowledge comments
1 parent 363ae41 commit 497100a

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

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

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> {

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,10 @@
1+
//! Ensure #[unstable] doesn't accept already stable features
2+
3+
#![feature(staged_api)]
4+
#![stable(feature = "rust_test", since = "1.0.0")]
5+
6+
#[unstable(feature = "arbitrary_enum_discriminant", issue = "42")] //~ ERROR can't mark as unstable using an already stable feature
7+
#[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")] //~ ERROR can't mark as unstable using an already stable feature
8+
const fn my_fun() {}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: can't mark as unstable using an already stable feature
2+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
3+
|
4+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
6+
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
7+
LL | const fn my_fun() {}
8+
| -------------------- the stability attribute annotates this item
9+
|
10+
help: consider removing the attribute
11+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
12+
|
13+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: can't mark as unstable using an already stable feature
17+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
18+
|
19+
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
21+
LL | const fn my_fun() {}
22+
| -------------------- the stability attribute annotates this item
23+
|
24+
help: consider removing the attribute
25+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
26+
|
27+
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)