Skip to content

Commit 8c90934

Browse files
committed
Suggest more likely code when encountering an incorrect assoc item referencing the current trait
1 parent 5558fe8 commit 8c90934

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/librustc_typeck/astconv.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1773,11 +1773,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
17731773
} else {
17741774
let path_str = tcx.def_path_str(trait_def_id);
17751775

1776+
let def_id = self.item_def_id();
1777+
1778+
debug!("qpath_to_ty: self.item_def_id()={:?}", def_id);
1779+
1780+
let parent_def_id = def_id.and_then(|def_id| tcx.hir().as_local_hir_id(def_id))
1781+
.map(|hir_id| tcx.hir().get_parent_did(hir_id));
1782+
1783+
debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id);
1784+
17761785
// If the trait in segment is the same as the trait defining the item,
17771786
// use the `<Self as ..>` syntax in the error.
1778-
debug!("qpath_to_ty: self.item_def_id()={:?}", self.item_def_id());
1787+
let is_part_of_self_trait_constraints = def_id == Some(trait_def_id);
1788+
let is_part_of_fn_in_self_trait = parent_def_id == Some(trait_def_id);
17791789

1780-
let type_name = if self.item_def_id() == Some(trait_def_id) {
1790+
let type_name = if is_part_of_self_trait_constraints || is_part_of_fn_in_self_trait {
17811791
"Self"
17821792
} else {
17831793
"Type"

src/test/ui/associated-types/associated-types-in-ambiguous-context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ trait Grab {
1010
type Value;
1111
fn grab(&self) -> Grab::Value;
1212
//~^ ERROR ambiguous associated type
13+
14+
fn get(&self) -> Get::Value;
15+
//~^ ERROR ambiguous associated type
1316
}
1417

1518
trait Bar {}

src/test/ui/associated-types/associated-types-in-ambiguous-context.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ LL | fn get<T:Get,U:Get>(x: T, y: U) -> Get::Value {}
55
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Get>::Value`
66

77
error[E0223]: ambiguous associated type
8-
--> $DIR/associated-types-in-ambiguous-context.rs:17:17
8+
--> $DIR/associated-types-in-ambiguous-context.rs:20:17
99
|
1010
LL | trait Foo where Foo::Assoc: Bar {
1111
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Foo>::Assoc`
1212

1313
error[E0223]: ambiguous associated type
14-
--> $DIR/associated-types-in-ambiguous-context.rs:22:10
14+
--> $DIR/associated-types-in-ambiguous-context.rs:25:10
1515
|
1616
LL | type X = std::ops::Deref::Target;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as std::ops::Deref>::Target`
@@ -20,8 +20,14 @@ error[E0223]: ambiguous associated type
2020
--> $DIR/associated-types-in-ambiguous-context.rs:11:23
2121
|
2222
LL | fn grab(&self) -> Grab::Value;
23-
| ^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Grab>::Value`
23+
| ^^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Grab>::Value`
2424

25-
error: aborting due to 4 previous errors
25+
error[E0223]: ambiguous associated type
26+
--> $DIR/associated-types-in-ambiguous-context.rs:14:22
27+
|
28+
LL | fn get(&self) -> Get::Value;
29+
| ^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Get>::Value`
30+
31+
error: aborting due to 5 previous errors
2632

2733
For more information about this error, try `rustc --explain E0223`.

0 commit comments

Comments
 (0)