Skip to content

Commit 4f38864

Browse files
committed
Check for already stable features in check_attr.
1 parent 408d816 commit 4f38864

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
@@ -11,11 +11,17 @@ use std::slice;
1111

1212
use rustc_abi::{Align, ExternAbi, Size};
1313
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast, join_path_syms};
14-
use rustc_attr_data_structures::{AttributeKind, InlineAttr, ReprAttr, find_attr};
14+
use rustc_attr_data_structures::{
15+
AttributeKind, InlineAttr, PartialConstStability, ReprAttr, Stability, StabilityLevel,
16+
find_attr,
17+
};
1518
use rustc_attr_parsing::{AttributeParser, Late};
1619
use rustc_data_structures::fx::FxHashMap;
1720
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
18-
use rustc_feature::{AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
21+
use rustc_feature::{
22+
ACCEPTED_LANG_FEATURES, AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP,
23+
BuiltinAttribute,
24+
};
1925
use rustc_hir::def::DefKind;
2026
use rustc_hir::def_id::LocalModDefId;
2127
use rustc_hir::intravisit::{self, Visitor};
@@ -165,9 +171,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
165171
Target::Impl { of_trait: true },
166172
),
167173
Attribute::Parsed(
168-
AttributeKind::Stability { span, .. }
169-
| AttributeKind::ConstStability { span, .. },
170-
) => self.check_stability_promotable(*span, target),
174+
AttributeKind::Stability {
175+
span: attr_span,
176+
stability: Stability { level, feature },
177+
}
178+
| AttributeKind::ConstStability {
179+
span: attr_span,
180+
stability: PartialConstStability { level, feature, .. },
181+
},
182+
) => self.check_stability(*attr_span, span, level, *feature, target),
171183
Attribute::Parsed(AttributeKind::Inline(InlineAttr::Force { .. }, ..)) => {} // handled separately below
172184
Attribute::Parsed(AttributeKind::Inline(kind, attr_span)) => {
173185
self.check_inline(hir_id, *attr_span, span, kind, target)
@@ -2319,13 +2331,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23192331
}
23202332
}
23212333

2322-
fn check_stability_promotable(&self, span: Span, target: Target) {
2334+
fn check_stability(
2335+
&self,
2336+
attr_span: Span,
2337+
item_span: Span,
2338+
level: &StabilityLevel,
2339+
feature: Symbol,
2340+
target: Target,
2341+
) {
23232342
match target {
23242343
Target::Expression => {
2325-
self.dcx().emit_err(errors::StabilityPromotable { attr_span: span });
2344+
self.dcx().emit_err(errors::StabilityPromotable { attr_span });
23262345
}
23272346
_ => {}
23282347
}
2348+
2349+
// Stable *language* features shouldn't be used as unstable library features.
2350+
// (Not doing this for stable library features is checked by tidy.)
2351+
if level.is_unstable()
2352+
&& ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
2353+
{
2354+
self.tcx
2355+
.dcx()
2356+
.emit_err(errors::UnstableAttrForAlreadyStableFeature { attr_span, item_span });
2357+
}
23292358
}
23302359

23312360
fn check_link_ordinal(&self, attr_span: Span, _span: Span, target: Target) {

compiler/rustc_passes/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,9 @@ pub(crate) struct UnstableAttrForAlreadyStableFeature {
13731373
#[primary_span]
13741374
#[label]
13751375
#[help]
1376-
pub span: Span,
1376+
pub attr_span: Span,
13771377
#[label(passes_item)]
1378-
pub item_sp: Span,
1378+
pub item_span: Span,
13791379
}
13801380

13811381
#[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)