diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index c12df083c30e..61d140a245aa 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3051,8 +3051,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", idx.node, struct_path); - self.tcx().sess.span_err(expr.span, &msg); + struct_span_err!(self.tcx().sess, expr.span, E0611, + "field `{}` of tuple-struct `{}` is private", + idx.node, struct_path); return field_ty; } diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 76c664d7997a..223206fffc50 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4152,6 +4152,62 @@ println!("x: {}, y: {}", variable.x, variable.y); For more information see The Rust Book: https://doc.rust-lang.org/book/ "##, +E0611: r##" +Attempted to access a private field on a tuple-struct. + +Erroneous code example: + +```compile_fail,E0611 +mod some_module { + pub struct Foo(u32); + + impl Foo { + pub fn new() -> Foo { Foo(0) } + } +} + +let y = some_module::Foo::new(); +println!("{}", y.0); // error: field `0` of tuple-struct `some_module::Foo` + // is private +``` + +Since the field is private, you have two solutions: + +1) Make the field public: + +``` +mod some_module { + pub struct Foo(pub u32); // The field is now public. + + impl Foo { + pub fn new() -> Foo { Foo(0) } + } +} + +let y = some_module::Foo::new(); +println!("{}", y.0); // So we can access it directly. +``` + +2) Add a getter function to keep the field private but allow for accessing its +value: + +``` +mod some_module { + pub struct Foo(u32); + + impl Foo { + pub fn new() -> Foo { Foo(0) } + + // We add the getter function. + pub fn get(&self) -> &u32 { self.0 } + } +} + +let y = some_module::Foo::new(); +println!("{}", y.get()); // So we can get the value through the function. +``` +"##, + E0617: r##" Attempted to pass an invalid type of variable into a variadic function. diff --git a/src/test/compile-fail/E0611.rs b/src/test/compile-fail/E0611.rs new file mode 100644 index 000000000000..1e392d194b1d --- /dev/null +++ b/src/test/compile-fail/E0611.rs @@ -0,0 +1,22 @@ +// 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(u32); + + impl Foo { + pub fn new() -> Foo { Foo(0) } + } +} + +fn main() { + let y = a::Foo::new(); + y.0; //~ ERROR E0611 +}