@@ -577,17 +577,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
577
577
}
578
578
579
579
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" ) ) ;
591
595
}
592
596
}
593
597
}
@@ -612,13 +616,8 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
612
616
self . check_missing_stability ( i. def_id , i. span ) ;
613
617
}
614
618
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 ) ;
622
621
623
622
intravisit:: walk_item ( self , i)
624
623
}
@@ -632,6 +631,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
632
631
let impl_def_id = self . tcx . hir ( ) . get_parent_item ( ii. hir_id ( ) ) ;
633
632
if self . tcx . impl_trait_ref ( impl_def_id) . is_none ( ) {
634
633
self . check_missing_stability ( ii. def_id , ii. span ) ;
634
+ self . check_missing_const_stability ( ii. def_id , ii. span ) ;
635
635
}
636
636
intravisit:: walk_impl_item ( self , ii) ;
637
637
}
0 commit comments