27
27
extern crate rustc;
28
28
extern crate rustc_front;
29
29
30
- use self :: FieldName :: * ;
31
-
32
30
use std:: cmp;
33
31
use std:: mem:: replace;
34
32
@@ -384,11 +382,6 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
384
382
in_foreign : bool ,
385
383
}
386
384
387
- enum FieldName {
388
- UnnamedField ( usize ) , // index
389
- NamedField ( ast:: Name ) ,
390
- }
391
-
392
385
impl < ' a , ' tcx > PrivacyVisitor < ' a , ' tcx > {
393
386
fn item_is_visible ( & self , did : DefId ) -> bool {
394
387
let visibility = match self . tcx . map . as_local_node_id ( did) {
@@ -407,30 +400,12 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
407
400
}
408
401
409
402
// Checks that a field is in scope.
410
- fn check_field ( & mut self ,
411
- span : Span ,
412
- def : ty:: AdtDef < ' tcx > ,
413
- v : ty:: VariantDef < ' tcx > ,
414
- name : FieldName ) {
415
- let field = match name {
416
- NamedField ( f_name) => v. field_named ( f_name) ,
417
- UnnamedField ( idx) => & v. fields [ idx]
418
- } ;
419
- if field. vis == hir:: Public || self . private_accessible ( def. did ) {
420
- return ;
403
+ fn check_field ( & mut self , span : Span , def : ty:: AdtDef < ' tcx > , field : ty:: FieldDef < ' tcx > ) {
404
+ if def. adt_kind ( ) == ty:: AdtKind :: Struct &&
405
+ field. vis != hir:: Public && !self . private_accessible ( def. did ) {
406
+ span_err ! ( self . tcx. sess, span, E0451 , "field `{}` of struct `{}` is private" ,
407
+ field. name, self . tcx. item_path_str( def. did) ) ;
421
408
}
422
-
423
- let struct_desc = match def. adt_kind ( ) {
424
- ty:: AdtKind :: Struct =>
425
- format ! ( "struct `{}`" , self . tcx. item_path_str( def. did) ) ,
426
- // struct variant fields have inherited visibility
427
- ty:: AdtKind :: Enum => return
428
- } ;
429
- let msg = match name {
430
- NamedField ( name) => format ! ( "field `{}` of {} is private" , name, struct_desc) ,
431
- UnnamedField ( idx) => format ! ( "field #{} of {} is private" , idx, struct_desc) ,
432
- } ;
433
- span_err ! ( self . tcx. sess, span, E0451 , "{}" , msg) ;
434
409
}
435
410
436
411
// Checks that a method is in scope.
@@ -476,7 +451,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
476
451
// Rather than computing the set of unmentioned fields
477
452
// (i.e. `all_fields - fields`), just check them all.
478
453
for field in & variant. fields {
479
- self . check_field ( expr. span , adt, variant , NamedField ( field. name ) ) ;
454
+ self . check_field ( expr. span , adt, field) ;
480
455
}
481
456
}
482
457
hir:: ExprPath ( ..) => {
@@ -518,8 +493,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
518
493
let def = self . tcx . def_map . borrow ( ) . get ( & pattern. id ) . unwrap ( ) . full_def ( ) ;
519
494
let variant = adt. variant_of_def ( def) ;
520
495
for field in fields {
521
- self . check_field ( pattern. span , adt, variant,
522
- NamedField ( field. node . name ) ) ;
496
+ self . check_field ( pattern. span , adt, variant. field_named ( field. node . name ) ) ;
523
497
}
524
498
}
525
499
@@ -532,10 +506,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
532
506
if let PatKind :: Wild = field. node {
533
507
continue
534
508
}
535
- self . check_field ( field. span ,
536
- def,
537
- def. struct_variant ( ) ,
538
- UnnamedField ( i) ) ;
509
+ self . check_field ( field. span , def, & def. struct_variant ( ) . fields [ i] ) ;
539
510
}
540
511
}
541
512
ty:: TyEnum ( ..) => {
0 commit comments