Skip to content

Commit ddb0c61

Browse files
authored
Rollup merge of rust-lang#131567 - CastilloDel:reject-unstable-with-accepted-features, r=jieyouxu
Emit an error for unstable attributes that reference already stable features Closes rust-lang#129814
2 parents e864a2c + 497100a commit ddb0c61

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

Diff for: compiler/rustc_passes/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,12 @@ passes_unrecognized_repr_hint =
739739
unrecognized representation hint
740740
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
741741
742+
passes_unstable_attr_for_already_stable_feature =
743+
can't mark as unstable using an already stable feature
744+
.label = this feature is already stable
745+
.item = the stability attribute annotates this item
746+
.help = consider removing the attribute
747+
742748
passes_unused =
743749
unused attribute
744750
.suggestion = remove this attribute

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

+11
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,17 @@ pub(crate) struct CannotStabilizeDeprecated {
14801480
pub item_sp: Span,
14811481
}
14821482

1483+
#[derive(Diagnostic)]
1484+
#[diag(passes_unstable_attr_for_already_stable_feature)]
1485+
pub(crate) struct UnstableAttrForAlreadyStableFeature {
1486+
#[primary_span]
1487+
#[label]
1488+
#[help]
1489+
pub span: Span,
1490+
#[label(passes_item)]
1491+
pub item_sp: Span,
1492+
}
1493+
14831494
#[derive(Diagnostic)]
14841495
#[diag(passes_missing_stability_attr)]
14851496
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,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)