Skip to content

Commit 6c6a2c1

Browse files
committed
fix: correct the args for disambiguate the associated function diagnostic
1 parent 5c927ab commit 6c6a2c1

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -3312,6 +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();
33153316
let trait_ref = if item.fn_has_self_parameter {
33163317
trait_ref.print_only_trait_name().to_string()
33173318
} else {
@@ -3324,6 +3325,13 @@ fn print_disambiguation_help<'tcx>(
33243325
{
33253326
let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
33263327
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()));
33273335
let rcvr_ref = tcx
33283336
.fn_sig(item.def_id)
33293337
.skip_binder()
@@ -3332,19 +3340,22 @@ fn print_disambiguation_help<'tcx>(
33323340
.get(0)
33333341
.and_then(|ty| ty.ref_mutability())
33343342
.map_or("", |mutbl| mutbl.ref_prefix_str());
3335-
let args = format!(
3336-
"({}{})",
3337-
rcvr_ref,
3338-
std::iter::once(receiver)
3339-
.chain(args.iter())
3340-
.map(|arg| tcx
3341-
.sess
3342-
.source_map()
3343-
.span_to_snippet(arg.span)
3344-
.unwrap_or_else(|_| { "_".to_owned() }))
3345-
.collect::<Vec<_>>()
3346-
.join(", "),
3347-
);
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)
3346+
{
3347+
std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
3348+
} else {
3349+
args.iter().collect::<Vec<_>>()
3350+
}
3351+
.iter()
3352+
.map(|arg| {
3353+
tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
3354+
})
3355+
.collect::<Vec<_>>()
3356+
.join(", ");
3357+
3358+
let args = format!("({}{})", rcvr_ref, args);
33483359
err.span_suggestion_verbose(
33493360
span,
33503361
format!(

0 commit comments

Comments
 (0)