Skip to content

Commit c6017ba

Browse files
committed
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.
1 parent 7b9105d commit c6017ba

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

compiler/rustc_middle/src/ty/error.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::borrow::Cow;
2+
use std::fs::File;
23
use std::hash::{DefaultHasher, Hash, Hasher};
4+
use std::io::{Read, Write};
35
use std::path::PathBuf;
46

57
use rustc_errors::pluralize;
@@ -250,8 +252,8 @@ impl<'tcx> TyCtxt<'tcx> {
250252
}
251253

252254
let width = self.sess.diagnostic_width();
253-
let length_limit = width.saturating_sub(30);
254-
if regular.len() <= width {
255+
let length_limit = width / 2;
256+
if regular.len() <= width * 2 / 3 {
255257
return regular;
256258
}
257259
let short = self.ty_string_with_limit(ty, length_limit);
@@ -265,7 +267,20 @@ impl<'tcx> TyCtxt<'tcx> {
265267
*path = Some(path.take().unwrap_or_else(|| {
266268
self.output_filenames(()).temp_path_ext(&format!("long-type-{hash}.txt"), None)
267269
}));
268-
match std::fs::write(path.as_ref().unwrap(), &format!("{regular}\n")) {
270+
let Ok(mut file) =
271+
File::options().create(true).read(true).append(true).open(&path.as_ref().unwrap())
272+
else {
273+
return regular;
274+
};
275+
276+
// Do not write the same type to the file multiple times.
277+
let mut contents = String::new();
278+
let _ = file.read_to_string(&mut contents);
279+
if let Some(_) = contents.lines().find(|line| line == &regular) {
280+
return short;
281+
}
282+
283+
match write!(file, "{regular}\n") {
269284
Ok(_) => short,
270285
Err(_) => regular,
271286
}

0 commit comments

Comments
 (0)