Skip to content

Commit

Permalink
Suggest more likely code when encountering an incorrect assoc item re…
Browse files Browse the repository at this point in the history
…ferencing the current trait
  • Loading branch information
ohadravid committed Nov 2, 2019
1 parent 5558fe8 commit 8c90934
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`.

0 comments on commit 8c90934

Please sign in to comment.