Skip to content

Commit 4745d34

Browse files
Use self instead of the actual self ty
1 parent e10262c commit 4745d34

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
2929
let trait_m_to_impl_m_args = impl_m_args.rebase_onto(tcx, impl_def_id, impl_trait_ref.args);
3030
let bound_trait_m_sig = tcx.fn_sig(trait_m.def_id).instantiate(tcx, trait_m_to_impl_m_args);
3131
let trait_m_sig = tcx.liberate_late_bound_regions(impl_m.def_id, bound_trait_m_sig);
32+
// replace the self type of the trait ref with `Self` so that diagnostics render better.
33+
let trait_m_sig_with_self_for_diag = tcx.liberate_late_bound_regions(
34+
impl_m.def_id,
35+
tcx.fn_sig(trait_m.def_id).instantiate(
36+
tcx,
37+
tcx.mk_args_from_iter(
38+
[tcx.types.self_param.into()]
39+
.into_iter()
40+
.chain(trait_m_to_impl_m_args.iter().skip(1)),
41+
),
42+
),
43+
);
3244

3345
let Ok(hidden_tys) = tcx.collect_return_position_impl_trait_in_trait_tys(impl_m.def_id) else {
3446
// Error already emitted, no need to delay another.
@@ -51,7 +63,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
5163
let ty::Alias(ty::Opaque, impl_opaque) = *hidden_ty.kind() else {
5264
report_mismatched_rpitit_signature(
5365
tcx,
54-
trait_m_sig,
66+
trait_m_sig_with_self_for_diag,
5567
trait_m.def_id,
5668
impl_m.def_id,
5769
None,
@@ -70,7 +82,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
7082
}) {
7183
report_mismatched_rpitit_signature(
7284
tcx,
73-
trait_m_sig,
85+
trait_m_sig_with_self_for_diag,
7486
trait_m.def_id,
7587
impl_m.def_id,
7688
None,
@@ -163,7 +175,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
163175
if !trait_bounds.contains(&clause) {
164176
report_mismatched_rpitit_signature(
165177
tcx,
166-
trait_m_sig,
178+
trait_m_sig_with_self_for_diag,
167179
trait_m.def_id,
168180
impl_m.def_id,
169181
Some(span),

tests/ui/impl-trait/in-trait/bad-item-bound-within-rpitit.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ LL | fn iter(&self) -> impl 'a + Iterator<Item = I::Item<'a>> {
2424
= note: `#[warn(refining_impl_trait)]` on by default
2525
help: replace the return type so that it matches the trait
2626
|
27-
LL | fn iter(&self) -> impl Iterator<Item = <&'a I as Iterable>::Item<'_>> + '_ {
28-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27+
LL | fn iter(&self) -> impl Iterator<Item = <Self as Iterable>::Item<'_>> + '_ {
28+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2929

3030
error: aborting due to previous error; 1 warning emitted
3131

0 commit comments

Comments
 (0)