Skip to content

Commit ee60064

Browse files
Add E0616
1 parent 5bb58bf commit ee60064

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/librustc_typeck/check/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2921,8 +2921,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29212921

29222922
if let Some((did, field_ty)) = private_candidate {
29232923
let struct_path = self.tcx().item_path_str(did);
2924-
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2925-
let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
2924+
let mut err = struct_span_err!(self.tcx().sess, expr.span, E0616,
2925+
"field `{}` of struct `{}` is private",
2926+
field.node, struct_path);
29262927
// Also check if an accessible method exists, which is often what is meant.
29272928
if self.method_exists(field.span, field.node, expr_t, expr.id, false) {
29282929
err.note(&format!("a method `{}` also exists, perhaps you wish to call it",

src/librustc_typeck/diagnostics.rs

+61-1
Original file line numberDiff line numberDiff line change
@@ -4199,7 +4199,7 @@ mod some_module {
41994199
pub fn new() -> Foo { Foo(0) }
42004200
42014201
// We add the getter function.
4202-
pub fn get(&self) -> &u32 { self.0 }
4202+
pub fn get(&self) -> &u32 { &self.0 }
42034203
}
42044204
}
42054205
@@ -4339,6 +4339,66 @@ println!("{}", f.x);
43394339
```
43404340
"##,
43414341

4342+
E0616: r##"
4343+
Attempted to access a private field on a struct.
4344+
4345+
Erroneous code example:
4346+
4347+
```compile_fail,E0616
4348+
mod some_module {
4349+
pub struct Foo {
4350+
x: u32, // So `x` is private in here.
4351+
}
4352+
4353+
impl Foo {
4354+
pub fn new() -> Foo { Foo { x: 0 } }
4355+
}
4356+
}
4357+
4358+
let f = some_module::Foo::new();
4359+
println!("{}", f.x); // error: field `x` of struct `some_module::Foo` is private
4360+
```
4361+
4362+
If you want to access this field, you have two options:
4363+
4364+
1) Set the field public:
4365+
4366+
```
4367+
mod some_module {
4368+
pub struct Foo {
4369+
pub x: u32, // `x` is now public.
4370+
}
4371+
4372+
impl Foo {
4373+
pub fn new() -> Foo { Foo { x: 0 } }
4374+
}
4375+
}
4376+
4377+
let f = some_module::Foo::new();
4378+
println!("{}", f.x); // ok!
4379+
```
4380+
4381+
2) Add a getter function:
4382+
4383+
```
4384+
mod some_module {
4385+
pub struct Foo {
4386+
x: u32, // So `x` is still private in here.
4387+
}
4388+
4389+
impl Foo {
4390+
pub fn new() -> Foo { Foo { x: 0 } }
4391+
4392+
// We create the getter function here:
4393+
pub fn get_x(&self) -> &u32 { &self.x }
4394+
}
4395+
}
4396+
4397+
let f = some_module::Foo::new();
4398+
println!("{}", f.get_x()); // ok!
4399+
```
4400+
"##,
4401+
43424402
E0617: r##"
43434403
Attempted to pass an invalid type of variable into a variadic function.
43444404

src/test/compile-fail/E0616.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod a {
12+
pub struct Foo {
13+
x: u32,
14+
}
15+
16+
impl Foo {
17+
pub fn new() -> Foo { Foo { x: 0 } }
18+
}
19+
}
20+
21+
fn main() {
22+
let f = a::Foo::new();
23+
f.x; //~ ERROR E0616
24+
}

0 commit comments

Comments
 (0)