Skip to content

Improve error message when duplicate names for type and trait method #43737

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 10, 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
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ pub enum MethodError<'tcx> {
ClosureAmbiguity(// DefId of fn trait
DefId),

// Found an applicable method, but it is not visible.
PrivateMatch(Def),
// Found an applicable method, but it is not visible. The second argument contains a list of
// not-in-scope traits which may work.
PrivateMatch(Def, Vec<DefId>),

// Found a `Self: Sized` bound where `Self` is a trait object, also the caller may have
// forgotten to import a trait.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
};

if let Some(def) = private_candidate {
return Err(MethodError::PrivateMatch(def));
return Err(MethodError::PrivateMatch(def, out_of_scope_traits));
}

Err(MethodError::NoMatch(NoMatchData::new(static_candidates,
Expand Down
32 changes: 22 additions & 10 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
self.sess().span_err(span, &msg);
}

MethodError::PrivateMatch(def) => {
struct_span_err!(self.tcx.sess, span, E0624,
"{} `{}` is private", def.kind_name(), item_name).emit();
MethodError::PrivateMatch(def, out_of_scope_traits) => {
let mut err = struct_span_err!(self.tcx.sess, span, E0624,
"{} `{}` is private", def.kind_name(), item_name);
self.suggest_valid_traits(&mut err, out_of_scope_traits);
err.emit();
}

MethodError::IllegalSizedBound(candidates) => {
Expand Down Expand Up @@ -353,13 +355,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.note(&msg[..]);
}

fn suggest_traits_to_import(&self,
err: &mut DiagnosticBuilder,
span: Span,
rcvr_ty: Ty<'tcx>,
item_name: ast::Name,
rcvr_expr: Option<&hir::Expr>,
valid_out_of_scope_traits: Vec<DefId>) {
fn suggest_valid_traits(&self,
err: &mut DiagnosticBuilder,
valid_out_of_scope_traits: Vec<DefId>) -> bool {
if !valid_out_of_scope_traits.is_empty() {
let mut candidates = valid_out_of_scope_traits;
candidates.sort();
Expand All @@ -379,6 +377,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
});

self.suggest_use_candidates(err, msg, candidates);
true
} else {
false
}
}

fn suggest_traits_to_import(&self,
err: &mut DiagnosticBuilder,
span: Span,
rcvr_ty: Ty<'tcx>,
item_name: ast::Name,
rcvr_expr: Option<&hir::Expr>,
valid_out_of_scope_traits: Vec<DefId>) {
if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4016,7 +4016,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
Ok(def) => def,
Err(error) => {
let def = match error {
method::MethodError::PrivateMatch(def) => def,
method::MethodError::PrivateMatch(def, _) => def,
_ => Def::Err,
};
if item_name != keywords::Invalid.name() {
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/trait-method-private.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 trait Bar {
fn method(&self);
}

pub struct Foo;

impl Foo {
fn method(&self) {}
}

impl Bar for Foo {
fn method(&self) {}
}
}

fn main() {
let foo = inner::Foo;
foo.method();
}
12 changes: 12 additions & 0 deletions src/test/ui/trait-method-private.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0624]: method `method` is private
--> $DIR/trait-method-private.rs:29:9
|
29 | foo.method();
| ^^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
candidate #1: `use inner::Bar;`

error: aborting due to previous error