]> git.lizzy.rs Git - rust.git/commitdiff
name struct in "field `...` is private" error
authorBenjamin Herr <ben@0x539.de>
Sat, 5 Apr 2014 17:28:01 +0000 (19:28 +0200)
committerBenjamin Herr <ben@0x539.de>
Sun, 6 Apr 2014 00:37:25 +0000 (02:37 +0200)
src/librustc/middle/privacy.rs
src/test/auxiliary/privacy-struct-variant.rs [new file with mode: 0644]
src/test/compile-fail/issue-3763.rs
src/test/compile-fail/privacy-struct-variant.rs [new file with mode: 0644]
src/test/compile-fail/privacy5.rs
src/test/compile-fail/private-struct-field-cross-crate.rs
src/test/compile-fail/private-struct-field-ctor.rs
src/test/compile-fail/private-struct-field-pattern.rs
src/test/compile-fail/private-struct-field.rs
src/test/compile-fail/struct-field-privacy.rs

index 9d9faee36450a4fb0e9ddae98e9938c03c06b186..ee514ba5f2438f4e867f2f92fe44d42d8c9fa2e3 100644 (file)
@@ -576,15 +576,37 @@ fn check_field(&mut self, span: Span, id: ast::DefId,
             }
             UnnamedField(idx) => fields.get(idx)
         };
-        if field.vis == ast::Public { return }
-        if !is_local(field.id) || !self.private_accessible(field.id.node) {
-            let msg = match name {
-                NamedField(name) => format!("field `{}` is private",
-                                            token::get_ident(name)),
-                UnnamedField(idx) => format!("field \\#{} is private", idx + 1),
-            };
-            self.tcx.sess.span_err(span, msg);
+        if field.vis == ast::Public ||
+            (is_local(field.id) && self.private_accessible(field.id.node)) {
+            return
         }
+
+        let struct_type = ty::lookup_item_type(self.tcx, id).ty;
+        let struct_desc = match ty::get(struct_type).sty {
+            ty::ty_struct(_, _) => format!("struct `{}`", ty::item_path_str(self.tcx, id)),
+            ty::ty_bare_fn(ty::BareFnTy { sig: ty::FnSig { output, .. }, .. }) => {
+                // Struct `id` is really a struct variant of an enum,
+                // and we're really looking at the variant's constructor
+                // function. So get the return type for a detailed error
+                // message.
+                let enum_id = match ty::get(output).sty {
+                    ty::ty_enum(id, _) => id,
+                    _ => self.tcx.sess.span_bug(span, "enum variant doesn't \
+                                                       belong to an enum")
+                };
+                format!("variant `{}` of enum `{}`",
+                        ty::with_path(self.tcx, id, |mut p| p.last().unwrap()),
+                        ty::item_path_str(self.tcx, enum_id))
+            }
+            _ => self.tcx.sess.span_bug(span, "can't find struct for field")
+        };
+        let msg = match name {
+            NamedField(name) => format!("field `{}` of {} is private",
+                                        token::get_ident(name), struct_desc),
+            UnnamedField(idx) => format!("field \\#{} of {} is private",
+                                         idx + 1, struct_desc),
+        };
+        self.tcx.sess.span_err(span, msg);
     }
 
     // Given the ID of a method, checks to ensure it's in scope.
diff --git a/src/test/auxiliary/privacy-struct-variant.rs b/src/test/auxiliary/privacy-struct-variant.rs
new file mode 100644 (file)
index 0000000..af2e332
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(struct_variant)]
+
+pub enum Foo {
+    Bar {
+        baz: int
+    }
+}
index 736ac55aef545e7256124da3c724df08975f7a15..8eed8aa86441a7d853dea56be6da37d44e54b1ae 100644 (file)
@@ -24,11 +24,15 @@ fn happyfun(&self) {}
 
 fn main() {
     let my_struct = my_mod::MyStruct();
-    let _woohoo = (&my_struct).priv_field; //~ ERROR field `priv_field` is private
-    let _woohoo = (~my_struct).priv_field; //~ ERROR field `priv_field` is private
-    let _woohoo = (@my_struct).priv_field; //~ ERROR field `priv_field` is private
+    let _woohoo = (&my_struct).priv_field;
+    //~^ ERROR field `priv_field` of struct `my_mod::MyStruct` is private
+    let _woohoo = (~my_struct).priv_field;
+    //~^ ERROR field `priv_field` of struct `my_mod::MyStruct` is private
+    let _woohoo = (@my_struct).priv_field;
+    //~^ ERROR field `priv_field` of struct `my_mod::MyStruct` is private
     (&my_struct).happyfun();               //~ ERROR method `happyfun` is private
     (~my_struct).happyfun();               //~ ERROR method `happyfun` is private
     (@my_struct).happyfun();               //~ ERROR method `happyfun` is private
-    let nope = my_struct.priv_field;       //~ ERROR field `priv_field` is private
+    let nope = my_struct.priv_field;
+    //~^ ERROR field `priv_field` of struct `my_mod::MyStruct` is private
 }
