about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-09-07 00:20:31 +0000
committerMichael Goulet <michael@errs.io>2023-09-07 00:49:09 +0000
commit4745d34bc3ac377f8444d5dac94e0f206b7cded7 (patch)
tree2e811046bef217c3dae8708c88bfed8b8ec32e0e
parente10262ca0aa2ff434b33898d1d03413fdde60edf (diff)
Use self instead of the actual self ty
-rw-r--r--compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs18
-rw-r--r--tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr4
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<Item = I::Item<'a>> {
    = 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 = <&'a I as Iterable>::Item<'_>> + '_ {
-   |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+LL |     fn iter(&self) -> impl Iterator<Item = <Self as Iterable>::Item<'_>> + '_ {
+   |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error: aborting due to previous error; 1 warning emitted