fix: correct the args for disambiguate the associated function diagnostic

This commit is contained in:
Young-Flash 2023-12-13 19:57:42 +08:00
parent 5c927ab999
commit 6c6a2c1ef7

View File

@ -3312,6 +3312,7 @@ fn print_disambiguation_help<'tcx>(
span: Span, span: Span,
item: ty::AssocItem, item: ty::AssocItem,
) -> Option<String> { ) -> Option<String> {
let trait_impl_type = trait_ref.self_ty();
let trait_ref = if item.fn_has_self_parameter { let trait_ref = if item.fn_has_self_parameter {
trait_ref.print_only_trait_name().to_string() trait_ref.print_only_trait_name().to_string()
} else { } else {
@ -3324,6 +3325,13 @@ fn print_disambiguation_help<'tcx>(
{ {
let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id); let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
let item_name = item.ident(tcx); let item_name = item.ident(tcx);
let first_arg_type = tcx
.fn_sig(item.def_id)
.instantiate_identity()
.skip_binder()
.inputs()
.get(0)
.and_then(|first| Some(first.peel_refs()));
let rcvr_ref = tcx let rcvr_ref = tcx
.fn_sig(item.def_id) .fn_sig(item.def_id)
.skip_binder() .skip_binder()
@ -3332,19 +3340,22 @@ fn print_disambiguation_help<'tcx>(
.get(0) .get(0)
.and_then(|ty| ty.ref_mutability()) .and_then(|ty| ty.ref_mutability())
.map_or("", |mutbl| mutbl.ref_prefix_str()); .map_or("", |mutbl| mutbl.ref_prefix_str());
let args = format!( // 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
rcvr_ref, && (t.to_string() == "Self" || t == trait_impl_type)
std::iter::once(receiver) {
.chain(args.iter()) std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
.map(|arg| tcx } else {
.sess args.iter().collect::<Vec<_>>()
.source_map() }
.span_to_snippet(arg.span) .iter()
.unwrap_or_else(|_| { "_".to_owned() })) .map(|arg| {
tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "), .join(", ");
);
let args = format!("({}{})", rcvr_ref, args);
err.span_suggestion_verbose( err.span_suggestion_verbose(
span, span,
format!( format!(