Skip to content

Add missing error code for private method #43699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}

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) => {
Expand Down
56 changes: 56 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4644,6 +4644,62 @@ whose implementation is handled specially by the compiler. In order to fix this
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! {
Expand Down
22 changes: 22 additions & 0 deletions src/test/compile-fail/E0624.rs
Original file line number Diff line number Diff line change
@@ -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]
}