Rollup merge of #138126 - compiler-errors:rtn-for-sugg, r=oli-obk

Add an opt-out in pretty printing for RTN rendering

Today, we render RPITIT types like `impl Sized { T::method(..) }` when RTN is enabled. This is very useful for diagnostics, since it's often not clear what the `impl Sized` type means by itself, and it makes it clear that that's an RPITIT that can be bounded using RTN syntax. See #115624.

However, since we don't distinguish types that are rendered for the purposes of printing messages vs suggestions, this representation leaks into suggestions and turns into code that can't be parsed. This PR adds a new `with_types_for_suggestion! {}` and `with_types_for_signature! {}` options to the pretty printing architecture to make it clear that we're rendering a type for code suggestions.

This can be applied later as we find that we need it.
This commit is contained in:
Matthias Krüger 2025-03-13 17:44:04 +01:00 committed by GitHub
commit b5955e74e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 154 additions and 32 deletions

View file

@ -84,6 +84,7 @@ use rustc_infer::infer::{self, TyCtxtInferExt as _};
use rustc_infer::traits::ObligationCause;
use rustc_middle::query::Providers;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::print::with_types_for_signature;
use rustc_middle::ty::{self, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypingMode};
use rustc_middle::{bug, span_bug};
use rustc_session::parse::feature_err;
@ -240,11 +241,11 @@ fn missing_items_err(
(Vec::new(), Vec::new(), Vec::new());
for &trait_item in missing_items {
let snippet = suggestion_signature(
let snippet = with_types_for_signature!(suggestion_signature(
tcx,
trait_item,
tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity(),
);
));
let code = format!("{padding}{snippet}\n{padding}");
if let Some(span) = tcx.hir().span_if_local(trait_item.def_id) {
missing_trait_item_label