@@ -6,6 +6,7 @@ use rustc_infer::infer::TyCtxtInferExt;
66use rustc_lint_defs:: builtin:: { REFINING_IMPL_TRAIT_INTERNAL , REFINING_IMPL_TRAIT_REACHABLE } ;
77use rustc_middle:: span_bug;
88use rustc_middle:: traits:: ObligationCause ;
9+ use rustc_middle:: ty:: print:: { with_no_trimmed_paths, with_types_for_signature} ;
910use rustc_middle:: ty:: {
1011 self , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperVisitable , TypeVisitable ,
1112 TypeVisitableExt , TypeVisitor , TypingMode ,
@@ -332,6 +333,17 @@ fn report_mismatched_rpitit_signature<'tcx>(
332333 hir:: FnRetTy :: Return ( ty) => ty. span ,
333334 } ) ;
334335
336+ // Use ForSignature mode to ensure RPITITs are printed as `impl Trait` rather than
337+ // `impl Trait { T::method(..) }` when RTN is enabled.
338+ //
339+ // We use `with_no_trimmed_paths!` to avoid triggering the `trimmed_def_paths` query,
340+ // which requires diagnostic context (via `must_produce_diag`). Since we're formatting
341+ // the type before creating the diagnostic, we need to avoid this query. This is the
342+ // standard approach used elsewhere in the compiler for formatting types in suggestions
343+ // (e.g., see `rustc_hir_typeck/src/demand.rs`).
344+ let return_ty_suggestion =
345+ with_no_trimmed_paths ! ( with_types_for_signature!( format!( "{return_ty}" ) ) ) ;
346+
335347 let span = unmatched_bound. unwrap_or ( span) ;
336348 tcx. emit_node_span_lint (
337349 if is_internal { REFINING_IMPL_TRAIT_INTERNAL } else { REFINING_IMPL_TRAIT_REACHABLE } ,
@@ -342,7 +354,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
342354 trait_return_span,
343355 pre,
344356 post,
345- return_ty,
357+ return_ty : return_ty_suggestion ,
346358 unmatched_bound,
347359 } ,
348360 ) ;
0 commit comments