diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 1de75fcddd9c2..b8016fd348207 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2921,8 +2921,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { 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", diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index b00f6f3000928..2d8d496cff9e3 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4199,7 +4199,7 @@ mod some_module { 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 @@ println!("{}", f.x); ``` "##, +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 index 0000000000000..2fd9f94763d8d --- /dev/null +++ b/src/test/compile-fail/E0616.rs @@ -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 or the MIT license +// , 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 +}