Skip to content

Commit 5f73b00

Browse files
committed
Auto merge of #118389 - estebank:type-verbosity, r=b-naber
Tweak `short_ty_string` to reduce number of files When shortening types and writing them to disk, make `short_ty_string` capable of reusing the same file, instead of writing a file per shortened type.
2 parents eeff92a + 9d846fc commit 5f73b00

File tree

14 files changed

+124
-119
lines changed

14 files changed

+124
-119
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
262262
rcvr_ty: Ty<'tcx>,
263263
rcvr_expr: &hir::Expr<'tcx>,
264264
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
265-
let (ty_str, _ty_file) = self.tcx.short_ty_string(rcvr_ty);
265+
let mut file = None;
266+
let ty_str = self.tcx.short_ty_string(rcvr_ty, &mut file);
266267
let mut err = struct_span_err!(
267268
self.tcx.sess,
268269
rcvr_expr.span,
@@ -280,6 +281,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
280281
"a writer is needed before this format string",
281282
);
282283
};
284+
if let Some(file) = file {
285+
err.note(format!("the full type name has been written to '{}'", file.display()));
286+
}
283287

284288
err
285289
}
@@ -299,11 +303,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
299303
let mode = no_match_data.mode;
300304
let tcx = self.tcx;
301305
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
302-
let ((mut ty_str, ty_file), short_ty_str) =
306+
let mut ty_file = None;
307+
let (mut ty_str, short_ty_str) =
303308
if trait_missing_method && let ty::Dynamic(predicates, _, _) = rcvr_ty.kind() {
304-
((predicates.to_string(), None), with_forced_trimmed_paths!(predicates.to_string()))
309+
(predicates.to_string(), with_forced_trimmed_paths!(predicates.to_string()))
305310
} else {
306-
(tcx.short_ty_string(rcvr_ty), with_forced_trimmed_paths!(rcvr_ty.to_string()))
311+
(
312+
tcx.short_ty_string(rcvr_ty, &mut ty_file),
313+
with_forced_trimmed_paths!(rcvr_ty.to_string()),
314+
)
307315
};
308316
let is_method = mode == Mode::MethodCall;
309317
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+45-56
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17431743
}
17441744
}
17451745

