Skip to content

Commit

Permalink
fix broken CI and code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Dec 16, 2023
1 parent 57eed05 commit 108c5dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
52 changes: 48 additions & 4 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3304,7 +3304,7 @@ fn print_disambiguation_help<'tcx>(
span: Span,
item: ty::AssocItem,
) -> Option<String> {
let trait_impl_type = trait_ref.self_ty();
let trait_impl_type = trait_ref.self_ty().peel_refs();
let trait_ref = if item.fn_has_self_parameter {
trait_ref.print_only_trait_name().to_string()
} else {
Expand Down Expand Up @@ -3332,9 +3332,53 @@ fn print_disambiguation_help<'tcx>(
.get(0)
.and_then(|ty| ty.ref_mutability())
.map_or("", |mutbl| mutbl.ref_prefix_str());
// 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.
let args = if let Some(t) = first_arg_type
&& (t.to_string() == "Self" || t == trait_impl_type)

// this is for `arbitrary_self_types`(egde case), see `tests/ui/methods/method-deref-to-same-trait-object-with-separate-params.rs`
// for the following example:
//
// impl<T: ?Sized> NuisanceFoo for T {
// fn foo(self) {}
// }
//
// type of `self` is T(unspecified), which is not `Self` or current trait impl type, but the receiver still shouble be take as the first arg.
let arbitrary_self_types_check =
|hir_map: rustc_middle::hir::map::Map<'_>,
local_def_id: Option<rustc_span::def_id::LocalDefId>|
-> bool {
match local_def_id {
Some(local_def_id) => match hir_map.find_by_def_id(local_def_id) {
Some(node) => match node {
Node::ImplItem(it) => match it.kind {
hir::ImplItemKind::Fn(_, body_id) => {
let body = hir_map.body(body_id);
match body.params.get(0) {
Some(first_param) => {
if let rustc_hir::PatKind::Binding(_, _, ident, _) =
&first_param.pat.kind
{
ident.name.as_str() == "self"
} else {
false
}
}
None => false,
}
}
_ => false,
},
_ => false,
},
None => false,
},
None => false,
}
};

// 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.
let args = if let Some(first_arg_type) = first_arg_type
&& (first_arg_type == tcx.types.self_param
|| first_arg_type == trait_impl_type
|| arbitrary_self_types_check(tcx.hir(), item.def_id.as_local()))
{
std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
} else {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/methods/method-ambiguity-no-rcvr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ LL | fn foo() {}
| ^^^^^^^^
help: disambiguate the associated function for candidate #1
|
LL | <Qux as Foo>::foo(Qux);
| ~~~~~~~~~~~~~~~~~~~~~~
LL | <Qux as Foo>::foo();
| ~~~~~~~~~~~~~~~~~~~
help: disambiguate the associated function for candidate #2
|
LL | <Qux as FooBar>::foo(Qux);
| ~~~~~~~~~~~~~~~~~~~~~~~~~
LL | <Qux as FooBar>::foo();
| ~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 1 previous error

Expand Down

0 comments on commit 108c5dd

Please sign in to comment.