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