Skip to content

Commit 48b5d11

Browse files
Rollup merge of #89255 - FabianWolff:issue-88806, r=cjgillot
Fix incorrect disambiguation suggestion for associated items Fixes #88806. I have not added a new test case, because the erroneous behavior is already present in existing test cases.
2 parents e601554 + 3d08ff1 commit 48b5d11

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
@@ -176,6 +176,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
176176
sugg_span,
177177
idx,
178178
self.tcx.sess.source_map(),
179+
item.fn_has_self_parameter,
179180
);
180181
}
181182
}
@@ -218,6 +219,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
218219
sugg_span,
219220
idx,
220221
self.tcx.sess.source_map(),
222+
item.fn_has_self_parameter,
221223
);
222224
}
223225
}
@@ -1736,6 +1738,7 @@ fn print_disambiguation_help(
17361738
span: Span,
17371739
candidate: Option<usize>,
17381740
source_map: &source_map::SourceMap,
1741+
fn_has_self_parameter: bool,
17391742
) {
17401743
let mut applicability = Applicability::MachineApplicable;
17411744
let (span, sugg) = if let (ty::AssocKind::Fn, Some(args)) = (kind, args) {
@@ -1754,9 +1757,14 @@ fn print_disambiguation_help(
17541757
.collect::<Vec<_>>()
17551758
.join(", "),
17561759
);
1760+
let trait_name = if !fn_has_self_parameter {
1761+
format!("<{} as {}>", rcvr_ty, trait_name)
1762+
} else {
1763+
trait_name
1764+
};
17571765
(span, format!("{}::{}{}", trait_name, item_name, args))
17581766
} else {
1759-
(span.with_hi(item_name.span.lo()), format!("{}::", trait_name))
1767+
(span.with_hi(item_name.span.lo()), format!("<{} as {}>::", rcvr_ty, trait_name))
17601768
};
17611769
err.span_suggestion_verbose(
17621770
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)