Auto merge of #43737 - GuillaumeGomez:duplicate-method, r=eddyb
Improve error message when duplicate names for type and trait method Fixes #43626.
This commit is contained in:
commit
2ac5f7d249
|
@ -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.
|
||||
|
|
|
@ -1013,7 +1013,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,
|
||||
|
|
|
@ -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) => {
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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
|
||||
|
Loading…
Reference in New Issue