diff --git a/src/test/compile-fail/privacy-struct-variant.rs b/src/test/compile-fail/privacy-struct-variant.rs
new file mode 100644 (file)
index 0000000..d6fab32
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// aux-build:privacy-struct-variant.rs
+
+#![feature(struct_variant)]
+
+extern crate other = "privacy-struct-variant";
+
+mod a {
+    pub enum Foo {
+        Bar {
+            baz: int
+        }
+    }
+
+    fn test() {
+        let foo = Bar { baz: 42 };
+
+        let Bar { baz: _ } = foo;
+        match foo { Bar { baz: _ } => {} }
+    }
+}
+
+fn main() {
+    let foo = a::Bar { baz: 42 };
+    //~^ ERROR: field `baz` of variant `Bar` of enum `a::Foo` is private
+
+    let a::Bar { baz: _ } = foo;
+    //~^ ERROR: field `baz` of variant `Bar` of enum `a::Foo` is private
+    match foo { a::Bar { baz: _ } => {} }
+    //~^ ERROR: field `baz` of variant `Bar` of enum `a::Foo` is private
+    //
+    let foo = other::Bar { baz: 42 };
+    //~^ ERROR: field `baz` of variant `Bar` of enum `privacy-struct-variant::Foo` is private
+
+    let other::Bar { baz: _ } = foo;
+    //~^ ERROR: field `baz` of variant `Bar` of enum `privacy-struct-variant::Foo` is private
+    match foo { other::Bar { baz: _ } => {} }
+    //~^ ERROR: field `baz` of variant `Bar` of enum `privacy-struct-variant::Foo` is private
+}
index c057236265e94ad218c05fa6cac2671517e4a539..2683022c4c823b2143b1e8c0c104ed38dbd54929 100644 (file)
@@ -64,25 +64,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 #1 is private
+    let a::A(()) = a; //~ ERROR: field #1 of struct `a::A` is private
     let a::A(_) = a;
-    match a { a::A(()) => {} } //~ ERROR: field #1 is private
+    match a { a::A(()) => {} } //~ ERROR: field #1 of struct `a::A` is private
     match a { a::A(_) => {} }
 
     let a::B(_) = b;
-    let a::B(_b) = b; //~ ERROR: field #1 is private
+    let a::B(_b) = b; //~ ERROR: field #1 of struct `a::B` is private
     match b { a::B(_) => {} }
-    match b { a::B(_b) => {} } //~ ERROR: field #1 is private
-    match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field #1 is private
+    match b { a::B(_b) => {} } //~ ERROR: field #1 of struct `a::B` is private
+    match b { a::B(1) => {} a::B(_) => {} } //~ ERROR: field #1 of struct `a::B` is private
 
     let a::C(_, _) = c;
     let a::C(_a, _) = c;
-    let a::C(_, _b) = c; //~ ERROR: field #2 is private
-    let a::C(_a, _b) = c; //~ ERROR: field #2 is private
+    let a::C(_, _b) = c; //~ ERROR: field #2 of struct `a::C` is private
+    let a::C(_a, _b) = c; //~ ERROR: field #2 of struct `a::C` is private
     match c { a::C(_, _) => {} }
     match c { a::C(_a, _) => {} }
-    match c { a::C(_, _b) => {} } //~ ERROR: field #2 is private
-    match c { a::C(_a, _b) => {} } //~ ERROR: field #2 is private
+    match c { a::C(_, _b) => {} } //~ ERROR: field #2 of struct `a::C` is private
+    match c { a::C(_a, _b) => {} } //~ ERROR: field #2 of struct `a::C` is private
 
     let a::D(_) = d;
     let a::D(_d) = d;
@@ -102,25 +102,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 #1 is private
+    let other::A(()) = a; //~ ERROR: field #1 of struct `privacy-tuple-struct::A` is private
     let other::A(_) = a;
-    match a { other::A(()) => {} } //~ ERROR: field #1 is private
+    match a { other::A(()) => {} }
+    //~^ ERROR: field #1 of struct `privacy-tuple-struct::A` is private
     match a { other::A(_) => {} }
 
     let other::B(_) = b;