1746-
if let Some((expected, found, exp_p, found_p)) = expected_found {
1746+
if let Some((expected, found, path)) = expected_found {
17471747
let (expected_label, found_label, exp_found) = match exp_found {
17481748
Mismatch::Variable(ef) => (
17491749
ef.expected.prefix_string(self.tcx),
@@ -1869,40 +1869,31 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18691869
}
18701870
TypeError::Sorts(values) => {
18711871
let extra = expected == found;
1872-
let sort_string = |ty: Ty<'tcx>, path: Option<PathBuf>| {
1873-
let mut s = match (extra, ty.kind()) {
1874-
(true, ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) => {
1875-
let sm = self.tcx.sess.source_map();
1876-
let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1877-
format!(
1878-
" (opaque type at <{}:{}:{}>)",
1879-
sm.filename_for_diagnostics(&pos.file.name),
1880-
pos.line,
1881-
pos.col.to_usize() + 1,
1882-
)
1883-
}
1884-
(true, ty::Alias(ty::Projection, proj))
1885-
if self.tcx.is_impl_trait_in_trait(proj.def_id) =>
1886-
{
1887-
let sm = self.tcx.sess.source_map();
1888-
let pos = sm.lookup_char_pos(self.tcx.def_span(proj.def_id).lo());
1889-
format!(
1890-
" (trait associated opaque type at <{}:{}:{}>)",
1891-
sm.filename_for_diagnostics(&pos.file.name),
1892-
pos.line,
1893-
pos.col.to_usize() + 1,
1894-
)
1895-
}
1896-
(true, _) => format!(" ({})", ty.sort_string(self.tcx)),
1897-
(false, _) => "".to_string(),
1898-
};
1899-
if let Some(path) = path {
1900-
s.push_str(&format!(
1901-
"\nthe full type name has been written to '{}'",
1902-
path.display(),
1903-
));
1872+
let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1873+
(true, ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) => {
1874+
let sm = self.tcx.sess.source_map();
1875+
let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1876+
format!(
1877+
" (opaque type at <{}:{}:{}>)",
1878+
sm.filename_for_diagnostics(&pos.file.name),
1879+
pos.line,
1880+
pos.col.to_usize() + 1,
1881+
)
19041882
}
1905-
s
1883+
(true, ty::Alias(ty::Projection, proj))
1884+
if self.tcx.is_impl_trait_in_trait(proj.def_id) =>
1885+
{
1886+
let sm = self.tcx.sess.source_map();
1887+
let pos = sm.lookup_char_pos(self.tcx.def_span(proj.def_id).lo());
1888+
format!(
1889+
" (trait associated opaque type at <{}:{}:{}>)",
1890+
sm.filename_for_diagnostics(&pos.file.name),
1891+
pos.line,
1892+
pos.col.to_usize() + 1,
1893+
)
1894+
}
1895+
(true, _) => format!(" ({})", ty.sort_string(self.tcx)),
1896+
(false, _) => "".to_string(),
19061897
};
19071898
if !(values.expected.is_simple_text(self.tcx)
19081899
&& values.found.is_simple_text(self.tcx))
@@ -1933,9 +1924,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19331924
expected,
19341925
&found_label,
19351926
found,
1936-
&sort_string(values.expected, exp_p),
1937-
&sort_string(values.found, found_p),
1927+
&sort_string(values.expected),
1928+
&sort_string(values.found),
19381929
);
1930+
if let Some(path) = path {
1931+
diag.note(format!(
1932+
"the full type name has been written to '{}'",
1933+
path.display(),
1934+
));
1935+
}
19391936
}
19401937
}
19411938
}
@@ -2101,7 +2098,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21012098
if let &(MatchExpressionArm(box MatchExpressionArmCause { source, .. })
21022099
| BlockTailExpression(.., source)) = code
21032100
&& let hir::MatchSource::TryDesugar(_) = source
2104-
&& let Some((expected_ty, found_ty, _, _)) = self.values_str(trace.values)
2101+
&& let Some((expected_ty, found_ty, _)) = self.values_str(trace.values)
21052102
{
21062103
suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
21072104
found: found_ty.content(),
@@ -2219,8 +2216,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22192216
fn values_str(
22202217
&self,
22212218
values: ValuePairs<'tcx>,
2222-
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>, Option<PathBuf>)>
2223-
{
2219+
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>)> {
22242220
match values {
22252221
infer::Regions(exp_found) => self.expected_found_str(exp_found),
22262222
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
@@ -2233,7 +2229,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22332229
found: exp_found.found.print_trait_sugared(),
22342230
};
22352231
match self.expected_found_str(pretty_exp_found) {
2236-
Some((expected, found, _, _)) if expected == found => {
2232+
Some((expected, found, _)) if expected == found => {
22372233
self.expected_found_str(exp_found)
22382234
}
22392235
ret => ret,
@@ -2245,16 +2241,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22452241
return None;
22462242
}
22472243
let (exp, fnd) = self.cmp_fn_sig(&exp_found.expected, &exp_found.found);
2248-
Some((exp, fnd, None, None))
2244+
Some((exp, fnd, None))
22492245
}
22502246
}
22512247
}
22522248

22532249
fn expected_found_str_term(
22542250
&self,
22552251
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
2256-
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>, Option<PathBuf>)>
2257-
{
2252+
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>)> {
22582253
let exp_found = self.resolve_vars_if_possible(exp_found);
22592254
if exp_found.references_error() {
22602255
return None;
@@ -2269,25 +2264,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22692264
let len = self.tcx.sess().diagnostic_width() + 40;
22702265
let exp_s = exp.content();
22712266
let fnd_s = fnd.content();
2272-
let mut exp_p = None;
2273-
let mut fnd_p = None;
2267+
let mut path = None;
22742268
if exp_s.len() > len {
2275-
let (exp_s, exp_path) = self.tcx.short_ty_string(expected);
2269+
let exp_s = self.tcx.short_ty_string(expected, &mut path);
22762270
exp = DiagnosticStyledString::highlighted(exp_s);
2277-
exp_p = exp_path;
22782271
}
22792272
if fnd_s.len() > len {
2280-
let (fnd_s, fnd_path) = self.tcx.short_ty_string(found);
2273+
let fnd_s = self.tcx.short_ty_string(found, &mut path);
22812274
fnd = DiagnosticStyledString::highlighted(fnd_s);
2282-
fnd_p = fnd_path;
22832275
}
2284-
(exp, fnd, exp_p, fnd_p)
2276+
(exp, fnd, path)
22852277
}
22862278
_ => (
22872279
DiagnosticStyledString::highlighted(exp_found.expected.to_string()),
22882280
DiagnosticStyledString::highlighted(exp_found.found.to_string()),
22892281
None,
2290-
None,
22912282
),
22922283
})
22932284
}
@@ -2296,8 +2287,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
22962287
fn expected_found_str<T: fmt::Display + TypeFoldable<TyCtxt<'tcx>>>(
22972288
&self,
22982289
exp_found: ty::error::ExpectedFound<T>,
2299-
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>, Option<PathBuf>)>
2300-
{
2290+
) -> Option<(DiagnosticStyledString, DiagnosticStyledString, Option<PathBuf>)> {
23012291
let exp_found = self.resolve_vars_if_possible(exp_found);
23022292
if exp_found.references_error() {
23032293
return None;
@@ -2307,7 +2297,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23072297
DiagnosticStyledString::highlighted(exp_found.expected.to_string()),
23082298
DiagnosticStyledString::highlighted(exp_found.found.to_string()),
23092299
None,
2310-
None,
23112300
))
23122301
}
23132302

