Skip to content

Commit d79f825

Browse files
committed
Restrict the set of things that const stability can be applied
Remove the custom special case rejecting const stability on macros as it is now handled by the general check
1 parent 2c6db18 commit d79f825

File tree

6 files changed

+26
-36
lines changed

6 files changed

+26
-36
lines changed

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,20 @@ impl<S: Stage> AttributeParser<S> for ConstStabilityParser {
243243
this.promotable = true;
244244
}),
245245
];
246-
const ALLOWED_TARGETS: AllowedTargets = ALLOWED_TARGETS;
246+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
247+
Allow(Target::Fn),
248+
Allow(Target::Method(MethodKind::Inherent)),
249+
Allow(Target::Method(MethodKind::TraitImpl)),
250+
Allow(Target::Method(MethodKind::Trait { body: true })),
251+
Allow(Target::Impl { of_trait: false }),
252+
Allow(Target::Impl { of_trait: true }),
253+
Allow(Target::Use), // FIXME I don't think this does anything?
254+
Allow(Target::Const),
255+
Allow(Target::AssocConst),
256+
Allow(Target::Trait),
257+
Allow(Target::Static),
258+
Allow(Target::Crate),
259+
]);
247260

248261
fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
249262
if self.promotable {

compiler/rustc_expand/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ expand_macro_call_unused_doc_comment = unused doc comment
8787
.label = rustdoc does not generate documentation for macro invocations
8888
.help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion
8989
90-
expand_macro_const_stability =
91-
macros cannot have const stability attributes
92-
.label = invalid const stability attribute
93-
.label2 = const stability attribute affects this macro
94-
9590
expand_macro_expands_to_match_arm = macros cannot expand to match arms
9691
9792
expand_malformed_feature_attribute =

compiler/rustc_expand/src/base.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -979,12 +979,6 @@ impl SyntaxExtension {
979979
let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);
980980

981981
// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
982-
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) {
983-
sess.dcx().emit_err(errors::MacroConstStability {
984-
span: sp,
985-
head_span: sess.source_map().guess_head_span(span),
986-
});
987-
}
988982
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{ span, .. } => *span) {
989983
sess.dcx().emit_err(errors::MacroBodyStability {
990984
span: sp,

compiler/rustc_expand/src/errors.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ pub(crate) struct CollapseMacroDebuginfoIllegal {
8585
pub span: Span,
8686
}
8787

88-
#[derive(Diagnostic)]
89-
#[diag(expand_macro_const_stability)]
90-
pub(crate) struct MacroConstStability {
91-
#[primary_span]
92-
#[label]
93-
pub span: Span,
94-
#[label(expand_label2)]
95-
pub head_span: Span,
96-
}
97-
9888
#[derive(Diagnostic)]
9989
#[diag(expand_macro_body_stability)]
10090
pub(crate) struct MacroBodyStability {

tests/ui/attributes/const-stability-on-macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#![stable(feature = "rust1", since = "1.0.0")]
33

44
#[rustc_const_stable(feature = "foo", since = "3.3.3")]
5-
//~^ ERROR macros cannot have const stability attributes
5+
//~^ ERROR attribute cannot be used on macro defs
66
macro_rules! foo {
77
() => {};
88
}
99

10-
#[rustc_const_unstable(feature = "bar", issue="none")]
11-
//~^ ERROR macros cannot have const stability attributes
10+
#[rustc_const_unstable(feature = "bar", issue = "none")]
11+
//~^ ERROR attribute cannot be used on macro defs
1212
macro_rules! bar {
1313
() => {};
1414
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
error: macros cannot have const stability attributes
1+
error: `#[rustc_const_stable]` attribute cannot be used on macro defs
22
--> $DIR/const-stability-on-macro.rs:4:1
33
|
44
LL | #[rustc_const_stable(feature = "foo", since = "3.3.3")]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid const stability attribute
6-
LL |
7-
LL | macro_rules! foo {
8-
| ---------------- const stability attribute affects this macro
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: `#[rustc_const_stable]` can be applied to associated consts, constants, crates, functions, impl blocks, statics, traits, and use statements
98

10-
error: macros cannot have const stability attributes
9+
error: `#[rustc_const_unstable]` attribute cannot be used on macro defs
1110
--> $DIR/const-stability-on-macro.rs:10:1
1211
|
13-
LL | #[rustc_const_unstable(feature = "bar", issue="none")]
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid const stability attribute
15-
LL |
16-
LL | macro_rules! bar {
17-
| ---------------- const stability attribute affects this macro
12+
LL | #[rustc_const_unstable(feature = "bar", issue = "none")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= help: `#[rustc_const_unstable]` can be applied to associated consts, constants, crates, functions, impl blocks, statics, traits, and use statements
1816

1917
error: aborting due to 2 previous errors
2018

0 commit comments

Comments
 (0)