Add E0611
This commit is contained in:
parent
0189cec5d4
commit
302f99693f
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
22
src/test/compile-fail/E0611.rs
Normal file
22
src/test/compile-fail/E0611.rs
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue