]> git.lizzy.rs Git - rust.git/commitdiff
Add E0616
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 12 Jun 2017 21:53:12 +0000 (23:53 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 15 Jun 2017 08:30:26 +0000 (10:30 +0200)
src/librustc_typeck/check/mod.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0616.rs [new file with mode: 0644]

index 1de75fcddd9c2f82bc98fd6cd906ed7f90b9cee4..b8016fd348207047070cd40eb1fdd3c10ce567fb 100644 (file)
@@ -2921,8 +2921,9 @@ fn check_field(&self,
 
         if let Some((did, field_ty)) = private_candidate {
             let struct_path = self.tcx().item_path_str(did);
-            let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
-            let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
+            let mut err = struct_span_err!(self.tcx().sess, expr.span, E0616,
+                                           "field `{}` of struct `{}` is private",
+                                           field.node, struct_path);
             // Also check if an accessible method exists, which is often what is meant.
             if self.method_exists(field.span, field.node, expr_t, expr.id, false) {
                 err.note(&format!("a method `{}` also exists, perhaps you wish to call it",
index b00f6f3000928696bf9797b4ec2e579311d1ff71..2d8d496cff9e3b4db84edd942f85c367f0a4ca27 100644 (file)
@@ -4199,7 +4199,7 @@ impl Foo {
         pub fn new() -> Foo { Foo(0) }
 
         // We add the getter function.
-        pub fn get(&self) -> &u32 { self.0 }
+        pub fn get(&self) -> &u32 { &self.0 }
     }
 }
 
@@ -4339,6 +4339,66 @@ fn method(&self) {}
 ```
 "##,
 
+E0616: r##"
+Attempted to access a private field on a struct.
+
+Erroneous code example:
+
+```compile_fail,E0616
+mod some_module {
+    pub struct Foo {
+        x: u32, // So `x` is private in here.
+    }
+
+    impl Foo {
+        pub fn new() -> Foo { Foo { x: 0 } }
+    }
+}
+
+let f = some_module::Foo::new();
+println!("{}", f.x); // error: field `x` of struct `some_module::Foo` is private
+```
+
+If you want to access this field, you have two options:
+
+1) Set the field public:
+
+```
+mod some_module {
+    pub struct Foo {
+        pub x: u32, // `x` is now public.
+    }
+
+    impl Foo {
+        pub fn new() -> Foo { Foo { x: 0 } }
+    }
+}
+
+let f = some_module::Foo::new();
+println!("{}", f.x); // ok!
+```
+
+2) Add a getter function:
+
+```
+mod some_module {
+    pub struct Foo {
+        x: u32, // So `x` is still private in here.
+    }
+
+    impl Foo {
+        pub fn new() -> Foo { Foo { x: 0 } }
+
+        // We create the getter function here:
+        pub fn get_x(&self) -> &u32 { &self.x }
+    }
+}
+
+let f = some_module::Foo::new();
+println!("{}", f.get_x()); // ok!
+```
+"##,
+
 E0617: r##"
 Attempted to pass an invalid type of variable into a variadic function.
 
diff --git a/src/test/compile-fail/E0616.rs b/src/test/compile-fail/E0616.rs
new file mode 100644 (file)
index 0000000..2fd9f94
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2017 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.
+
+mod a {
+    pub struct Foo {
+        x: u32,
+    }
+
+    impl Foo {
+        pub fn new() -> Foo { Foo { x: 0 } }
+    }
+}
+
+fn main() {
+    let f = a::Foo::new();
+    f.x; //~ ERROR E0616
+}