@@ -219,7 +219,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
219
219
if kind == AnnotationKind :: Prohibited
220
220
|| ( kind == AnnotationKind :: Container && stab. level . is_stable ( ) && is_deprecated)
221
221
{
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" )
223
223
. span_label ( span, "useless stability annotation" )
224
224
. span_label ( item_sp, "the stability attribute annotates this item" )
225
225
. emit ( ) ;
@@ -605,6 +605,24 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
605
605
}
606
606
}
607
607
}
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
+ }
608
626
}
609
627
610
628
impl < ' tcx > Visitor < ' tcx > for MissingStabilityAnnotations < ' tcx > {
@@ -635,11 +653,14 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
635
653
self . check_missing_const_stability ( i. hir_id ( ) , i. span ) ;
636
654
}
637
655
656
+ self . check_private_stability ( i. hir_id ( ) , i. span ) ;
657
+
638
658
intravisit:: walk_item ( self , i)
639
659
}
640
660
641
661
fn visit_trait_item ( & mut self , ti : & ' tcx hir:: TraitItem < ' tcx > ) {
642
662
self . check_missing_stability ( ti. hir_id ( ) , ti. span ) ;
663
+ self . check_private_stability ( ti. hir_id ( ) , ti. span ) ;
643
664
intravisit:: walk_trait_item ( self , ti) ;
644
665
}
645
666
@@ -648,26 +669,31 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
648
669
if self . tcx . impl_trait_ref ( impl_def_id) . is_none ( ) {
649
670
self . check_missing_stability ( ii. hir_id ( ) , ii. span ) ;
650
671
}
672
+ self . check_private_stability ( ii. hir_id ( ) , ii. span ) ;
651
673
intravisit:: walk_impl_item ( self , ii) ;
652
674
}
653
675
654
676
fn visit_variant ( & mut self , var : & ' tcx Variant < ' tcx > , g : & ' tcx Generics < ' tcx > , item_id : HirId ) {
655
677
self . check_missing_stability ( var. id , var. span ) ;
678
+ self . check_private_stability ( var. id , var. span ) ;
656
679
intravisit:: walk_variant ( self , var, g, item_id) ;
657
680
}
658
681
659
682
fn visit_field_def ( & mut self , s : & ' tcx FieldDef < ' tcx > ) {
660
683
self . check_missing_stability ( s. hir_id , s. span ) ;
684
+ self . check_private_stability ( s. hir_id , s. span ) ;
661
685
intravisit:: walk_field_def ( self , s) ;
662
686
}
663
687
664
688
fn visit_foreign_item ( & mut self , i : & ' tcx hir:: ForeignItem < ' tcx > ) {
665
689
self . check_missing_stability ( i. hir_id ( ) , i. span ) ;
690
+ self . check_private_stability ( i. hir_id ( ) , i. span ) ;
666
691
intravisit:: walk_foreign_item ( self , i) ;
667
692
}
668
693
669
694
fn visit_macro_def ( & mut self , md : & ' tcx hir:: MacroDef < ' tcx > ) {
670
695
self . check_missing_stability ( md. hir_id ( ) , md. span ) ;
696
+ self . check_private_stability ( md. hir_id ( ) , md. span ) ;
671
697
}
672
698
673
699
// 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<'_>) {
930
956
let krate = tcx. hir ( ) . krate ( ) ;
931
957
let mut missing = MissingStabilityAnnotations { tcx, access_levels } ;
932
958
missing. check_missing_stability ( hir:: CRATE_HIR_ID , krate. item . inner ) ;
959
+ missing. check_private_stability ( hir:: CRATE_HIR_ID , krate. item . inner ) ;
933
960
intravisit:: walk_crate ( & mut missing, krate) ;
934
961
krate. visit_all_item_likes ( & mut missing. as_deep_visitor ( ) ) ;
935
962
}
0 commit comments