1
1
#![ doc( html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/" ) ]
2
+ #![ feature( associated_type_defaults) ]
2
3
#![ feature( control_flow_enum) ]
4
+ #![ feature( rustc_private) ]
3
5
#![ feature( try_blocks) ]
4
- #![ feature( associated_type_defaults) ]
5
6
#![ recursion_limit = "256" ]
6
7
#![ allow( rustc:: potential_query_instability) ]
8
+ #![ cfg_attr( not( bootstrap) , deny( rustc:: untranslatable_diagnostic) ) ]
9
+ #![ cfg_attr( not( bootstrap) , deny( rustc:: diagnostic_outside_of_impl) ) ]
10
+
11
+ mod errors;
7
12
8
13
use rustc_ast:: MacroDef ;
9
14
use rustc_attr as attr;
10
15
use rustc_data_structures:: fx:: FxHashSet ;
11
16
use rustc_data_structures:: intern:: Interned ;
12
- use rustc_errors:: struct_span_err;
13
17
use rustc_hir as hir;
14
18
use rustc_hir:: def:: { DefKind , Res } ;
15
19
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalDefIdSet , CRATE_DEF_ID } ;
@@ -34,6 +38,11 @@ use std::marker::PhantomData;
34
38
use std:: ops:: ControlFlow ;
35
39
use std:: { cmp, fmt, mem} ;
36
40
41
+ use errors:: {
42
+ FieldIsPrivate , FieldIsPrivateLabel , InPublicInterface , InPublicInterfaceTraits , ItemIsPrivate ,
43
+ UnnamedItemIsPrivate ,
44
+ } ;
45
+
37
46
////////////////////////////////////////////////////////////////////////////////
38
47
/// Generic infrastructure used to implement specific visitors below.
39
48
////////////////////////////////////////////////////////////////////////////////
@@ -935,23 +944,17 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
935
944
let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( self . current_item ) ;
936
945
let def_id = self . tcx . adjust_ident_and_get_scope ( ident, def. did ( ) , hir_id) . 1 ;
937
946
if !field. vis . is_accessible_from ( def_id, self . tcx ) {
938
- let label = if in_update_syntax {
939
- format ! ( "field `{}` is private" , field. name)
940
- } else {
941
- "private field" . to_string ( )
942
- } ;
943
-
944
- struct_span_err ! (
945
- self . tcx. sess,
947
+ self . tcx . sess . emit_err ( FieldIsPrivate {
946
948
span,
947
- E0451 ,
948
- "field `{}` of {} `{}` is private" ,
949
- field. name,
950
- def. variant_descr( ) ,
951
- self . tcx. def_path_str( def. did( ) )
952
- )
953
- . span_label ( span, label)
954
- . emit ( ) ;
949
+ field_name : field. name ,
950
+ variant_descr : def. variant_descr ( ) ,
951
+ def_path_str : self . tcx . def_path_str ( def. did ( ) ) ,
952
+ label : if in_update_syntax {
953
+ FieldIsPrivateLabel :: IsUpdateSyntax { span, field_name : field. name }
954
+ } else {
955
+ FieldIsPrivateLabel :: Other { span }
956
+ } ,
957
+ } ) ;
955
958
}
956
959
}
957
960
}
@@ -1075,11 +1078,11 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
1075
1078
fn check_def_id ( & mut self , def_id : DefId , kind : & str , descr : & dyn fmt:: Display ) -> bool {
1076
1079
let is_error = !self . item_is_accessible ( def_id) ;
1077
1080
if is_error {
1078
- self . tcx
1079
- . sess
1080
- . struct_span_err ( self . span , & format ! ( "{} `{}` is private" , kind, descr ) )
1081
- . span_label ( self . span , & format ! ( "private {}" , kind ) )
1082
- . emit ( ) ;
1081
+ self . tcx . sess . emit_err ( ItemIsPrivate {
1082
+ span : self . span ,
1083
+ kind,
1084
+ descr : descr . to_string ( ) ,
1085
+ } ) ;
1083
1086
}
1084
1087
is_error
1085
1088
}
@@ -1250,13 +1253,10 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
1250
1253
hir:: QPath :: TypeRelative ( _, segment) => Some ( segment. ident . to_string ( ) ) ,
1251
1254
} ;
1252
1255
let kind = kind. descr ( def_id) ;
1253
- let msg = match name {
1254
- Some ( name) => format ! ( "{} `{}` is private" , kind, name) ,
1255
- None => format ! ( "{} is private" , kind) ,
1256
+ let _ = match name {
1257
+ Some ( name) => sess . emit_err ( ItemIsPrivate { span , kind, descr : name } ) ,
1258
+ None => sess . emit_err ( UnnamedItemIsPrivate { span , kind } ) ,
1256
1259
} ;
1257
- sess. struct_span_err ( span, & msg)
1258
- . span_label ( span, & format ! ( "private {}" , kind) )
1259
- . emit ( ) ;
1260
1260
return ;
1261
1261
}
1262
1262
}
@@ -1753,30 +1753,44 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
1753
1753
}
1754
1754
}
1755
1755
} ;
1756
- let make_msg = || format ! ( "{} {} `{}` in public interface" , vis_descr, kind, descr) ;
1757
1756
let span = self . tcx . def_span ( self . item_def_id . to_def_id ( ) ) ;
1758
1757
if self . has_old_errors
1759
1758
|| self . in_assoc_ty
1760
1759
|| self . tcx . resolutions ( ( ) ) . has_pub_restricted
1761
1760
{
1762
- let mut err = if kind == "trait" {
1763
- struct_span_err ! ( self . tcx. sess, span, E0445 , "{}" , make_msg( ) )
1764
- } else {
1765
- struct_span_err ! ( self . tcx. sess, span, E0446 , "{}" , make_msg( ) )
1766
- } ;
1761
+ let descr = descr. to_string ( ) ;
1767
1762
let vis_span =
1768
1763
self . tcx . sess . source_map ( ) . guess_head_span ( self . tcx . def_span ( def_id) ) ;
1769
- err. span_label ( span, format ! ( "can't leak {} {}" , vis_descr, kind) ) ;
1770
- err. span_label ( vis_span, format ! ( "`{}` declared as {}" , descr, vis_descr) ) ;
1771
- err. emit ( ) ;
1764
+ if kind == "trait" {
1765
+ self . tcx . sess . emit_err ( InPublicInterfaceTraits {
1766
+ span,
1767
+ vis_descr,
1768
+ kind,
1769
+ descr,
1770
+ vis_span,
1771
+ } ) ;
1772
+ } else {
1773
+ self . tcx . sess . emit_err ( InPublicInterface {
1774
+ span,
1775
+ vis_descr,
1776
+ kind,
1777
+ descr,
1778
+ vis_span,
1779
+ } ) ;
1780
+ }
1772
1781
} else {
1773
1782
let err_code = if kind == "trait" { "E0445" } else { "E0446" } ;
1774
1783
self . tcx . struct_span_lint_hir (
1775
1784
lint:: builtin:: PRIVATE_IN_PUBLIC ,
1776
1785
hir_id,
1777
1786
span,
1778
1787
|lint| {
1779
- lint. build ( & format ! ( "{} (error {})" , make_msg( ) , err_code) ) . emit ( ) ;
1788
+ lint. build ( & format ! (
1789
+ "{} (error {})" ,
1790
+ format!( "{} {} `{}` in public interface" , vis_descr, kind, descr) ,
1791
+ err_code
1792
+ ) )
1793
+ . emit ( ) ;
1780
1794
} ,
1781
1795
) ;
1782
1796
}
0 commit comments