@@ -219,7 +219,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
219219 if kind == AnnotationKind :: Prohibited
220220 || ( kind == AnnotationKind :: Container && stab. level . is_stable ( ) && is_deprecated)
221221 {
222- self . tcx . sess . struct_span_err ( span, "this stability annotation is useless" )
222+ self . tcx . sess . struct_span_err ( span, "this stability annotation is useless" )
223223 . span_label ( span, "useless stability annotation" )
224224 . span_label ( item_sp, "the stability attribute annotates this item" )
225225 . emit ( ) ;
@@ -605,6 +605,24 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
605605 }
606606 }
607607 }
608+
609+ fn check_private_stability ( & self , hir_id : HirId , span : Span ) {
610+ let stab = self . tcx . stability ( ) . local_stability ( hir_id) ;
611+ let is_error = stab. is_some ( ) && !self . access_levels . is_reachable ( hir_id) ;
612+ if is_error {
613+ let def_id = self . tcx . hir ( ) . local_def_id ( hir_id) ;
614+ let descr = self . tcx . def_kind ( def_id) . descr ( def_id. to_def_id ( ) ) ;
615+ self . tcx
616+ . sess
617+ . struct_span_err (
618+ span,
619+ & format ! ( "{} is private but has a stability attribute" , descr) ,
620+ )
621+ . help ( "if it is meant to be private, remove the stability attribute" )
622+ . help ( "or, if it is meant to be public, make it public" )
623+ . emit ( ) ;
624+ }
625+ }
608626}
609627
610628impl < ' tcx > Visitor < ' tcx > for MissingStabilityAnnotations < ' tcx > {
@@ -635,11 +653,14 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
635653 self . check_missing_const_stability ( i. hir_id ( ) , i. span ) ;
636654 }
637655
656+ self . check_private_stability ( i. hir_id ( ) , i. span ) ;
657+
638658 intravisit:: walk_item ( self , i)
639659 }
640660
641661 fn visit_trait_item ( & mut self , ti : & ' tcx hir:: TraitItem < ' tcx > ) {
642662 self . check_missing_stability ( ti. hir_id ( ) , ti. span ) ;
663+ self . check_private_stability ( ti. hir_id ( ) , ti. span ) ;
643664 intravisit:: walk_trait_item ( self , ti) ;
644665 }
645666
@@ -648,26 +669,31 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
648669 if self . tcx . impl_trait_ref ( impl_def_id) . is_none ( ) {
649670 self . check_missing_stability ( ii. hir_id ( ) , ii. span ) ;
650671 }
672+ self . check_private_stability ( ii. hir_id ( ) , ii. span ) ;
651673 intravisit:: walk_impl_item ( self , ii) ;
652674 }
653675
654676 fn visit_variant ( & mut self , var : & ' tcx Variant < ' tcx > , g : & ' tcx Generics < ' tcx > , item_id : HirId ) {
655677 self . check_missing_stability ( var. id , var. span ) ;
678+ self . check_private_stability ( var. id , var. span ) ;
656679 intravisit:: walk_variant ( self , var, g, item_id) ;
657680 }
658681
659682 fn visit_field_def ( & mut self , s : & ' tcx FieldDef < ' tcx > ) {
660683 self . check_missing_stability ( s. hir_id , s. span ) ;
684+ self . check_private_stability ( s. hir_id , s. span ) ;
661685 intravisit:: walk_field_def ( self , s) ;
662686 }
663687
664688 fn visit_foreign_item ( & mut self , i : & ' tcx hir:: ForeignItem < ' tcx > ) {
665689 self . check_missing_stability ( i. hir_id ( ) , i. span ) ;
690+ self . check_private_stability ( i. hir_id ( ) , i. span ) ;
666691 intravisit:: walk_foreign_item ( self , i) ;
667692 }
668693
669694 fn visit_macro_def ( & mut self , md : & ' tcx hir:: MacroDef < ' tcx > ) {
670695 self . check_missing_stability ( md. hir_id ( ) , md. span ) ;
696+ self . check_private_stability ( md. hir_id ( ) , md. span ) ;
671697 }
672698
673699 // Note that we don't need to `check_missing_stability` for default generic parameters,
@@ -930,6 +956,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
930956 let krate = tcx. hir ( ) . krate ( ) ;
931957 let mut missing = MissingStabilityAnnotations { tcx, access_levels } ;
932958 missing. check_missing_stability ( hir:: CRATE_HIR_ID , krate. item . inner ) ;
959+ missing. check_private_stability ( hir:: CRATE_HIR_ID , krate. item . inner ) ;
933960 intravisit:: walk_crate ( & mut missing, krate) ;
934961 krate. visit_all_item_likes ( & mut missing. as_deep_visitor ( ) ) ;
935962 }
0 commit comments