Skip to content

Commit

Permalink
Add E0611
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 15, 2017
1 parent 0189cec commit 302f996
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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;
}

Expand Down
56 changes: 56 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions 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 <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(u32);

impl Foo {
pub fn new() -> Foo { Foo(0) }
}
}

fn main() {
let y = a::Foo::new();
y.0; //~ ERROR E0611
}

0 comments on commit 302f996

Please sign in to comment.