Skip to content

Commit 3ae768d

Browse files
committed
privacy: Cleanup check_field
1 parent 6166bd7 commit 3ae768d

File tree

2 files changed

+26
-55
lines changed

2 files changed

+26
-55
lines changed

src/librustc_privacy/lib.rs

+8-37
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
extern crate rustc;
2828
extern crate rustc_front;
2929

30-
use self::FieldName::*;
31-
3230
use std::cmp;
3331
use std::mem::replace;
3432

@@ -384,11 +382,6 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
384382
in_foreign: bool,
385383
}
386384

387-
enum FieldName {
388-
UnnamedField(usize), // index
389-
NamedField(ast::Name),
390-
}
391-
392385
impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
393386
fn item_is_visible(&self, did: DefId) -> bool {
394387
let visibility = match self.tcx.map.as_local_node_id(did) {
@@ -407,30 +400,12 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
407400
}
408401

409402
// 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));
421408
}
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);
434409
}
435410

436411
// Checks that a method is in scope.
@@ -476,7 +451,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
476451
// Rather than computing the set of unmentioned fields
477452
// (i.e. `all_fields - fields`), just check them all.
478453
for field in &variant.fields {
479-
self.check_field(expr.span, adt, variant, NamedField(field.name));
454+
self.check_field(expr.span, adt, field);
480455
}
481456
}
482457
hir::ExprPath(..) => {
@@ -518,8 +493,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
518493
let def = self.tcx.def_map.borrow().get(&pattern.id).unwrap().full_def();
519494
let variant = adt.variant_of_def(def);
520495
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));
523497
}
524498
}
525499

@@ -532,10 +506,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
532506
if let PatKind::Wild = field.node {
533507
continue
534508
}
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]);
539510
}
540511
}
541512
ty::TyEnum(..) => {

src/test/compile-fail/privacy5.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ fn this_crate() {
6363
let c = a::C(2, 3); //~ ERROR: cannot invoke tuple struct constructor
6464
let d = a::D(4);
6565

66-
let a::A(()) = a; //~ ERROR: field #0 of struct `a::A` is private
66+
let a::A(()) = a; //~ ERROR: field `0` of struct `a::A` is private
6767
let a::A(_) = a;
68-
match a { a::A(()) => {} } //~ ERROR: field #0 of struct `a::A` is private
68+
match a { a::A(()) => {} } //~ ERROR: field `0` of struct `a::A` is private
6969
match a { a::A(_) => {} }
7070

7171
let a::B(_) = b;
72-
let a::B(_b) = b; //~ ERROR: field #0 of struct `a::B` is private
72+
let a::B(_b) = b; //~ ERROR: field `0` of struct `a::B` is private
7373
match b { a::B(_) => {} }
74-
match b { a::B(_b) => {} } //~ ERROR: field #0 of struct `a::B` is private
75-
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field #0 of struct `a::B` is private
74+
match b { a::B(_b) => {} } //~ ERROR: field `0` of struct `a::B` is private
75+
match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field `0` of struct `a::B` is private
7676

7777
let a::C(_, _) = c;
7878
let a::C(_a, _) = c;
79-
let a::C(_, _b) = c; //~ ERROR: field #1 of struct `a::C` is private
80-
let a::C(_a, _b) = c; //~ ERROR: field #1 of struct `a::C` is private
79+
let a::C(_, _b) = c; //~ ERROR: field `1` of struct `a::C` is private
80+
let a::C(_a, _b) = c; //~ ERROR: field `1` of struct `a::C` is private
8181
match c { a::C(_, _) => {} }
8282
match c { a::C(_a, _) => {} }
83-
match c { a::C(_, _b) => {} } //~ ERROR: field #1 of struct `a::C` is private
84-
match c { a::C(_a, _b) => {} } //~ ERROR: field #1 of struct `a::C` is private
83+
match c { a::C(_, _b) => {} } //~ ERROR: field `1` of struct `a::C` is private
84+
match c { a::C(_a, _b) => {} } //~ ERROR: field `1` of struct `a::C` is private
8585

8686
let a::D(_) = d;
8787
let a::D(_d) = d;
@@ -101,30 +101,30 @@ fn xcrate() {
101101
let c = other::C(2, 3); //~ ERROR: cannot invoke tuple struct constructor
102102
let d = other::D(4);
103103

104-
let other::A(()) = a; //~ ERROR: field #0 of struct `other::A` is private
104+
let other::A(()) = a; //~ ERROR: field `0` of struct `other::A` is private
105105
let other::A(_) = a;
106106
match a { other::A(()) => {} }
107-
//~^ ERROR: field #0 of struct `other::A` is private
107+
//~^ ERROR: field `0` of struct `other::A` is private
108108
match a { other::A(_) => {} }
109109

110110
let other::B(_) = b;
111-
let other::B(_b) = b; //~ ERROR: field #0 of struct `other::B` is private
111+
let other::B(_b) = b; //~ ERROR: field `0` of struct `other::B` is private
112112
match b { other::B(_) => {} }
113113
match b { other::B(_b) => {} }
114-
//~^ ERROR: field #0 of struct `other::B` is private
114+
//~^ ERROR: field `0` of struct `other::B` is private
115115
match b { other::B(1) => {} other::B(_) => {} }
116-
//~^ ERROR: field #0 of struct `other::B` is private
116+
//~^ ERROR: field `0` of struct `other::B` is private
117117

118118
let other::C(_, _) = c;
119119
let other::C(_a, _) = c;
120-
let other::C(_, _b) = c; //~ ERROR: field #1 of struct `other::C` is private
121-
let other::C(_a, _b) = c; //~ ERROR: field #1 of struct `other::C` is private
120+
let other::C(_, _b) = c; //~ ERROR: field `1` of struct `other::C` is private
121+
let other::C(_a, _b) = c; //~ ERROR: field `1` of struct `other::C` is private
122122
match c { other::C(_, _) => {} }
123123
match c { other::C(_a, _) => {} }
124124
match c { other::C(_, _b) => {} }
125-
//~^ ERROR: field #1 of struct `other::C` is private
125+
//~^ ERROR: field `1` of struct `other::C` is private
126126
match c { other::C(_a, _b) => {} }
127-
//~^ ERROR: field #1 of struct `other::C` is private
127+
//~^ ERROR: field `1` of struct `other::C` is private
128128

129129
let other::D(_) = d;
130130
let other::D(_d) = d;

0 commit comments

Comments
 (0)