Skip to content

Commit 88eda64

Browse files
authored
Rollup merge of #152431 - oli-obk:limited_stability_attr, r=jdonszelmann
Restrict the set of things that const stability can be applied to r? @jdonszelmann
2 parents 2194af9 + 475b9d9 commit 88eda64

6 files changed

Lines changed: 26 additions & 35 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/stability.rs

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

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

compiler/rustc_expand/src/base.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,15 +962,6 @@ impl SyntaxExtension {
962962

963963
let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);
964964

965-
// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
966-
if let Some(sp) =
967-
find_attr!(attrs, AttributeKind::RustcConstStability { span, .. } => *span)
968-
{
969-
sess.dcx().emit_err(errors::MacroConstStability {
970-
span: sp,
971-
head_span: sess.source_map().guess_head_span(span),
972-
});
973-
}
974965
if let Some(sp) = find_attr!(attrs, AttributeKind::RustcBodyStability{ span, .. } => *span)
975966
{
976967
sess.dcx().emit_err(errors::MacroBodyStability {

compiler/rustc_expand/src/errors.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ pub(crate) struct ResolveRelativePath {
8080
pub path: String,
8181
}
8282

83-
#[derive(Diagnostic)]
84-
#[diag("macros cannot have const stability attributes")]
85-
pub(crate) struct MacroConstStability {
86-
#[primary_span]
87-
#[label("invalid const stability attribute")]
88-
pub span: Span,
89-
#[label("const stability attribute affects this macro")]
90-
pub head_span: Span,
91-
}
92-
9383
#[derive(Diagnostic)]
9484
#[diag("macros cannot have body stability attributes")]
9585
pub(crate) struct MacroBodyStability {

library/core/src/array/drain.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ impl<'l, 'f, T, U, const N: usize, F: FnMut(T) -> U> Drain<'l, 'f, T, N, F> {
3131
}
3232

3333
/// See [`Drain::new`]; this is our fake iterator.
34-
#[rustc_const_unstable(feature = "array_try_map", issue = "79711")]
3534
#[unstable(feature = "array_try_map", issue = "79711")]
3635
pub(super) struct Drain<'l, 'f, T, const N: usize, F> {
3736
// FIXME(const-hack): This is essentially a slice::IterMut<'static>, replace when possible.

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)