@@ -2591,8 +2580,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25912580

25922581
if let infer::Subtype(ref sup_trace) = sup_origin
25932582
&& let infer::Subtype(ref sub_trace) = sub_origin
2594-
&& let Some((sup_expected, sup_found, _, _)) = self.values_str(sup_trace.values)
2595-
&& let Some((sub_expected, sub_found, _, _)) = self.values_str(sub_trace.values)
2583+
&& let Some((sup_expected, sup_found, _)) = self.values_str(sup_trace.values)
2584+
&& let Some((sub_expected, sub_found, _)) = self.values_str(sub_trace.values)
25962585
&& sub_expected == sup_expected
25972586
&& sub_found == sup_found
25982587
{

compiler/rustc_infer/src/infer/error_reporting/note.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2222
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
2323
span: trace.cause.span,
2424
requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
25-
expected_found: self.values_str(trace.values).map(|(e, f, _, _)| (e, f)),
25+
expected_found: self.values_str(trace.values).map(|(e, f, _)| (e, f)),
2626
}
2727
.add_to_diagnostic(err),
2828
infer::Reborrow(span) => {

compiler/rustc_middle/src/ty/error.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -345,33 +345,35 @@ impl<'tcx> TyCtxt<'tcx> {
345345
short
346346
}
347347

348-
pub fn short_ty_string(self, ty: Ty<'tcx>) -> (String, Option<PathBuf>) {
348+
pub fn short_ty_string(self, ty: Ty<'tcx>, path: &mut Option<PathBuf>) -> String {
349349
let regular = FmtPrinter::print_string(self, hir::def::Namespace::TypeNS, |cx| {
350350
cx.pretty_print_type(ty)
351351
})
352352
.expect("could not write to `String`");
353353

354354
if !self.sess.opts.unstable_opts.write_long_types_to_disk {
355-
return (regular, None);
355+
return regular;
356356
}
357357

358358
let width = self.sess.diagnostic_width();
359359
let length_limit = width.saturating_sub(30);
360360
if regular.len() <= width {
361-
return (regular, None);
361+
return regular;
362362
}
363363
let short = self.ty_string_with_limit(ty, length_limit);
364364
if regular == short {
365-
return (regular, None);
365+
return regular;
366366
}
367-
// Multiple types might be shortened in a single error, ensure we create a file for each.
367+
// Ensure we create an unique file for the type passed in when we create a file.
368368
let mut s = DefaultHasher::new();
369369
ty.hash(&mut s);
370370
let hash = s.finish();
371-
let path = self.output_filenames(()).temp_path_ext(&format!("long-type-{hash}.txt"), None);
372-
match std::fs::write(&path, &regular) {
373-
Ok(_) => (short, Some(path)),
374-
Err(_) => (regular, None),
371+
*path = Some(path.take().unwrap_or_else(|| {
372+
self.output_filenames(()).temp_path_ext(&format!("long-type-{hash}.txt"), None)
373+
}));
374+
match std::fs::write(path.as_ref().unwrap(), &format!("{regular}\n")) {
375+
Ok(_) => short,
376+
Err(_) => regular,
375377
}
376378
}
377379
}

0 commit comments

Comments
 (0)