Skip to content

Commit 7621f4e

Browse files
committed
Check for already stable features in check_attr.
1 parent 6211d49 commit 7621f4e

File tree

4 files changed

+51
-45
lines changed

4 files changed

+51
-45
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ use std::collections::hash_map::Entry;
1010

1111
use rustc_abi::{Align, ExternAbi, Size};
1212
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
13-
use rustc_attr_data_structures::{AttributeKind, InlineAttr, ReprAttr, find_attr};
13+
use rustc_attr_data_structures::{
14+
AttributeKind, InlineAttr, PartialConstStability, ReprAttr, Stability, StabilityLevel,
15+
find_attr,
16+
};
1417
use rustc_data_structures::fx::FxHashMap;
1518
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
16-
use rustc_feature::{AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
19+
use rustc_feature::{
20+
ACCEPTED_LANG_FEATURES, AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP,
21+
BuiltinAttribute,
22+
};
1723
use rustc_hir::def::DefKind;
1824
use rustc_hir::def_id::LocalModDefId;
1925
use rustc_hir::intravisit::{self, Visitor};
@@ -154,9 +160,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
154160
self.check_confusables(*first_span, target);
155161
}
156162
Attribute::Parsed(
157-
AttributeKind::Stability { span, .. }
158-
| AttributeKind::ConstStability { span, .. },
159-
) => self.check_stability_promotable(*span, target),
163+
AttributeKind::Stability {
164+
span: attr_span,
165+
stability: Stability { level, feature },
166+
}
167+
| AttributeKind::ConstStability {
168+
span: attr_span,
169+
stability: PartialConstStability { level, feature, .. },
170+
},
171+
) => self.check_stability(*attr_span, span, level, *feature, target),
160172
Attribute::Parsed(AttributeKind::Inline(InlineAttr::Force { .. }, ..)) => {} // handled separately below
161173
Attribute::Parsed(AttributeKind::Inline(kind, attr_span)) => {
162174
self.check_inline(hir_id, *attr_span, span, kind, target)
@@ -2241,13 +2253,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22412253
}
22422254
}
22432255

2244-
fn check_stability_promotable(&self, span: Span, target: Target) {
2256+
fn check_stability(
2257+
&self,
2258+
attr_span: Span,
2259+
item_span: Span,
2260+
level: &StabilityLevel,
2261+
feature: Symbol,
2262+
target: Target,
2263+
) {
22452264
match target {
22462265
Target::Expression => {
2247-
self.dcx().emit_err(errors::StabilityPromotable { attr_span: span });
2266+
self.dcx().emit_err(errors::StabilityPromotable { attr_span });
22482267
}
22492268
_ => {}
22502269
}
2270+
2271+
// Stable *language* features shouldn't be used as unstable library features.
2272+
// (Not doing this for stable library features is checked by tidy.)
2273+
if level.is_unstable()
2274+
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
2275+
{
2276+
self.tcx
2277+
.dcx()
2278+
.emit_err(errors::UnstableAttrForAlreadyStableFeature { attr_span, item_span });
2279+
}
22512280
}
22522281

22532282
fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) {

compiler/rustc_passes/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,9 +1356,9 @@ pub(crate) struct UnstableAttrForAlreadyStableFeature {
13561356
#[primary_span]
13571357
#[label]
13581358
#[help]
1359-
pub span: Span,
1359+
pub attr_span: Span,
13601360
#[label(passes_item)]
1361-
pub item_sp: Span,
1361+
pub item_span: Span,
13621362
}
13631363

13641364
#[derive(Diagnostic)]

compiler/rustc_passes/src/stability.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_attr_data_structures::{
1111
};
1212
use rustc_data_structures::fx::FxIndexMap;
1313
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
14-
use rustc_feature::{ACCEPTED_LANG_FEATURES, EnabledLangFeature, EnabledLibFeature};
14+
use rustc_feature::{EnabledLangFeature, EnabledLibFeature};
1515
use rustc_hir::def::{DefKind, Res};
1616
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalModDefId};
1717
use rustc_hir::hir_id::CRATE_HIR_ID;
@@ -478,6 +478,7 @@ struct MissingStabilityAnnotations<'tcx> {
478478
}
479479

480480
impl<'tcx> MissingStabilityAnnotations<'tcx> {
481+
/// Verify that deprecation and stability attributes make sense with one another.
481482
#[instrument(level = "trace", skip(self))]
482483
fn check_compatible_stability(&self, def_id: LocalDefId, item_sp: Span) {
483484
if !self.tcx.features().staged_api() {
@@ -539,17 +540,6 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
539540
}
540541
}
541542
}
542-
543-
// Stable *language* features shouldn't be used as unstable library features.
544-
// (Not doing this for stable library features is checked by tidy.)
545-
if let Stability { level: StabilityLevel::Unstable { .. }, feature } = stab
546-
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
547-
&& let Some(span) = find_attr_span!(Stability)
548-
{
549-
self.tcx
550-
.dcx()
551-
.emit_err(errors::UnstableAttrForAlreadyStableFeature { span, item_sp });
552-
}
553543
}
554544

555545
// If the current node is a function with const stability attributes (directly given or
@@ -575,19 +565,6 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
575565
.emit_err(errors::ConstStableNotStable { fn_sig_span: fn_sig.span, const_span });
576566
}
577567

578-
// Stable *language* features shouldn't be used as unstable library features.
579-
// (Not doing this for stable library features is checked by tidy.)
580-
if let Some(ConstStability { level: StabilityLevel::Unstable { .. }, feature, .. }) =
581-
const_stab
582-
&& let Some(const_span) = find_attr_span!(ConstStability)
583-
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
584-
{
585-
self.tcx.dcx().emit_err(errors::UnstableAttrForAlreadyStableFeature {
586-
span: const_span,
587-
item_sp,
588-
});
589-
}
590-
591568
if let Some(stab) = &const_stab
592569
&& stab.is_const_stable()
593570
&& stab.const_stable_indirect
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: can't mark as unstable using an already stable feature
2-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
2+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
33
|
4-
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
64
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
76
LL | const fn my_fun() {}
87
| -------------------- the stability attribute annotates this item
98
|
109
help: consider removing the attribute
11-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
10+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
1211
|
13-
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1514

1615
error: can't mark as unstable using an already stable feature
17-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
16+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
1817
|
18+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
1920
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
2121
LL | const fn my_fun() {}
2222
| -------------------- the stability attribute annotates this item
2323
|
2424
help: consider removing the attribute
25-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
25+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
2626
|
27-
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929

3030
error: aborting due to 2 previous errors
3131

0 commit comments

Comments
 (0)