Skip to content

Commit 9e9353c

Browse files
committed
fix broken CI and code review
1 parent ffd619a commit 9e9353c

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -3312,7 +3312,7 @@ fn print_disambiguation_help<'tcx>(
33123312
span: Span,
33133313
item: ty::AssocItem,
33143314
) -> Option<String> {
3315-
let trait_impl_type = trait_ref.self_ty();
3315+
let trait_impl_type = trait_ref.self_ty().peel_refs();
33163316
let trait_ref = if item.fn_has_self_parameter {
33173317
trait_ref.print_only_trait_name().to_string()
33183318
} else {
@@ -3325,30 +3325,27 @@ fn print_disambiguation_help<'tcx>(
33253325
{
33263326
let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
33273327
let item_name = item.ident(tcx);
3328-
let first_arg_type = tcx
3329-
.fn_sig(item.def_id)
3330-
.instantiate_identity()
3331-
.skip_binder()
3332-
.inputs()
3333-
.get(0)
3334-
.and_then(|first| Some(first.peel_refs()));
3335-
let rcvr_ref = tcx
3336-
.fn_sig(item.def_id)
3337-
.skip_binder()
3338-
.skip_binder()
3339-
.inputs()
3340-
.get(0)
3341-
.and_then(|ty| ty.ref_mutability())
3342-
.map_or("", |mutbl| mutbl.ref_prefix_str());
3343-
// If the type of first arg of this assoc function is `Self` or current trait impl type, we need to take the receiver as args. Otherwise, we don't.
3344-
let args = if let Some(t) = first_arg_type
3345-
&& (t.to_string() == "Self" || t == trait_impl_type)
3328+
let first_input =
3329+
tcx.fn_sig(item.def_id).instantiate_identity().skip_binder().inputs().get(0);
3330+
let (first_arg_type, rcvr_ref) = (
3331+
first_input.map(|first| first.peel_refs()),
3332+
first_input
3333+
.and_then(|ty| ty.ref_mutability())
3334+
.map_or("", |mutbl| mutbl.ref_prefix_str()),
3335+
);
3336+
3337+
// If the type of first arg of this assoc function is `Self` or current trait impl type or `arbitrary_self_types`, we need to take the receiver as args. Otherwise, we don't.
3338+
let args = if let Some(first_arg_type) = first_arg_type
3339+
&& (first_arg_type == tcx.types.self_param
3340+
|| first_arg_type == trait_impl_type
3341+
|| item.fn_has_self_parameter)
33463342
{
3347-
std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
3343+
Some(receiver)
33483344
} else {
3349-
args.iter().collect::<Vec<_>>()
3345+
None
33503346
}
3351-
.iter()
3347+
.into_iter()
3348+
.chain(args)
33523349
.map(|arg| {
33533350
tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
33543351
})

tests/ui/methods/disambiguate-associated-function-first-arg.rs

+21
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,24 @@ trait O {
2626
impl O for A {
2727
fn new(_a: A, _b: i32) {}
2828
}
29+
30+
struct S;
31+
32+
trait TraitA {
33+
fn f(self);
34+
}
35+
trait TraitB {
36+
fn f(self);
37+
}
38+
39+
impl<T> TraitA for T {
40+
fn f(self) {}
41+
}
42+
impl<T> TraitB for T {
43+
fn f(self) {}
44+
}
45+
46+
fn test() {
47+
S.f();
48+
//~^ multiple applicable items in scope
49+
}

tests/ui/methods/disambiguate-associated-function-first-arg.stderr

+28-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ help: disambiguate the associated function for candidate #3
3636
LL | <A as O>::new(_a, 1);
3737
| ~~~~~~~~~~~~~~~~~~~~
3838

39-
error: aborting due to 1 previous error
39+
error[E0034]: multiple applicable items in scope
40+
--> $DIR/disambiguate-associated-function-first-arg.rs:47:7
41+
|
42+
LL | S.f();
43+
| ^ multiple `f` found
44+
|
45+
note: candidate #1 is defined in an impl of the trait `TraitA` for the type `T`
46+
--> $DIR/disambiguate-associated-function-first-arg.rs:40:5
47+
|
48+
LL | fn f(self) {}
49+
| ^^^^^^^^^^
50+
note: candidate #2 is defined in an impl of the trait `TraitB` for the type `T`
51+
--> $DIR/disambiguate-associated-function-first-arg.rs:43:5
52+
|
53+
LL | fn f(self) {}
54+
| ^^^^^^^^^^
55+
help: disambiguate the method for candidate #1
56+
|
57+
LL | TraitA::f(S);
58+
| ~~~~~~~~~~~~
59+
help: disambiguate the method for candidate #2
60+
|
61+
LL | TraitB::f(S);
62+
| ~~~~~~~~~~~~
63+
64+
error: aborting due to 2 previous errors
4065

41-
For more information about this error, try `rustc --explain E0599`.
66+
Some errors have detailed explanations: E0034, E0599.
67+
For more information about an error, try `rustc --explain E0034`.

tests/ui/methods/method-ambiguity-no-rcvr.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ LL | fn foo() {}
2020
| ^^^^^^^^
2121
help: disambiguate the associated function for candidate #1
2222
|
23-
LL | <Qux as Foo>::foo(Qux);
24-
| ~~~~~~~~~~~~~~~~~~~~~~
23+
LL | <Qux as Foo>::foo();
24+
| ~~~~~~~~~~~~~~~~~~~
2525
help: disambiguate the associated function for candidate #2
2626
|
27-
LL | <Qux as FooBar>::foo(Qux);
28-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
27+
LL | <Qux as FooBar>::foo();
28+
| ~~~~~~~~~~~~~~~~~~~~~~
2929

3030
error: aborting due to 1 previous error
3131

0 commit comments

Comments
 (0)