about summary refs log tree commit diff
path: root/tests/ui/diagnostic-width
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2025-02-18 01:15:59 +0000
committerEsteban Küber <esteban@kuber.com.ar>2025-02-25 16:56:03 +0000
commitd12ecaed558426ec7998816d64b240ea685a2a8f (patch)
tree5c0c68022dd9b4da83daa6e7bd645f24ce738d0b /tests/ui/diagnostic-width
parentc51b9b6d5234aa8e50c3b87784113a1af1af47cb (diff)
downloadrust-d12ecaed558426ec7998816d64b240ea685a2a8f.tar.gz
rust-d12ecaed558426ec7998816d64b240ea685a2a8f.zip
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
```
Diffstat (limited to 'tests/ui/diagnostic-width')
-rw-r--r--tests/ui/diagnostic-width/long-E0618.rs13
-rw-r--r--tests/ui/diagnostic-width/long-E0618.stderr16
2 files changed, 29 insertions, 0 deletions
diff --git a/tests/ui/diagnostic-width/long-E0618.rs b/tests/ui/diagnostic-width/long-E0618.rs
new file mode 100644
index 00000000000..f8626ab9455
--- /dev/null
+++ b/tests/ui/diagnostic-width/long-E0618.rs
@@ -0,0 +1,13 @@
+//@ compile-flags: --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
+// The regex below normalizes the long type file name to make it suitable for compare-modes.
+//@ normalize-stderr: "'\$TEST_BUILD_DIR/.*\.long-type-\d+.txt'" -> "'$$TEST_BUILD_DIR/$$FILE.long-type-hash.txt'"
+type A = (i32, i32, i32, i32);
+type B = (A, A, A, A);
+type C = (B, B, B, B);
+type D = (C, C, C, C);
+
+fn foo(x: D) { //~ `x` has type `(...
+    x(); //~ ERROR expected function, found `(...
+}
+
+fn main() {}
diff --git a/tests/ui/diagnostic-width/long-E0618.stderr b/tests/ui/diagnostic-width/long-E0618.stderr
new file mode 100644
index 00000000000..f0838cbddcc
--- /dev/null
+++ b/tests/ui/diagnostic-width/long-E0618.stderr
@@ -0,0 +1,16 @@
+error[E0618]: expected function, found `(..., ..., ..., ...)`
+  --> $DIR/long-E0618.rs:10:5
+   |
+LL | fn foo(x: D) {
+   |        - `x` has type `(..., ..., ..., ...)`
+LL |     x();
+   |     ^--
+   |     |
+   |     call expression requires function
+   |
+   = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt'
+   = note: consider using `--verbose` to print the full type name to the console
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0618`.