Skip to content

Commit 41f84c2

Browse files
committed
Require const stability on all stable const items
This was supposed to be the case previously, but a missed method call meant that trait impls were not checked.
1 parent ad88831 commit 41f84c2

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

compiler/rustc_passes/src/stability.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
577577
}
578578

579579
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
580-
let stab_map = self.tcx.stability();
581-
let stab = stab_map.local_stability(def_id);
582-
if stab.map_or(false, |stab| stab.level.is_stable()) {
583-
let const_stab = stab_map.local_const_stability(def_id);
584-
if const_stab.is_none() {
585-
self.tcx.sess.span_err(
586-
span,
587-
"`#[stable]` const functions must also be either \
588-
`#[rustc_const_stable]` or `#[rustc_const_unstable]`",
589-
);
590-
}
580+
if !self.tcx.features().staged_api {
581+
return;
582+
}
583+
584+
let is_const = self.tcx.is_const_fn(def_id.to_def_id());
585+
let is_stable = self
586+
.tcx
587+
.lookup_stability(def_id)
588+
.map_or(false, |stability| stability.level.is_stable());
589+
let missing_const_stability_attribute = self.tcx.lookup_const_stability(def_id).is_none();
590+
let is_reachable = self.access_levels.is_reachable(def_id);
591+
592+
if is_const && is_stable && missing_const_stability_attribute && is_reachable {
593+
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
594+
self.tcx.sess.span_err(span, &format!("{descr} has missing const stability attribute"));
591595
}
592596
}
593597
}
@@ -612,13 +616,8 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
612616
self.check_missing_stability(i.def_id, i.span);
613617
}
614618

615-
// Ensure `const fn` that are `stable` have one of `rustc_const_unstable` or
616-
// `rustc_const_stable`.
617-
if self.tcx.features().staged_api
618-
&& matches!(&i.kind, hir::ItemKind::Fn(sig, ..) if sig.header.is_const())
619-
{
620-
self.check_missing_const_stability(i.def_id, i.span);
621-
}
619+
// Ensure stable `const fn` have a const stability attribute.
620+
self.check_missing_const_stability(i.def_id, i.span);
622621

623622
intravisit::walk_item(self, i)
624623
}
@@ -632,6 +631,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
632631
let impl_def_id = self.tcx.hir().get_parent_item(ii.hir_id());
633632
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
634633
self.check_missing_stability(ii.def_id, ii.span);
634+
self.check_missing_const_stability(ii.def_id, ii.span);
635635
}
636636
intravisit::walk_impl_item(self, ii);
637637
}
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#![feature(staged_api)]
2+
#![stable(feature = "stable", since = "1.0.0")]
23

3-
#![stable(feature = "rust1", since = "1.0.0")]
4+
#[stable(feature = "stable", since = "1.0.0")]
5+
pub const fn foo() {} //~ ERROR function has missing const stability attribute
46

5-
#[stable(feature = "foo", since = "1.0.0")]
6-
pub const fn foo() {}
7-
//~^ ERROR rustc_const_stable
7+
#[unstable(feature = "unstable", issue = "none")]
8+
pub const fn bar() {} // ok for now
89

9-
#[unstable(feature = "bar", issue = "none")]
10-
pub const fn bar() {} // ok
10+
#[stable(feature = "stable", since = "1.0.0")]
11+
pub struct Foo;
12+
impl Foo {
13+
#[stable(feature = "stable", since = "1.0.0")]
14+
pub const fn foo() {} //~ ERROR associated function has missing const stability attribute
15+
16+
#[unstable(feature = "unstable", issue = "none")]
17+
pub const fn bar() {} // ok for now
18+
}
19+
20+
// FIXME When #![feature(const_trait_impl)] is stabilized, add tests for const
21+
// trait impls. Right now, a "trait methods cannot be stable const fn" error is
22+
// emitted, but that's not in the scope of this test.
1123

1224
fn main() {}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error: `#[stable]` const functions must also be either `#[rustc_const_stable]` or `#[rustc_const_unstable]`
2-
--> $DIR/missing-const-stability.rs:6:1
1+
error: function has missing const stability attribute
2+
--> $DIR/missing-const-stability.rs:5:1
33
|
44
LL | pub const fn foo() {}
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: associated function has missing const stability attribute
8+
--> $DIR/missing-const-stability.rs:14:5
9+
|
10+
LL | pub const fn foo() {}
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

0 commit comments

Comments
 (0)