diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 8bcf51eb9e3c0..b14121da79f59 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -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 `` 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" diff --git a/src/test/ui/associated-types/associated-types-in-ambiguous-context.rs b/src/test/ui/associated-types/associated-types-in-ambiguous-context.rs index 202eecfa7b7f5..51b53908f98c3 100644 --- a/src/test/ui/associated-types/associated-types-in-ambiguous-context.rs +++ b/src/test/ui/associated-types/associated-types-in-ambiguous-context.rs @@ -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 {} diff --git a/src/test/ui/associated-types/associated-types-in-ambiguous-context.stderr b/src/test/ui/associated-types/associated-types-in-ambiguous-context.stderr index db6dec6e899dc..77835c5f6766e 100644 --- a/src/test/ui/associated-types/associated-types-in-ambiguous-context.stderr +++ b/src/test/ui/associated-types/associated-types-in-ambiguous-context.stderr @@ -5,13 +5,13 @@ LL | fn get(x: T, y: U) -> Get::Value {} | ^^^^^^^^^^ help: use fully-qualified syntax: `::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: `::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: `::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: `::Value` + | ^^^^^^^^^^^ help: use fully-qualified syntax: `::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: `::Value` + +error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0223`.