Skip to content

Commit 08188c3

Browse files
Add missing error code for private method
1 parent be69520 commit 08188c3

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/librustc_typeck/check/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
312312
}
313313

314314
MethodError::PrivateMatch(def) => {
315-
let msg = format!("{} `{}` is private", def.kind_name(), item_name);
316-
self.tcx.sess.span_err(span, &msg);
315+
struct_span_err!(self.tcx.sess, span, E0624,
316+
"{} `{}` is private", def.kind_name(), item_name).emit();
317317
}
318318

319319
MethodError::IllegalSizedBound(candidates) => {

src/librustc_typeck/diagnostics.rs

+56
Original file line numberDiff line numberDiff line change
@@ -4644,6 +4644,62 @@ whose implementation is handled specially by the compiler. In order to fix this
46444644
error, just declare a function.
46454645
"##,
46464646

4647+
E0624: r##"
4648+
A private item was used outside of its scope.
4649+
4650+
Erroneous code example:
4651+
4652+
```compile_fail,E0624
4653+
mod inner {
4654+
pub struct Foo;
4655+
4656+
impl Foo {
4657+
fn method(&self) {}
4658+
}
4659+
}
4660+
4661+
let foo = inner::Foo;
4662+
foo.method(); // error: method `method` is private
4663+
```
4664+
4665+
Two possibilities are available to solve this issue:
4666+
4667+
1. Only use the item in the scope it has been defined:
4668+
4669+
```
4670+
mod inner {
4671+
pub struct Foo;
4672+
4673+
impl Foo {
4674+
fn method(&self) {}
4675+
}
4676+
4677+
pub fn call_method(foo: &Foo) { // We create a public function.
4678+
foo.method(); // Which calls the item.
4679+
}
4680+
}
4681+
4682+
let foo = inner::Foo;
4683+
inner::call_method(&foo); // And since the function is public, we can call the
4684+
// method through it.
4685+
```
4686+
4687+
2. Make the item public:
4688+
4689+
```
4690+
mod inner {
4691+
pub struct Foo;
4692+
4693+
impl Foo {
4694+
pub fn method(&self) {} // It's now public.
4695+
}
4696+
}
4697+
4698+
let foo = inner::Foo;
4699+
foo.method(); // Ok!
4700+
```
4701+
"##,
4702+
46474703
}
46484704

46494705
register_diagnostics! {

src/test/compile-fail/E0624.rs

+22
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 inner {
12+
pub struct Foo;
13+
14+
impl Foo {
15+
fn method(&self) {}
16+
}
17+
}
18+
19+
fn main() {
20+
let foo = inner::Foo;
21+
foo.method(); //~ ERROR method `method` is private [E0624]
22+
}

0 commit comments

Comments
 (0)