Auto merge of #43699 - GuillaumeGomez:e0623, r=eddyb
Add missing error code for private method
This commit is contained in:
commit
3de807a00b
@ -312,8 +312,8 @@ macro_rules! report_function {
|
||||
}
|
||||
|
||||
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! {
|
||||
|
22
src/test/compile-fail/E0624.rs
Normal file
22
src/test/compile-fail/E0624.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 inner {
|
||||
pub struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn method(&self) {}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let foo = inner::Foo;
|
||||
foo.method(); //~ ERROR method `method` is private [E0624]
|
||||
}
|
Loading…
Reference in New Issue
Block a user