Skip to content

Commit 11a0308

Browse files
committed
fix: correct the args for disambiguate the associated function diagnostic
1 parent 1c15b82 commit 11a0308

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
@@ -3304,6 +3304,7 @@ fn print_disambiguation_help<'tcx>(
33043304
span: Span,
33053305
item: ty::AssocItem,
33063306
) -> Option<String> {
3307+
let trait_impl_type = trait_ref.self_ty();
33073308
let trait_ref = if item.fn_has_self_parameter {
33083309
trait_ref.print_only_trait_name().to_string()
33093310
} else {
@@ -3316,6 +3317,13 @@ fn print_disambiguation_help<'tcx>(
33163317
{
33173318
let def_kind_descr = tcx.def_kind_descr(item.kind.as_def_kind(), item.def_id);
33183319
let item_name = item.ident(tcx);
3320+
let first_arg_type = tcx
3321+
.fn_sig(item.def_id)
3322+
.instantiate_identity()
3323+
.skip_binder()
3324+
.inputs()
3325+
.get(0)
3326+
.and_then(|first| Some(first.peel_refs()));
33193327
let rcvr_ref = tcx
33203328
.fn_sig(item.def_id)
33213329
.skip_binder()
@@ -3324,19 +3332,22 @@ fn print_disambiguation_help<'tcx>(
33243332
.get(0)
33253333
.and_then(|ty| ty.ref_mutability())
33263334
.map_or("", |mutbl| mutbl.ref_prefix_str());
3327-
let args = format!(
3328-
"({}{})",
3329-
rcvr_ref,
3330-
std::iter::once(receiver)
3331-
.chain(args.iter())
3332-
.map(|arg| tcx
3333-
.sess
3334-
.source_map()
3335-
.span_to_snippet(arg.span)
3336-
.unwrap_or_else(|_| { "_".to_owned() }))
3337-
.collect::<Vec<_>>()
3338-
.join(", "),
3339-
);
3335+
// 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.
3336+
let args = if let Some(t) = first_arg_type
3337+
&& (t.to_string() == "Self" || t == trait_impl_type)
3338+
{
3339+
std::iter::once(receiver).chain(args.iter()).collect::<Vec<_>>()
3340+
} else {
3341+
args.iter().collect::<Vec<_>>()
3342+
}
3343+
.iter()
3344+
.map(|arg| {
3345+
tcx.sess.source_map().span_to_snippet(arg.span).unwrap_or_else(|_| "_".to_owned())
3346+
})
3347+
.collect::<Vec<_>>()
3348+
.join(", ");
3349+
3350+
let args = format!("({}{})", rcvr_ref, args);
33403351
err.span_suggestion_verbose(
33413352
span,
33423353
format!(

0 commit comments

Comments
 (0)