提交 08188c3c 编写于 作者: G Guillaume Gomez

Add missing error code for private method

上级 be69520b
......@@ -312,8 +312,8 @@ pub fn report_method_error(&self,
}
MethodError::PrivateMatch(def) => {
let msg = format!("{} `{}` is private", def.kind_name(), item_name);
self.tcx.sess.span_err(span, &msg);
struct_span_err!(self.tcx.sess, span, E0624,
"{} `{}` is private", def.kind_name(), item_name).emit();
}
MethodError::IllegalSizedBound(candidates) => {
......
......@@ -4644,6 +4644,62 @@ fn i_am_a_function() {}
error, just declare a function.
"##,
E0624: r##"
A private item was used outside of its scope.
Erroneous code example:
```compile_fail,E0624
mod inner {
pub struct Foo;
impl Foo {
fn method(&self) {}
}
}
let foo = inner::Foo;
foo.method(); // error: method `method` is private
```
Two possibilities are available to solve this issue:
1. Only use the item in the scope it has been defined:
```
mod inner {
pub struct Foo;
impl Foo {
fn method(&self) {}
}
pub fn call_method(foo: &Foo) { // We create a public function.
foo.method(); // Which calls the item.
}
}
let foo = inner::Foo;
inner::call_method(&foo); // And since the function is public, we can call the
// method through it.
```
2. Make the item public:
```
mod inner {
pub struct Foo;
impl Foo {
pub fn method(&self) {} // It's now public.
}
}
let foo = inner::Foo;
foo.method(); // Ok!
```
"##,
}
register_diagnostics! {
......
// 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 inner {
pub struct Foo;
impl Foo {
fn method(&self) {}
}
}
fn main() {
let foo = inner::Foo;
foo.method(); //~ ERROR method `method` is private [E0624]
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册