about summary refs log tree commit diff
path: root/compiler/rustc_middle/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2022-11-25 17:14:25 -0800
committerEsteban Küber <esteban@kuber.com.ar>2022-11-28 14:08:18 -0800
commit7674edeebac02ce9b3ee6434e5a4ab6a085005f9 (patch)
treeb7a2e8e44842af103bb021aebc88bf0fa3607e3b /compiler/rustc_middle/src
parent8a09420ac48658cad726e0a6997687ceac4151e3 (diff)
downloadrust-7674edeebac02ce9b3ee6434e5a4ab6a085005f9.tar.gz
rust-7674edeebac02ce9b3ee6434e5a4ab6a085005f9.zip
Detect long types in E0308 and write them to disk
On type error with long types, print an abridged type and write the full
type to disk.

Print the widest possible short type while still fitting in the
terminal.
Diffstat (limited to 'compiler/rustc_middle/src')
-rw-r--r--compiler/rustc_middle/src/ty/error.rs28
1 files changed, 18 insertions, 10 deletions
diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs
index d83e17574a0..cc3101bc83b 100644
--- a/compiler/rustc_middle/src/ty/error.rs
+++ b/compiler/rustc_middle/src/ty/error.rs
@@ -986,8 +986,8 @@ fn foo(&self) -> Self::T { String::new() }
     }
 
     pub fn short_ty_string(self, ty: Ty<'tcx>) -> (String, Option<PathBuf>) {
-        let length_limit = 50;
-        let type_limit = 4;
+        let length_limit = self.sess.diagnostic_width().saturating_sub(20);
+        let mut type_limit = 50;
         let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS)
             .pretty_print_type(ty)
             .expect("could not write to `String`")
@@ -995,14 +995,22 @@ fn foo(&self) -> Self::T { String::new() }
         if regular.len() <= length_limit {
             return (regular, None);
         }
-        let short = FmtPrinter::new_with_limit(
-            self,
-            hir::def::Namespace::TypeNS,
-            rustc_session::Limit(type_limit),
-        )
-        .pretty_print_type(ty)
-        .expect("could not write to `String`")
-        .into_buffer();
+        let mut short;
+        loop {
+            // Look for the longest properly trimmed path that still fits in lenght_limit.
+            short = FmtPrinter::new_with_limit(
+                self,
+                hir::def::Namespace::TypeNS,
+                rustc_session::Limit(type_limit),
+            )
+            .pretty_print_type(ty)
+            .expect("could not write to `String`")
+            .into_buffer();
+            if short.len() <= length_limit || type_limit == 0 {
+                break;
+            }
+            type_limit -= 1;
+        }
         if regular == short {
             return (regular, None);
         }