Repository: rust-lang/rust
File: compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs (Rust)

Code before:
        };
        let Some(future_output_ty) = tcx
            .explicit_item_bounds(future_ty.def_id)
            .iter_instantiated_copied(tcx, future_ty.args)
            .find_map(|(clause, _)| match clause.kind().no_bound_vars()? {
                ty::ClauseKind::Projection(proj) => proj.term.as_type(),
                _ => None,
            })
        else {
            span_bug!(tcx.def_span(trait_m_def_id), "expected `Future` projection bound in AFIT");
        };
        return_ty = future_output_ty;
    }

    let (span, impl_return_span, pre, post) =
        match tcx.hir_node_by_def_id(impl_m_def_id.expect_local()).fn_decl().unwrap().output {
            hir::FnRetTy::DefaultReturn(span) => (tcx.def_span(impl_m_def_id), span, "-> ", " "),
            hir::FnRetTy::Return(ty) => (ty.span, ty.span, "", ""),
        };
    let trait_return_span =
        tcx.hir_get_if_local(trait_m_def_id).map(|node| match node.fn_decl().unwrap().output {
            hir::FnRetTy::DefaultReturn(_) => tcx.def_span(trait_m_def_id),
            hir::FnRetTy::Return(ty) => ty.span,
        });

    let return_ty_suggestion = if tcx.asyncness(trait_m_def_id).is_async() {
        format!("{return_ty}")
    } else if let Some(trait_ret_span) = trait_return_span
        && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(trait_ret_span)
    {
        snippet
    } else {
        format!("{return_ty}")
    };

    let span = unmatched_bound.unwrap_or(span);
    tcx.emit_node_span_lint(
        if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
        tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
        span,
        crate::errors::ReturnPositionImplTraitInTraitRefined {
            impl_return_span,
            trait_return_span,
            pre,
            post,
            return_ty: return_ty_suggestion,
            unmatched_bound,
        },
    );
}


Review comment (bug):
I'm interested in the reason *why* the type gets printed that way under RTN. I would look in the HIR pretty printer to look for the culprit. Once identified, there might be a more robust solution because using `span_to_snippet` is more like hot patching the bug (e.g., if the source is not available (so `None`) I'm pretty sure we will continue to print the type that way under RTN).

Code after:
            );
        };
        let Some(future_output_ty) = tcx
            .explicit_item_bounds(future_ty.def_id)
            .iter_instantiated_copied(tcx, future_ty.args)
            .find_map(|(clause, _)| match clause.kind().no_bound_vars()? {
                ty::ClauseKind::Projection(proj) => proj.term.as_type(),
                _ => None,
            })
        else {
            span_bug!(tcx.def_span(trait_m_def_id), "expected `Future` projection bound in AFIT");
        };
        return_ty = future_output_ty;
    }

    let (span, impl_return_span, pre, post) =
        match tcx.hir_node_by_def_id(impl_m_def_id.expect_local()).fn_decl().unwrap().output {
            hir::FnRetTy::DefaultReturn(span) => (tcx.def_span(impl_m_def_id), span, "-> ", " "),
            hir::FnRetTy::Return(ty) => (ty.span, ty.span, "", ""),
        };
    let trait_return_span =
        tcx.hir_get_if_local(trait_m_def_id).map(|node| match node.fn_decl().unwrap().output {
            hir::FnRetTy::DefaultReturn(_) => tcx.def_span(trait_m_def_id),
            hir::FnRetTy::Return(ty) => ty.span,
        });

    // Use ForSignature mode to ensure RPITITs are printed as `impl Trait` rather than
    // `impl Trait { T::method(..) }` when RTN is enabled.
    //
    // We use `with_no_trimmed_paths!` to avoid triggering the `trimmed_def_paths` query,
    // which requires diagnostic context (via `must_produce_diag`). Since we're formatting
    // the type before creating the diagnostic, we need to avoid this query. This is the
    // standard approach used elsewhere in the compiler for formatting types in suggestions
    // (e.g., see `rustc_hir_typeck/src/demand.rs`).
    let return_ty_suggestion =
        with_no_trimmed_paths!(with_types_for_signature!(format!("{return_ty}")));

    let span = unmatched_bound.unwrap_or(span);
    tcx.emit_node_span_lint(
        if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE },
        tcx.local_def_id_to_hir_id(impl_m_def_id.expect_local()),
        span,
        crate::errors::ReturnPositionImplTraitInTraitRefined {
            impl_return_span,
            trait_return_span,
            pre,
            post,
            return_ty: return_ty_suggestion,
            unmatched_bound,
        },
    );