@@ -24,7 +24,6 @@ use rustc_hir::def::{DefKind, Res};
24
24
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId , CRATE_DEF_ID } ;
25
25
use rustc_hir:: intravisit:: { self , Visitor } ;
26
26
use rustc_hir:: { AssocItemKind , ForeignItemKind , ItemId , PatKind } ;
27
- use rustc_middle:: hir:: nested_filter;
28
27
use rustc_middle:: middle:: privacy:: { EffectiveVisibilities , EffectiveVisibility , Level } ;
29
28
use rustc_middle:: query:: Providers ;
30
29
use rustc_middle:: ty:: GenericArgs ;
@@ -36,9 +35,9 @@ use rustc_span::hygiene::Transparency;
36
35
use rustc_span:: symbol:: { kw, sym, Ident } ;
37
36
use rustc_span:: Span ;
38
37
38
+ use std:: fmt;
39
39
use std:: marker:: PhantomData ;
40
40
use std:: ops:: ControlFlow ;
41
- use std:: { fmt, mem} ;
42
41
43
42
use errors:: {
44
43
FieldIsPrivate , FieldIsPrivateLabel , FromPrivateDependencyInPublicInterface , InPublicInterface ,
@@ -935,7 +934,6 @@ impl<'tcx, 'a> Visitor<'tcx> for TestReachabilityVisitor<'tcx, 'a> {
935
934
struct NamePrivacyVisitor < ' tcx > {
936
935
tcx : TyCtxt < ' tcx > ,
937
936
maybe_typeck_results : Option < & ' tcx ty:: TypeckResults < ' tcx > > ,
938
- current_item : LocalDefId ,
939
937
}
940
938
941
939
impl < ' tcx > NamePrivacyVisitor < ' tcx > {
@@ -951,6 +949,7 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
951
949
// Checks that a field in a struct constructor (expression or pattern) is accessible.
952
950
fn check_field (
953
951
& mut self ,
952
+ hir_id : hir:: HirId , // ID of the field use
954
953
use_ctxt : Span , // syntax context of the field name at the use site
955
954
span : Span , // span of the field pattern, e.g., `x: 0`
956
955
def : ty:: AdtDef < ' tcx > , // definition of the struct or enum
@@ -963,7 +962,6 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
963
962
964
963
// definition of the field
965
964
let ident = Ident :: new ( kw:: Empty , use_ctxt) ;
966
- let hir_id = self . tcx . local_def_id_to_hir_id ( self . current_item ) ;
967
965
let def_id = self . tcx . adjust_ident_and_get_scope ( ident, def. did ( ) , hir_id) . 1 ;
968
966
if !field. vis . is_accessible_from ( def_id, self . tcx ) {
969
967
self . tcx . dcx ( ) . emit_err ( FieldIsPrivate {
@@ -982,33 +980,13 @@ impl<'tcx> NamePrivacyVisitor<'tcx> {
982
980
}
983
981
984
982
impl < ' tcx > Visitor < ' tcx > for NamePrivacyVisitor < ' tcx > {
985
- type NestedFilter = nested_filter:: All ;
986
-
987
- /// We want to visit items in the context of their containing
988
- /// module and so forth, so supply a crate for doing a deep walk.
989
- fn nested_visit_map ( & mut self ) -> Self :: Map {
990
- self . tcx . hir ( )
991
- }
992
-
993
- fn visit_mod ( & mut self , _m : & ' tcx hir:: Mod < ' tcx > , _s : Span , _n : hir:: HirId ) {
994
- // Don't visit nested modules, since we run a separate visitor walk
995
- // for each module in `effective_visibilities`
996
- }
997
-
998
- fn visit_nested_body ( & mut self , body : hir:: BodyId ) {
983
+ fn visit_nested_body ( & mut self , body_id : hir:: BodyId ) {
999
984
let old_maybe_typeck_results =
1000
- self . maybe_typeck_results . replace ( self . tcx . typeck_body ( body) ) ;
1001
- let body = self . tcx . hir ( ) . body ( body) ;
1002
- self . visit_body ( body) ;
985
+ self . maybe_typeck_results . replace ( self . tcx . typeck_body ( body_id) ) ;
986
+ self . visit_body ( self . tcx . hir ( ) . body ( body_id) ) ;
1003
987
self . maybe_typeck_results = old_maybe_typeck_results;
1004
988
}
1005
989
1006
- fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
1007
- let orig_current_item = mem:: replace ( & mut self . current_item , item. owner_id . def_id ) ;
1008
- intravisit:: walk_item ( self , item) ;
1009
- self . current_item = orig_current_item;
1010
- }
1011
-
1012
990
fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' tcx > ) {
1013
991
if let hir:: ExprKind :: Struct ( qpath, fields, ref base) = expr. kind {
1014
992
let res = self . typeck_results ( ) . qpath_res ( qpath, expr. hir_id ) ;
@@ -1022,17 +1000,17 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
1022
1000
let field = fields
1023
1001
. iter ( )
1024
1002
. find ( |f| self . typeck_results ( ) . field_index ( f. hir_id ) == vf_index) ;
1025
- let ( use_ctxt, span) = match field {
1026
- Some ( field) => ( field. ident . span , field. span ) ,
1027
- None => ( base. span , base. span ) ,
1003
+ let ( hir_id , use_ctxt, span) = match field {
1004
+ Some ( field) => ( field. hir_id , field . ident . span , field. span ) ,
1005
+ None => ( base. hir_id , base . span , base. span ) ,
1028
1006
} ;
1029
- self . check_field ( use_ctxt, span, adt, variant_field, true ) ;
1007
+ self . check_field ( hir_id , use_ctxt, span, adt, variant_field, true ) ;
1030
1008
}
1031
1009
} else {
1032
1010
for field in fields {
1033
- let use_ctxt = field. ident . span ;
1011
+ let ( hir_id , use_ctxt, span ) = ( field. hir_id , field . ident . span , field . span ) ;
1034
1012
let index = self . typeck_results ( ) . field_index ( field. hir_id ) ;
1035
- self . check_field ( use_ctxt, field . span , adt, & variant. fields [ index] , false ) ;
1013
+ self . check_field ( hir_id , use_ctxt, span, adt, & variant. fields [ index] , false ) ;
1036
1014
}
1037
1015
}
1038
1016
}
@@ -1046,9 +1024,9 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> {
1046
1024
let adt = self . typeck_results ( ) . pat_ty ( pat) . ty_adt_def ( ) . unwrap ( ) ;
1047
1025
let variant = adt. variant_of_res ( res) ;
1048
1026
for field in fields {
1049
- let use_ctxt = field. ident . span ;
1027
+ let ( hir_id , use_ctxt, span ) = ( field. hir_id , field . ident . span , field . span ) ;
1050
1028
let index = self . typeck_results ( ) . field_index ( field. hir_id ) ;
1051
- self . check_field ( use_ctxt, field . span , adt, & variant. fields [ index] , false ) ;
1029
+ self . check_field ( hir_id , use_ctxt, span, adt, & variant. fields [ index] , false ) ;
1052
1030
}
1053
1031
}
1054
1032
@@ -1743,17 +1721,12 @@ pub fn provide(providers: &mut Providers) {
1743
1721
1744
1722
fn check_mod_privacy ( tcx : TyCtxt < ' _ > , module_def_id : LocalModDefId ) {
1745
1723
// Check privacy of names not checked in previous compilation stages.
1746
- let mut visitor = NamePrivacyVisitor {
1747
- tcx,
1748
- maybe_typeck_results : None ,
1749
- current_item : module_def_id. to_local_def_id ( ) ,
1750
- } ;
1751
- let ( module, span, hir_id) = tcx. hir ( ) . get_module ( module_def_id) ;
1752
-
1753
- intravisit:: walk_mod ( & mut visitor, module, hir_id) ;
1724
+ let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results : None } ;
1725
+ tcx. hir ( ) . visit_item_likes_in_module ( module_def_id, & mut visitor) ;
1754
1726
1755
1727
// Check privacy of explicitly written types and traits as well as
1756
1728
// inferred types of expressions and patterns.
1729
+ let span = tcx. def_span ( module_def_id) ;
1757
1730
let mut visitor = TypePrivacyVisitor { tcx, module_def_id, maybe_typeck_results : None , span } ;
1758
1731
tcx. hir ( ) . visit_item_likes_in_module ( module_def_id, & mut visitor) ;
1759
1732
}
0 commit comments