Skip to content

Commit 302f996

Browse files
Add E0611
1 parent 0189cec commit 302f996

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,8 +3051,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30513051

30523052
if let Some((did, field_ty)) = private_candidate {
30533053
let struct_path = self.tcx().item_path_str(did);
3054-
let msg = format!("field `{}` of struct `{}` is private", idx.node, struct_path);
3055-
self.tcx().sess.span_err(expr.span, &msg);
3054+
struct_span_err!(self.tcx().sess, expr.span, E0611,
3055+
"field `{}` of tuple-struct `{}` is private",
3056+
idx.node, struct_path);
30563057
return field_ty;
30573058
}
30583059

src/librustc_typeck/diagnostics.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,6 +4152,62 @@ println!("x: {}, y: {}", variable.x, variable.y);
41524152
For more information see The Rust Book: https://doc.rust-lang.org/book/
41534153
"##,
41544154

4155+
E0611: r##"
4156+
Attempted to access a private field on a tuple-struct.
4157+
4158+
Erroneous code example:
4159+
4160+
```compile_fail,E0611
4161+
mod some_module {
4162+
pub struct Foo(u32);
4163+
4164+
impl Foo {
4165+
pub fn new() -> Foo { Foo(0) }
4166+
}
4167+
}
4168+
4169+
let y = some_module::Foo::new();
4170+
println!("{}", y.0); // error: field `0` of tuple-struct `some_module::Foo`
4171+
// is private
4172+
```
4173+
4174+
Since the field is private, you have two solutions:
4175+
4176+
1) Make the field public:
4177+
4178+
```
4179+
mod some_module {
4180+
pub struct Foo(pub u32); // The field is now public.
4181+
4182+
impl Foo {
4183+
pub fn new() -> Foo { Foo(0) }
4184+
}
4185+
}
4186+
4187+
let y = some_module::Foo::new();
4188+
println!("{}", y.0); // So we can access it directly.
4189+
```
4190+
4191+
2) Add a getter function to keep the field private but allow for accessing its
4192+
value:
4193+
4194+
```
4195+
mod some_module {
4196+
pub struct Foo(u32);
4197+
4198+
impl Foo {
4199+
pub fn new() -> Foo { Foo(0) }
4200+
4201+
// We add the getter function.
4202+
pub fn get(&self) -> &u32 { self.0 }
4203+
}
4204+
}
4205+
4206+
let y = some_module::Foo::new();
4207+
println!("{}", y.get()); // So we can get the value through the function.
4208+
```
4209+
"##,
4210+
41554211
E0617: r##"
41564212
Attempted to pass an invalid type of variable into a variadic function.
41574213

src/test/compile-fail/E0611.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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(u32);
13+
14+
impl Foo {
15+
pub fn new() -> Foo { Foo(0) }
16+
}
17+
}
18+
19+
fn main() {
20+
let y = a::Foo::new();
21+
y.0; //~ ERROR E0611
22+
}

0 commit comments

Comments
 (0)