@@ -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
373388impl < ' 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