Teach structured errors to display short Ty

Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to.

```
error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)``
 --> long.rs:7:5
  |
6 | fn foo(x: D) { //~ `x` has type `(...
  |        - `x` has type `((..., ..., ..., ...), ..., ..., ...)`
7 |     x(); //~ ERROR expected function, found `(...
  |     ^--
  |     |
  |     call expression requires function
  |
  = note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt'
  = note: consider using `--verbose` to print the full type name to the console
```
This commit is contained in:
Esteban Küber 2025-02-18 01:15:59 +00:00
parent c51b9b6d52
commit d12ecaed55
42 changed files with 217 additions and 168 deletions

View file

@ -2418,7 +2418,7 @@ impl<'tcx> ObligationCause<'tcx> {
pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
impl IntoDiagArg for ObligationCauseAsDiagArg<'_> {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
let kind = match self.0.code() {
ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn, .. } => "method_compat",
ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type, .. } => "type_compat",

View file

@ -137,7 +137,7 @@ impl InferenceDiagnosticsParentData {
}
impl IntoDiagArg for UnderspecifiedArgKind {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
let kind = match self {
Self::Type { .. } => "type",
Self::Const { is_parameter: true } => "const_with_param",

View file

@ -31,7 +31,7 @@ impl<'tcx, T> IntoDiagArg for Highlighted<'tcx, T>
where
T: for<'a> Print<'tcx, FmtPrinter<'a, 'tcx>>,
{
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
rustc_errors::DiagArgValue::Str(self.to_string().into())
}
}

View file

@ -784,10 +784,10 @@ pub enum TyOrSig<'tcx> {
}
impl IntoDiagArg for TyOrSig<'_> {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
match self {
TyOrSig::Ty(ty) => ty.into_diag_arg(),
TyOrSig::ClosureSig(sig) => sig.into_diag_arg(),
TyOrSig::Ty(ty) => ty.into_diag_arg(path),
TyOrSig::ClosureSig(sig) => sig.into_diag_arg(path),
}
}
}

View file

@ -105,7 +105,7 @@ pub enum SuffixKind {
}
impl IntoDiagArg for PrefixKind {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
let kind = match self {
Self::Empty => "empty",
Self::RefValidFor => "ref_valid_for",
@ -127,7 +127,7 @@ impl IntoDiagArg for PrefixKind {
}
impl IntoDiagArg for SuffixKind {
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
let kind = match self {
Self::Empty => "empty",
Self::Continues => "continues",