1
Fork 0

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

@ -828,7 +828,7 @@ pub(crate) struct IrrefutableLetPatternsWhileLet {
#[derive(Diagnostic)]
#[diag(mir_build_borrow_of_moved_value)]
pub(crate) struct BorrowOfMovedValue {
pub(crate) struct BorrowOfMovedValue<'tcx> {
#[primary_span]
#[label]
#[label(mir_build_occurs_because_label)]
@ -836,7 +836,7 @@ pub(crate) struct BorrowOfMovedValue {
#[label(mir_build_value_borrowed_label)]
pub(crate) conflicts_ref: Vec<Span>,
pub(crate) name: Ident,
pub(crate) ty: String,
pub(crate) ty: Ty<'tcx>,
#[suggestion(code = "ref ", applicability = "machine-applicable")]
pub(crate) suggest_borrowing: Option<Span>,
}

View file

@ -786,17 +786,13 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, 'tcx>, pat:
}
});
if !conflicts_ref.is_empty() {
let mut path = None;
let ty = cx.tcx.short_string(ty, &mut path);
let mut err = sess.dcx().create_err(BorrowOfMovedValue {
sess.dcx().emit_err(BorrowOfMovedValue {
binding_span: pat.span,
conflicts_ref,
name: Ident::new(name, pat.span),
ty,
suggest_borrowing: Some(pat.span.shrink_to_lo()),
});
*err.long_ty_path() = path;
err.emit();
}
return;
}