Skip to content

Commit 3d08ff1

Browse files
committed
Fix incorrect disambiguation suggestion for associated items
1 parent a0648ea commit 3d08ff1

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
178178
sugg_span,
179179
idx,
180180
self.tcx.sess.source_map(),
181+
item.fn_has_self_parameter,
181182
);
182183
}
183184
}
@@ -220,6 +221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
220221
sugg_span,
221222
idx,
222223
self.tcx.sess.source_map(),
224+
item.fn_has_self_parameter,
223225
);
224226
}
225227
}
@@ -1738,6 +1740,7 @@ fn print_disambiguation_help(
17381740
span: Span,
17391741
candidate: Option<usize>,
17401742
source_map: &source_map::SourceMap,
1743+
fn_has_self_parameter: bool,
17411744
) {
17421745
let mut applicability = Applicability::MachineApplicable;
17431746
let (span, sugg) = if let (ty::AssocKind::Fn, Some(args)) = (kind, args) {
@@ -1756,9 +1759,14 @@ fn print_disambiguation_help(
17561759
.collect::<Vec<_>>()
17571760
.join(", "),
17581761
);
1762+
let trait_name = if !fn_has_self_parameter {
1763+
format!("<{} as {}>", rcvr_ty, trait_name)
1764+
} else {
1765+
trait_name
1766+
};
17591767
(span, format!("{}::{}{}", trait_name, item_name, args))
17601768
} else {
1761-
(span.with_hi(item_name.span.lo()), format!("{}::", trait_name))
1769+
(span.with_hi(item_name.span.lo()), format!("<{} as {}>::", rcvr_ty, trait_name))
17621770
};
17631771
err.span_suggestion_verbose(
17641772
span,

src/test/ui/associated-consts/associated-const-ambiguity-report.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ LL | const ID: i32 = 3;
1616
| ^^^^^^^^^^^^^^^^^^
1717
help: disambiguate the associated constant for candidate #1
1818
|
19-
LL | const X: i32 = Foo::ID;
20-
| ~~~~~
19+
LL | const X: i32 = <i32 as Foo>::ID;
20+
| ~~~~~~~~~~~~~~
2121
help: disambiguate the associated constant for candidate #2
2222
|
23-
LL | const X: i32 = Bar::ID;
24-
| ~~~~~
23+
LL | const X: i32 = <i32 as Bar>::ID;
24+
| ~~~~~~~~~~~~~~
2525

2626
error: aborting due to previous error
2727

src/test/ui/error-codes/E0034.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ LL | fn foo() {}
1616
| ^^^^^^^^
1717
help: disambiguate the associated function for candidate #1
1818
|
19-
LL | Trait1::foo()
20-
| ~~~~~~~~
19+
LL | <Test as Trait1>::foo()
20+
| ~~~~~~~~~~~~~~~~~~
2121
help: disambiguate the associated function for candidate #2
2222
|
23-
LL | Trait2::foo()
24-
| ~~~~~~~~
23+
LL | <Test as Trait2>::foo()
24+
| ~~~~~~~~~~~~~~~~~~
2525

2626
error: aborting due to previous error
2727

src/test/ui/methods/method-ambig-two-traits-from-impls2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ LL | fn foo() {}
1616
| ^^^^^^^^
1717
help: disambiguate the associated function for candidate #1
1818
|
19-
LL | A::foo();
20-
| ~~~
19+
LL | <AB as A>::foo();
20+
| ~~~~~~~~~~~
2121
help: disambiguate the associated function for candidate #2
2222
|
23-
LL | B::foo();
24-
| ~~~
23+
LL | <AB as B>::foo();
24+
| ~~~~~~~~~~~
2525

2626
error: aborting due to previous error
2727

src/test/ui/span/issue-7575.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ LL | fn f9(_: usize) -> usize;
2727
candidate #3: `UnusedTrait`
2828
help: disambiguate the associated function for candidate #1
2929
|
30-
LL | u.f8(42) + CtxtFn::f9(u, 342) + m.fff(42)
31-
| ~~~~~~~~~~~~~~~~~~
30+
LL | u.f8(42) + <usize as CtxtFn>::f9(u, 342) + m.fff(42)
31+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3232
help: disambiguate the associated function for candidate #2
3333
|
34-
LL | u.f8(42) + OtherTrait::f9(u, 342) + m.fff(42)
35-
| ~~~~~~~~~~~~~~~~~~~~~~
34+
LL | u.f8(42) + <usize as OtherTrait>::f9(u, 342) + m.fff(42)
35+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3636
help: disambiguate the associated function for candidate #3
3737
|
38-
LL | u.f8(42) + UnusedTrait::f9(u, 342) + m.fff(42)
39-
| ~~~~~~~~~~~~~~~~~~~~~~~
38+
LL | u.f8(42) + <usize as UnusedTrait>::f9(u, 342) + m.fff(42)
39+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4040

4141
error[E0599]: no method named `fff` found for struct `Myisize` in the current scope
4242
--> $DIR/issue-7575.rs:62:30
@@ -72,7 +72,7 @@ LL | fn is_str() -> bool {
7272
= help: items from traits can only be used if the type parameter is bounded by the trait
7373
help: disambiguate the associated function for the candidate
7474
|
75-
LL | ManyImplTrait::is_str(t)
75+
LL | <T as ManyImplTrait>::is_str(t)
7676
|
7777

7878
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)