1
Fork 0

Suggest more likely code when encountering an incorrect assoc item referencing the current trait

This commit is contained in:
Ohad Ravid 2019-11-02 09:49:05 +01:00
parent 5558fe8a92
commit 8c909344ed
3 changed files with 25 additions and 6 deletions

View file

@ -1773,11 +1773,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} else {
let path_str = tcx.def_path_str(trait_def_id);
let def_id = self.item_def_id();
debug!("qpath_to_ty: self.item_def_id()={:?}", def_id);
let parent_def_id = def_id.and_then(|def_id| tcx.hir().as_local_hir_id(def_id))
.map(|hir_id| tcx.hir().get_parent_did(hir_id));
debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id);
// If the trait in segment is the same as the trait defining the item,
// use the `<Self as ..>` syntax in the error.
debug!("qpath_to_ty: self.item_def_id()={:?}", self.item_def_id());
let is_part_of_self_trait_constraints = def_id == Some(trait_def_id);
let is_part_of_fn_in_self_trait = parent_def_id == Some(trait_def_id);
let type_name = if self.item_def_id() == Some(trait_def_id) {
let type_name = if is_part_of_self_trait_constraints || is_part_of_fn_in_self_trait {
"Self"
} else {
"Type"

View file

@ -10,6 +10,9 @@ trait Grab {
type Value;
fn grab(&self) -> Grab::Value;
//~^ ERROR ambiguous associated type
fn get(&self) -> Get::Value;
//~^ ERROR ambiguous associated type
}
trait Bar {}

View file

@ -5,13 +5,13 @@ LL | fn get<T:Get,U:Get>(x: T, y: U) -> Get::Value {}
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Get>::Value`
error[E0223]: ambiguous associated type
--> $DIR/associated-types-in-ambiguous-context.rs:17:17
--> $DIR/associated-types-in-ambiguous-context.rs:20:17
|
LL | trait Foo where Foo::Assoc: Bar {
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Foo>::Assoc`
error[E0223]: ambiguous associated type
--> $DIR/associated-types-in-ambiguous-context.rs:22:10
--> $DIR/associated-types-in-ambiguous-context.rs:25:10
|
LL | type X = std::ops::Deref::Target;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as std::ops::Deref>::Target`
@ -20,8 +20,14 @@ error[E0223]: ambiguous associated type
--> $DIR/associated-types-in-ambiguous-context.rs:11:23
|
LL | fn grab(&self) -> Grab::Value;
| ^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Grab>::Value`
| ^^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Grab>::Value`
error: aborting due to 4 previous errors
error[E0223]: ambiguous associated type
--> $DIR/associated-types-in-ambiguous-context.rs:14:22
|
LL | fn get(&self) -> Get::Value;
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Get>::Value`
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0223`.