Skip to content

Commit

Permalink
Rollup merge of #77203 - ecstatic-morse:const-stability-attr-checks, …
Browse files Browse the repository at this point in the history
…r=oli-obk

Check for missing const-stability attributes in `rustc_passes`

Currently, this happens as a side effect of `is_min_const_fn`, which is non-obvious. Also adds a test for this case, since we didn't seem to have one before.
  • Loading branch information
jonas-schievink authored Sep 27, 2020
2 parents ec1766c + 6ce178f commit 9f086fc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
None => {
if let Some(stab) = tcx.lookup_stability(def_id) {
if stab.level.is_stable() {
tcx.sess.span_err(
tcx.sess.delay_span_bug(
tcx.def_span(def_id),
"stable const functions must have either `rustc_const_stable` or \
`rustc_const_unstable` attribute",
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_mir/src/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ impl Validator<'mir, 'tcx> {
pub fn check_body(&mut self) {
let ConstCx { tcx, body, def_id, .. } = *self.ccx;

// HACK: This function has side-effects???? Make sure we call it.
let _ = crate::const_eval::is_min_const_fn(tcx, def_id.to_def_id());

// The local type and predicate checks are not free and only relevant for `const fn`s.
if self.const_kind() == hir::ConstContext::ConstFn {
// Prevent const trait methods from being annotated as `stable`.
Expand Down
38 changes: 31 additions & 7 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
self.tcx.sess.span_err(span, &format!("{} has missing stability attribute", descr));
}
}

fn check_missing_const_stability(&self, hir_id: HirId, span: Span) {
let stab_map = self.tcx.stability();
let stab = stab_map.local_stability(hir_id);
if stab.map_or(false, |stab| stab.level.is_stable()) {
let const_stab = stab_map.local_const_stability(hir_id);
if const_stab.is_none() {
self.tcx.sess.span_err(
span,
"`#[stable]` const functions must also be either \
`#[rustc_const_stable]` or `#[rustc_const_unstable]`",
);
}
}
}
}

impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
Expand All @@ -469,14 +484,23 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
}

fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
match i.kind {
// Inherent impls and foreign modules serve only as containers for other items,
// they don't have their own stability. They still can be annotated as unstable
// and propagate this unstability to children, but this annotation is completely
// optional. They inherit stability from their parents when unannotated.
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {}
// Inherent impls and foreign modules serve only as containers for other items,
// they don't have their own stability. They still can be annotated as unstable
// and propagate this unstability to children, but this annotation is completely
// optional. They inherit stability from their parents when unannotated.
if !matches!(
i.kind,
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..)
) {
self.check_missing_stability(i.hir_id, i.span);
}

_ => self.check_missing_stability(i.hir_id, i.span),
// Ensure `const fn` that are `stable` have one of `rustc_const_unstable` or
// `rustc_const_stable`.
if self.tcx.features().staged_api
&& matches!(&i.kind, hir::ItemKind::Fn(sig, ..) if sig.header.is_const())
{
self.check_missing_const_stability(i.hir_id, i.span);
}

intravisit::walk_item(self, i)
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/stability-attribute/missing-const-stability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(staged_api)]

#![stable(feature = "rust1", since = "1.0.0")]

#[stable(feature = "foo", since = "1.0.0")]
pub const fn foo() {}
//~^ ERROR rustc_const_stable

#[unstable(feature = "bar", issue = "none")]
pub const fn bar() {} // ok

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `#[stable]` const functions must also be either `#[rustc_const_stable]` or `#[rustc_const_unstable]`
--> $DIR/missing-const-stability.rs:6:1
|
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit 9f086fc

Please sign in to comment.