From c6017badb4ca22b85c70916746c233340be53c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 25 Oct 2024 02:41:39 +0000 Subject: [PATCH] Fix type shortening writing to file Make sure that we append to the file for long ty paths. Do not write the same type more than once. Shorten the calculated width a bit. --- compiler/rustc_middle/src/ty/error.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index c49824bb418..43d243b0584 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -1,5 +1,7 @@ use std::borrow::Cow; +use std::fs::File; use std::hash::{DefaultHasher, Hash, Hasher}; +use std::io::{Read, Write}; use std::path::PathBuf; use rustc_errors::pluralize; @@ -250,8 +252,8 @@ impl<'tcx> TyCtxt<'tcx> { } let width = self.sess.diagnostic_width(); - let length_limit = width.saturating_sub(30); - if regular.len() <= width { + let length_limit = width / 2; + if regular.len() <= width * 2 / 3 { return regular; } let short = self.ty_string_with_limit(ty, length_limit); @@ -265,7 +267,20 @@ impl<'tcx> TyCtxt<'tcx> { *path = Some(path.take().unwrap_or_else(|| { self.output_filenames(()).temp_path_ext(&format!("long-type-{hash}.txt"), None) })); - match std::fs::write(path.as_ref().unwrap(), &format!("{regular}\n")) { + let Ok(mut file) = + File::options().create(true).read(true).append(true).open(&path.as_ref().unwrap()) + else { + return regular; + }; + + // Do not write the same type to the file multiple times. + let mut contents = String::new(); + let _ = file.read_to_string(&mut contents); + if let Some(_) = contents.lines().find(|line| line == ®ular) { + return short; + } + + match write!(file, "{regular}\n") { Ok(_) => short, Err(_) => regular, }