14
14
15
15
use std:: mem:: replace;
16
16
17
- use metadata:: csearch;
18
17
use middle:: lint;
19
18
use middle:: resolve;
20
19
use middle:: ty;
@@ -562,53 +561,10 @@ impl<'a> PrivacyVisitor<'a> {
562
561
563
562
// Checks that a field is in scope.
564
563
// FIXME #6993: change type (and name) from Ident to Name
565
- fn check_field ( & mut self , span : Span , id : ast:: DefId , ident : ast:: Ident ,
566
- enum_id : Option < ast:: DefId > ) {
567
- let fields = ty:: lookup_struct_fields ( self . tcx , id) ;
568
- let struct_vis = if is_local ( id) {
569
- match self . tcx . map . get ( id. node ) {
570
- ast_map:: NodeItem ( ref it) => it. vis ,
571
- ast_map:: NodeVariant ( ref v) => {
572
- if v. node . vis == ast:: Inherited {
573
- let parent = self . tcx . map . get_parent ( id. node ) ;
574
- self . tcx . map . expect_item ( parent) . vis
575
- } else {
576
- v. node . vis
577
- }
578
- }
579
- _ => {
580
- self . tcx . sess . span_bug ( span,
581
- format ! ( "not an item or variant def" ) ) ;
582
- }
583
- }
584
- } else {
585
- let cstore = & self . tcx . sess . cstore ;
586
- match enum_id {
587
- Some ( enum_id) => {
588
- let v = csearch:: get_enum_variants ( self . tcx , enum_id) ;
589
- match v. iter ( ) . find ( |v| v. id == id) {
590
- Some ( variant) => {
591
- if variant. vis == ast:: Inherited {
592
- csearch:: get_item_visibility ( cstore, enum_id)
593
- } else {
594
- variant. vis
595
- }
596
- }
597
- None => {
598
- self . tcx . sess . span_bug ( span, "no xcrate variant" ) ;
599
- }
600
- }
601
- }
602
- None => csearch:: get_item_visibility ( cstore, id)
603
- }
604
- } ;
605
-
606
- for field in fields. iter ( ) {
564
+ fn check_field ( & mut self , span : Span , id : ast:: DefId , ident : ast:: Ident ) {
565
+ for field in ty:: lookup_struct_fields ( self . tcx , id) . iter ( ) {
607
566
if field. name != ident. name { continue ; }
608
- // public structs have public fields by default, and private structs
609
- // have private fields by default.
610
- if struct_vis == ast:: Public && field. vis != ast:: Private { break }
611
- if struct_vis != ast:: Public && field. vis == ast:: Public { break }
567
+ if field. vis == ast:: Public { break }
612
568
if !is_local ( field. id ) ||
613
569
!self . private_accessible ( field. id . node ) {
614
570
self . tcx . sess . span_err ( span,
@@ -770,7 +726,7 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
770
726
match ty:: get ( ty:: expr_ty_adjusted ( self . tcx , base,
771
727
& * self . method_map . borrow ( ) ) ) . sty {
772
728
ty:: ty_struct( id, _) => {
773
- self . check_field ( expr. span , id, ident, None ) ;
729
+ self . check_field ( expr. span , id, ident) ;
774
730
}
775
731
_ => { }
776
732
}
@@ -793,17 +749,15 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
793
749
match ty:: get ( ty:: expr_ty ( self . tcx , expr) ) . sty {
794
750
ty:: ty_struct( id, _) => {
795
751
for field in ( * fields) . iter ( ) {
796
- self . check_field ( expr. span , id, field. ident . node ,
797
- None ) ;
752
+ self . check_field ( expr. span , id, field. ident . node ) ;
798
753
}
799
754
}
800
755
ty:: ty_enum( _, _) => {
801
756
match self . tcx . def_map . borrow ( ) . get_copy ( & expr. id ) {
802
- ast:: DefVariant ( enum_id , variant_id, _) => {
757
+ ast:: DefVariant ( _ , variant_id, _) => {
803
758
for field in fields. iter ( ) {
804
759
self . check_field ( expr. span , variant_id,
805
- field. ident . node ,
806
- Some ( enum_id) ) ;
760
+ field. ident . node ) ;
807
761
}
808
762
}
809
763
_ => self . tcx . sess . span_bug ( expr. span ,
@@ -867,16 +821,15 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
867
821
match ty:: get ( ty:: pat_ty ( self . tcx , pattern) ) . sty {
868
822
ty:: ty_struct( id, _) => {
869
823
for field in fields. iter ( ) {
870
- self . check_field ( pattern. span , id, field. ident ,
871
- None ) ;
824
+ self . check_field ( pattern. span , id, field. ident ) ;
872
825
}
873
826
}
874
827
ty:: ty_enum( _, _) => {
875
828
match self . tcx . def_map . borrow ( ) . find ( & pattern. id ) {
876
- Some ( & ast:: DefVariant ( enum_id , variant_id, _) ) => {
829
+ Some ( & ast:: DefVariant ( _ , variant_id, _) ) => {
877
830
for field in fields. iter ( ) {
878
831
self . check_field ( pattern. span , variant_id,
879
- field. ident , Some ( enum_id ) ) ;
832
+ field. ident ) ;
880
833
}
881
834
}
882
835
_ => self . tcx . sess . span_bug ( pattern. span ,
@@ -992,16 +945,10 @@ impl<'a> SanePrivacyVisitor<'a> {
992
945
}
993
946
}
994
947
} ;
995
- let check_struct = |def : & @ast:: StructDef ,
996
- vis : ast:: Visibility ,
997
- parent_vis : Option < ast:: Visibility > | {
998
- let public_def = match vis {
999
- ast:: Public => true ,
1000
- ast:: Inherited | ast:: Private => parent_vis == Some ( ast:: Public ) ,
1001
- } ;
948
+ let check_struct = |def : & @ast:: StructDef | {
1002
949
for f in def. fields . iter ( ) {
1003
- match f. node . kind {
1004
- ast:: NamedField ( _, ast:: Private ) if !public_def => {
950
+ match f. node . kind {
951
+ ast:: NamedField ( _, ast:: Private ) => {
1005
952
tcx. sess . span_err ( f. span , "unnecessary `priv` \
1006
953
visibility") ;
1007
954
}
@@ -1058,15 +1005,13 @@ impl<'a> SanePrivacyVisitor<'a> {
1058
1005
}
1059
1006
1060
1007
match v. node . kind {
1061
- ast:: StructVariantKind ( ref s) => {
1062
- check_struct ( s, v. node . vis , Some ( item. vis ) ) ;
1063
- }
1008
+ ast:: StructVariantKind ( ref s) => check_struct ( s) ,
1064
1009
ast:: TupleVariantKind ( ..) => { }
1065
1010
}
1066
1011
}
1067
1012
}
1068
1013
1069
- ast:: ItemStruct ( ref def, _) => check_struct ( def, item . vis , None ) ,
1014
+ ast:: ItemStruct ( ref def, _) => check_struct ( def) ,
1070
1015
1071
1016
ast:: ItemTrait ( _, _, ref methods) => {
1072
1017
for m in methods. iter ( ) {
@@ -1372,12 +1317,10 @@ impl<'a> Visitor<()> for VisiblePrivateTypesVisitor<'a> {
1372
1317
1373
1318
fn visit_struct_field ( & mut self , s : & ast:: StructField , _: ( ) ) {
1374
1319
match s. node . kind {
1375
- // the only way to get here is by being inside a public
1376
- // struct/enum variant, so the only way to have a private
1377
- // field is with an explicit `priv`.
1378
- ast:: NamedField ( _, ast:: Private ) => { }
1379
-
1380
- _ => visit:: walk_struct_field ( self , s, ( ) )
1320
+ ast:: NamedField ( _, ast:: Public ) => {
1321
+ visit:: walk_struct_field ( self , s, ( ) ) ;
1322
+ }
1323
+ _ => { }
1381
1324
}
1382
1325
}
1383
1326
0 commit comments