Skip to content

Commit

Permalink
Add E0616
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 15, 2017
1 parent 5bb58bf commit ee60064
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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",
Expand Down
62 changes: 61 additions & 1 deletion src/librustc_typeck/diagnostics.rs
Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions 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 <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
}

0 comments on commit ee60064

Please sign in to comment.