-    let other::B(_b) = b; //~ ERROR: field #1 is private
+    let other::B(_b) = b; //~ ERROR: field #1 of struct `privacy-tuple-struct::B` is private
     match b { other::B(_) => {} }
-    match b { other::B(_b) => {} } //~ ERROR: field #1 is private
-    match b { other::B(1) => {} other::B(_) => {} } //~ ERROR: field #1 is private
+    match b { other::B(_b) => {} }
+    //~^ ERROR: field #1 of struct `privacy-tuple-struct::B` is private
+    match b { other::B(1) => {} other::B(_) => {} }
+    //~^ ERROR: field #1 of struct `privacy-tuple-struct::B` is private
 
     let other::C(_, _) = c;
     let other::C(_a, _) = c;
-    let other::C(_, _b) = c; //~ ERROR: field #2 is private
-    let other::C(_a, _b) = c; //~ ERROR: field #2 is private
+    let other::C(_, _b) = c; //~ ERROR: field #2 of struct `privacy-tuple-struct::C` is private
+    let other::C(_a, _b) = c; //~ ERROR: field #2 of struct `privacy-tuple-struct::C` is private
     match c { other::C(_, _) => {} }
     match c { other::C(_a, _) => {} }
-    match c { other::C(_, _b) => {} } //~ ERROR: field #2 is private
-    match c { other::C(_a, _b) => {} } //~ ERROR: field #2 is private
+    match c { other::C(_, _b) => {} }
+    //~^ ERROR: field #2 of struct `privacy-tuple-struct::C` is private
+    match c { other::C(_a, _b) => {} }
+    //~^ ERROR: field #2 of struct `privacy-tuple-struct::C` is private
 
     let other::D(_) = d;
     let other::D(_d) = d;
index 0993ac9a64ad377c01912cb5cc59ccf83313ce83..45c4b2b7ae1b0aa038d9c696dafd9761382a009a 100644 (file)
@@ -14,5 +14,6 @@
 
 fn main() {
   let nyan : cat = cat(52u, 99);
-  assert!((nyan.meows == 52u));   //~ ERROR field `meows` is private
+  assert!((nyan.meows == 52u));
+  //~^ ERROR field `meows` of struct `cci_class::kitties::cat` is private
 }
index a5cee47d4e39a30dee2d355a9e07a03bac787363..ae19c22149669d48fb1c971f51f070633a2723a0 100644 (file)
@@ -15,5 +15,5 @@ pub struct Foo {
 }
 
 fn main() {
-    let s = a::Foo { x: 1 };    //~ ERROR field `x` is private
+    let s = a::Foo { x: 1 };    //~ ERROR field `x` of struct `a::Foo` is private
 }
index ee1303b99dd52dd398b3d390cc7bedd2cc6488a3..991457ef1ce7ca24eade6d402ab3c9f99ebfaa3e 100644 (file)
@@ -22,6 +22,6 @@ pub fn make() -> Foo {
 
 fn main() {
     match a::make() {
-        Foo { x: _ } => {}  //~ ERROR field `x` is private
+        Foo { x: _ } => {}  //~ ERROR field `x` of struct `a::Foo` is private
     }
 }
index 52e979342f57df732e3fe9b1096996269e388eca..3f6fa573cc0b56301e5ba849b98aa1b642d944a0 100644 (file)
@@ -20,5 +20,5 @@ pub fn new_cat() -> Cat {
 
 fn main() {
     let nyan = cat::new_cat();
-    assert!(nyan.meows == 52);    //~ ERROR field `meows` is private
+    assert!(nyan.meows == 52);    //~ ERROR field `meows` of struct `cat::Cat` is private
 }
index b6ae7235bb3de75e1e09200550519bd3c53acedc..82ba9a02a304aeaac98ea2dce1f2f1b65d3ebbb0 100644 (file)
@@ -32,16 +32,16 @@ fn test(a: A, b: inner::A, c: inner::B, d: xc::A, e: xc::B) {
     //~^^ ERROR: struct `A` is private
 
     a.a;
-    b.a; //~ ERROR: field `a` is private
+    b.a; //~ ERROR: field `a` of struct `inner::A` is private
     b.b;
     c.a;
-    c.b; //~ ERROR: field `b` is private
+    c.b; //~ ERROR: field `b` of struct `inner::B` is private
 
-    d.a; //~ ERROR: field `a` is private
+    d.a; //~ ERROR: field `a` of struct `struct-field-privacy::A` is private
     d.b;
 
     e.a;
-    e.b; //~ ERROR: field `b` is private
+    e.b; //~ ERROR: field `b` of struct `struct-field-privacy::B` is private
 }
 
 fn main() {}