From 4745d34bc3ac377f8444d5dac94e0f206b7cded7 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 7 Sep 2023 00:20:31 +0000 Subject: [PATCH] Use self instead of the actual self ty --- .../src/check/compare_impl_item/refine.rs | 18 +++++++++++++++--- .../bad-item-bound-within-rpitit.stderr | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs index bc80cd62057..b013f292e52 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs @@ -29,6 +29,18 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args); let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args); let trait_m_sig = tcx.liberate_late_bound_regions(impl_m.def_id, bound_trait_m_sig); + // replace the self type of the trait ref with `Self` so that diagnostics render better. + let trait_m_sig_with_self_for_diag = tcx.liberate_late_bound_regions( + impl_m.def_id, + tcx.fn_sig(trait_m.def_id).instantiate( + tcx, + tcx.mk_args_from_iter( + [tcx.types.self_param.into()] + .into_iter() + .chain(trait_m_to_impl_m_args.iter().skip(1)), + ), + ), + ); let Ok(hidden_tys) = tcx.collect_return_position_impl_trait_in_trait_tys(impl_m.def_id) else { // Error already emitted, no need to delay another. @@ -51,7 +63,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( let ty::Alias(ty::Opaque, impl_opaque) = *hidden_ty.kind() else { report_mismatched_rpitit_signature( tcx, - trait_m_sig, + trait_m_sig_with_self_for_diag, trait_m.def_id, impl_m.def_id, None, @@ -70,7 +82,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( }) { report_mismatched_rpitit_signature( tcx, - trait_m_sig, + trait_m_sig_with_self_for_diag, trait_m.def_id, impl_m.def_id, None, @@ -163,7 +175,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( if !trait_bounds.contains(&clause) { report_mismatched_rpitit_signature( tcx, - trait_m_sig, + trait_m_sig_with_self_for_diag, trait_m.def_id, impl_m.def_id, Some(span), diff --git a/tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr b/tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr index 8d08d5e521e..c0e07e94dab 100644 --- a/tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr +++ b/tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr @@ -24,8 +24,8 @@ LL | fn iter(&self) -> impl 'a + Iterator> { = note: `#[warn(refining_impl_trait)]` on by default help: replace the return type so that it matches the trait | -LL | fn iter(&self) -> impl Iterator::Item<'_>> + '_ { - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | fn iter(&self) -> impl Iterator::Item<'_>> + '_ { + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: aborting due to previous error; 1 warning emitted