Skip to content

Commit 61d86fa

Browse files
Check for missing const-stability attributes in stability
This used to happen as a side-effect of `is_min_const_fn`, which was subtle.
1 parent 5bfeee5 commit 61d86fa

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

compiler/rustc_passes/src/stability.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
368368
self.tcx.sess.span_err(span, &format!("{} has missing stability attribute", descr));
369369
}
370370
}
371+
372+
fn check_missing_const_stability(&self, hir_id: HirId, span: Span) {
373+
let stab_map = self.tcx.stability();
374+
let stab = stab_map.local_stability(hir_id);
375+
if stab.map_or(false, |stab| stab.level.is_stable()) {
376+
let const_stab = stab_map.local_const_stability(hir_id);
377+
if const_stab.is_none() {
378+
self.tcx.sess.span_err(
379+
span,
380+
"`#[stable]` const functions must also be either \
381+
`#[rustc_const_stable]` or `#[rustc_const_unstable]`",
382+
);
383+
}
384+
}
385+
}
371386
}
372387

373388
impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
@@ -378,14 +393,23 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
378393
}
379394

380395
fn visit_item(&mut self, i: &'tcx Item<'tcx>) {
381-
match i.kind {
382-
// Inherent impls and foreign modules serve only as containers for other items,
383-
// they don't have their own stability. They still can be annotated as unstable
384-
// and propagate this unstability to children, but this annotation is completely
385-
// optional. They inherit stability from their parents when unannotated.
386-
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {}
396+
// Inherent impls and foreign modules serve only as containers for other items,
397+
// they don't have their own stability. They still can be annotated as unstable
398+
// and propagate this unstability to children, but this annotation is completely
399+
// optional. They inherit stability from their parents when unannotated.
400+
if !matches!(
401+
i.kind,
402+
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..)
403+
) {
404+
self.check_missing_stability(i.hir_id, i.span);
405+
}
387406

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

391415
intravisit::walk_item(self, i)

0 commit comments

Comments
 (0)