about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_privacy/lib.rs45
-rw-r--r--src/test/compile-fail/privacy5.rs36
2 files changed, 26 insertions, 55 deletions
diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs
index 5bef33af452..d4b309fb039 100644
--- a/src/librustc_privacy/lib.rs
+++ b/src/librustc_privacy/lib.rs
@@ -27,8 +27,6 @@
 extern crate rustc;
 extern crate rustc_front;
 
-use self::FieldName::*;
-
 use std::cmp;
 use std::mem::replace;
 
@@ -384,11 +382,6 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
     in_foreign: bool,
 }
 
-enum FieldName {
-    UnnamedField(usize), // index
-    NamedField(ast::Name),
-}
-
 impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
     fn item_is_visible(&self, did: DefId) -> bool {
         let visibility = match self.tcx.map.as_local_node_id(did) {
@@ -407,30 +400,12 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
     }
 
     // Checks that a field is in scope.
-    fn check_field(&mut self,
-                   span: Span,
-                   def: ty::AdtDef<'tcx>,
-                   v: ty::VariantDef<'tcx>,
-                   name: FieldName) {
-        let field = match name {
-            NamedField(f_name) => v.field_named(f_name),
-            UnnamedField(idx) => &v.fields[idx]
-        };
-        if field.vis == hir::Public || self.private_accessible(def.did) {
-            return;
+    fn check_field(&mut self, span: Span, def: ty::AdtDef<'tcx>, field: ty::FieldDef<'tcx>) {
+        if def.adt_kind() == ty::AdtKind::Struct &&
+                field.vis != hir::Public && !self.private_accessible(def.did) {
+            span_err!(self.tcx.sess, span, E0451, "field `{}` of struct `{}` is private",
+                      field.name, self.tcx.item_path_str(def.did));
         }
-
-        let struct_desc = match def.adt_kind() {
-            ty::AdtKind::Struct =>
-                format!("struct `{}`", self.tcx.item_path_str(def.did)),
-            // struct variant fields have inherited visibility
-            ty::AdtKind::Enum => return
-        };
-        let msg = match name {
-            NamedField(name) => format!("field `{}` of {} is private", name, struct_desc),
-            UnnamedField(idx) => format!("field #{} of {} is private", idx, struct_desc),
-        };
-        span_err!(self.tcx.sess, span, E0451, "{}", msg);
     }
 
     // Checks that a method is in scope.
@@ -476,7 +451,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
                 // Rather than computing the set of unmentioned fields
                 // (i.e. `all_fields - fields`), just check them all.
                 for field in &variant.fields {
-                    self.check_field(expr.span, adt, variant, NamedField(field.name));
+                    self.check_field(expr.span, adt, field);
                 }
             }
             hir::ExprPath(..) => {
@@ -518,8 +493,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
                 let def = self.tcx.def_map.borrow().get(&pattern.id).unwrap().full_def();
                 let variant = adt.variant_of_def(def);
                 for field in fields {
-                    self.check_field(pattern.span, adt, variant,
-                                     NamedField(field.node.name));
+                    self.check_field(pattern.span, adt, variant.field_named(field.node.name));
                 }
             }
 
@@ -532,10 +506,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
                             if let PatKind::Wild = field.node {
                                 continue
                             }
-                            self.check_field(field.span,
-                                             def,
-                                             def.struct_variant(),
-                                             UnnamedField(i));
+                            self.check_field(field.span, def, &def.struct_variant().fields[i]);
                         }
                     }
                     ty::TyEnum(..) => {
diff --git a/src/test/compile-fail/privacy5.rs b/src/test/compile-fail/privacy5.rs
index 588c9be3065..9d6ae187cd3 100644
--- a/src/test/compile-fail/privacy5.rs
+++ b/src/test/compile-fail/privacy5.rs
@@ -63,25 +63,25 @@ fn this_crate() {
     let c = a::C(2, 3); //~ ERROR: cannot invoke tuple struct constructor
     let d = a::D(4);
 
-    let a::A(()) = a; //~ ERROR: field #0 of struct `a::A` is private
+    let a::A(()) = a; //~ ERROR: field `0` of struct `a::A` is private
     let a::A(_) = a;
-    match a { a::A(()) => {} } //~ ERROR: field #0 of struct `a::A` is private
+    match a { a::A(()) => {} } //~ ERROR: field `0` of struct `a::A` is private
     match a { a::A(_) => {} }
 
     let a::B(_) = b;
-    let a::B(_b) = b; //~ ERROR: field #0 of struct `a::B` is private
+    let a::B(_b) = b; //~ ERROR: field `0` of struct `a::B` is private
     match b { a::B(_) => {} }
-    match b { a::B(_b) => {} } //~ ERROR: field #0 of struct `a::B` is private
-    match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field #0 of struct `a::B` is private
+    match b { a::B(_b) => {} } //~ ERROR: field `0` of struct `a::B` is private
+    match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field `0` of struct `a::B` is private
 
     let a::C(_, _) = c;
     let a::C(_a, _) = c;
-    let a::C(_, _b) = c; //~ ERROR: field #1 of struct `a::C` is private
-    let a::C(_a, _b) = c; //~ ERROR: field #1 of struct `a::C` is private
+    let a::C(_, _b) = c; //~ ERROR: field `1` of struct `a::C` is private
+    let a::C(_a, _b) = c; //~ ERROR: field `1` of struct `a::C` is private
     match c { a::C(_, _) => {} }
     match c { a::C(_a, _) => {} }
-    match c { a::C(_, _b) => {} } //~ ERROR: field #1 of struct `a::C` is private
-    match c { a::C(_a, _b) => {} } //~ ERROR: field #1 of struct `a::C` is private
+    match c { a::C(_, _b) => {} } //~ ERROR: field `1` of struct `a::C` is private
+    match c { a::C(_a, _b) => {} } //~ ERROR: field `1` of struct `a::C` is private
 
     let a::D(_) = d;
     let a::D(_d) = d;
@@ -101,30 +101,30 @@ fn xcrate() {
     let c = other::C(2, 3); //~ ERROR: cannot invoke tuple struct constructor
     let d = other::D(4);
 
-    let other::A(()) = a; //~ ERROR: field #0 of struct `other::A` is private
+    let other::A(()) = a; //~ ERROR: field `0` of struct `other::A` is private
     let other::A(_) = a;
     match a { other::A(()) => {} }
-    //~^ ERROR: field #0 of struct `other::A` is private
+    //~^ ERROR: field `0` of struct `other::A` is private
     match a { other::A(_) => {} }
 
     let other::B(_) = b;
-    let other::B(_b) = b; //~ ERROR: field #0 of struct `other::B` is private
+    let other::B(_b) = b; //~ ERROR: field `0` of struct `other::B` is private
     match b { other::B(_) => {} }
     match b { other::B(_b) => {} }
-    //~^ ERROR: field #0 of struct `other::B` is private
+    //~^ ERROR: field `0` of struct `other::B` is private
     match b { other::B(1) => {} other::B(_) => {} }
-    //~^ ERROR: field #0 of struct `other::B` is private
+    //~^ ERROR: field `0` of struct `other::B` is private
 
     let other::C(_, _) = c;
     let other::C(_a, _) = c;
-    let other::C(_, _b) = c; //~ ERROR: field #1 of struct `other::C` is private
-    let other::C(_a, _b) = c; //~ ERROR: field #1 of struct `other::C` is private
+    let other::C(_, _b) = c; //~ ERROR: field `1` of struct `other::C` is private
+    let other::C(_a, _b) = c; //~ ERROR: field `1` of struct `other::C` is private
     match c { other::C(_, _) => {} }
     match c { other::C(_a, _) => {} }
     match c { other::C(_, _b) => {} }
-    //~^ ERROR: field #1 of struct `other::C` is private
+    //~^ ERROR: field `1` of struct `other::C` is private
     match c { other::C(_a, _b) => {} }
-    //~^ ERROR: field #1 of struct `other::C` is private
+    //~^ ERROR: field `1` of struct `other::C` is private
 
     let other::D(_) = d;
     let other::D(_d) = d;