@@ -44,6 +44,7 @@ struct MarkSymbolVisitor<'tcx> {
4444 repr_has_repr_c : bool ,
4545 in_pat : bool ,
4646 inherited_pub_visibility : bool ,
47+ pub_visibility : bool ,
4748 ignore_variant_stack : Vec < DefId > ,
4849 // maps from tuple struct constructors to tuple struct items
4950 struct_constructors : FxHashMap < hir:: HirId , hir:: HirId > ,
@@ -188,27 +189,33 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
188189
189190 fn visit_node ( & mut self , node : Node < ' tcx > ) {
190191 let had_repr_c = self . repr_has_repr_c ;
191- self . repr_has_repr_c = false ;
192192 let had_inherited_pub_visibility = self . inherited_pub_visibility ;
193+ let had_pub_visibility = self . pub_visibility ;
194+ self . repr_has_repr_c = false ;
193195 self . inherited_pub_visibility = false ;
196+ self . pub_visibility = false ;
194197 match node {
195- Node :: Item ( item) => match item. kind {
196- hir:: ItemKind :: Struct ( ..) | hir:: ItemKind :: Union ( ..) => {
197- let def = self . tcx . adt_def ( item. def_id ) ;
198- self . repr_has_repr_c = def. repr . c ( ) ;
198+ Node :: Item ( item) => {
199+ self . pub_visibility = item. vis . node . is_pub ( ) ;
199200
200- intravisit :: walk_item ( self , & item) ;
201- }
202- hir :: ItemKind :: Enum ( .. ) => {
203- self . inherited_pub_visibility = item . vis . node . is_pub ( ) ;
201+ match item. kind {
202+ hir :: ItemKind :: Struct ( .. ) | hir :: ItemKind :: Union ( .. ) => {
203+ let def = self . tcx . adt_def ( item . def_id ) ;
204+ self . repr_has_repr_c = def . repr . c ( ) ;
204205
205- intravisit:: walk_item ( self , & item) ;
206- }
207- hir:: ItemKind :: ForeignMod { .. } => { }
208- _ => {
209- intravisit:: walk_item ( self , & item) ;
206+ intravisit:: walk_item ( self , & item) ;
207+ }
208+ hir:: ItemKind :: Enum ( ..) => {
209+ self . inherited_pub_visibility = self . pub_visibility ;
210+
211+ intravisit:: walk_item ( self , & item) ;
212+ }
213+ hir:: ItemKind :: ForeignMod { .. } => { }
214+ _ => {
215+ intravisit:: walk_item ( self , & item) ;
216+ }
210217 }
211- } ,
218+ }
212219 Node :: TraitItem ( trait_item) => {
213220 intravisit:: walk_trait_item ( self , trait_item) ;
214221 }
@@ -220,8 +227,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
220227 }
221228 _ => { }
222229 }
223- self . repr_has_repr_c = had_repr_c ;
230+ self . pub_visibility = had_pub_visibility ;
224231 self . inherited_pub_visibility = had_inherited_pub_visibility;
232+ self . repr_has_repr_c = had_repr_c;
225233 }
226234
227235 fn mark_as_used_if_union ( & mut self , adt : & ty:: AdtDef , fields : & [ hir:: ExprField < ' _ > ] ) {
@@ -259,10 +267,10 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
259267 ) {
260268 let has_repr_c = self . repr_has_repr_c ;
261269 let inherited_pub_visibility = self . inherited_pub_visibility ;
262- let live_fields = def
263- . fields ( )
264- . iter ( )
265- . filter ( |f| has_repr_c || inherited_pub_visibility || f . vis . node . is_pub ( ) ) ;
270+ let pub_visibility = self . pub_visibility ;
271+ let live_fields = def . fields ( ) . iter ( ) . filter ( |f| {
272+ has_repr_c || ( pub_visibility && ( inherited_pub_visibility || f . vis . node . is_pub ( ) ) )
273+ } ) ;
266274 self . live_symbols . extend ( live_fields. map ( |f| f. hir_id ) ) ;
267275
268276 intravisit:: walk_struct_def ( self , def) ;
@@ -500,6 +508,7 @@ fn find_live<'tcx>(
500508 repr_has_repr_c : false ,
501509 in_pat : false ,
502510 inherited_pub_visibility : false ,
511+ pub_visibility : false ,
503512 ignore_variant_stack : vec ! [ ] ,
504513 struct_constructors,
505514 } ;
0 commit comments