Skip to content

Commit 39efaa0

Browse files
authored
Rollup merge of #136328 - estebank:long-ty-path, r=jieyouxu,lqd
Rework "long type names" printing logic Make it so more type-system types can be printed in a shortened version (like `Predicate`s). Centralize printing the information about the "full type name path". Make the "long type path" for the file where long types are written part of `Diag`, so that it becomes easier to keep track of it, and ensure it will always will be printed out last in the diagnostic by making its addition to the output implicit. Tweak the shortening of types in "expected/found" labels. Remove dead file `note.rs`.
2 parents 58a5f89 + 0751e90 commit 39efaa0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+331
-740
lines changed

compiler/rustc_borrowck/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ borrowck_lifetime_constraints_error =
9292
borrowck_limitations_implies_static =
9393
due to current limitations in the borrow checker, this implies a `'static` lifetime
9494
95-
borrowck_long_type_consider_verbose = consider using `--verbose` to print the full type name to the console
96-
borrowck_long_type_full_path = the full type name has been written to '{$path}'
97-
9895
borrowck_move_closure_suggestion =
9996
consider adding 'move' keyword before the nested closure
10097

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
289289
None => "value".to_owned(),
290290
};
291291
if needs_note {
292-
let mut path = None;
293-
let ty = self.infcx.tcx.short_ty_string(ty, &mut path);
292+
let ty = self.infcx.tcx.short_string(ty, err.long_ty_path());
294293
if let Some(local) = place.as_local() {
295294
let span = self.body.local_decls[local].source_info.span;
296295
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -306,11 +305,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
306305
place: &note_msg,
307306
});
308307
};
309-
if let Some(path) = path {
310-
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
311-
path: path.display().to_string(),
312-
});
313-
}
314308
}
315309

316310
if let UseSpans::FnSelfUse {

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -596,19 +596,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
596596
self.suggest_cloning(err, place_ty, expr, None);
597597
}
598598

599-
let mut path = None;
600-
let ty = self.infcx.tcx.short_ty_string(place_ty, &mut path);
599+
let ty = self.infcx.tcx.short_string(place_ty, err.long_ty_path());
601600
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
602601
is_partial_move: false,
603602
ty,
604603
place: &place_desc,
605604
span,
606605
});
607-
if let Some(path) = path {
608-
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
609-
path: path.display().to_string(),
610-
});
611-
}
612606
} else {
613607
binds_to.sort();
614608
binds_to.dedup();
@@ -635,19 +629,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
635629
self.suggest_cloning(err, place_ty, expr, Some(use_spans));
636630
}
637631

638-
let mut path = None;
639-
let ty = self.infcx.tcx.short_ty_string(place_ty, &mut path);
632+
let ty = self.infcx.tcx.short_string(place_ty, err.long_ty_path());
640633
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
641634
is_partial_move: false,
642635
ty,
643636
place: &place_desc,
644637
span: use_span,
645638
});
646-
if let Some(path) = path {
647-
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
648-
path: path.display().to_string(),
649-
});
650-
}
651639

652640
use_spans.args_subdiag(err, |args_span| {
653641
crate::session_diagnostics::CaptureArgLabel::MoveOutPlace {
@@ -845,19 +833,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
845833
self.suggest_cloning(err, bind_to.ty, expr, None);
846834
}
847835

848-
let mut path = None;
849-
let ty = self.infcx.tcx.short_ty_string(bind_to.ty, &mut path);
836+
let ty = self.infcx.tcx.short_string(bind_to.ty, err.long_ty_path());
850837
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
851838
is_partial_move: false,
852839
ty,
853840
place: place_desc,
854841
span: binding_span,
855842
});
856-
if let Some(path) = path {
857-
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
858-
path: path.display().to_string(),
859-
});
860-
}
861843
}
862844
}
863845

compiler/rustc_borrowck/src/session_diagnostics.rs

-7
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,6 @@ pub(crate) enum OnClosureNote<'a> {
458458
},
459459
}
460460

461-
#[derive(Subdiagnostic)]
462-
#[note(borrowck_long_type_full_path)]
463-
#[note(borrowck_long_type_consider_verbose)]
464-
pub(crate) struct LongTypePath {
465-
pub(crate) path: String,
466-
}
467-
468461
#[derive(Subdiagnostic)]
469462
pub(crate) enum TypeNoCopy<'a> {
470463
#[label(borrowck_ty_no_impl_copy)]

compiler/rustc_errors/src/diagnostic.rs

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::hash::{Hash, Hasher};
44
use std::marker::PhantomData;
55
use std::ops::{Deref, DerefMut};
66
use std::panic;
7+
use std::path::PathBuf;
78
use std::thread::panicking;
89

910
use rustc_data_structures::fx::FxIndexMap;
@@ -301,6 +302,7 @@ pub struct DiagInner {
301302

302303
pub is_lint: Option<IsLint>,
303304

305+
pub long_ty_path: Option<PathBuf>,
304306
/// With `-Ztrack_diagnostics` enabled,
305307
/// we print where in rustc this error was emitted.
306308
pub(crate) emitted_at: DiagLocation,
@@ -324,6 +326,7 @@ impl DiagInner {
324326
args: Default::default(),
325327
sort_span: DUMMY_SP,
326328
is_lint: None,
329+
long_ty_path: None,
327330
emitted_at: DiagLocation::caller(),
328331
}
329332
}
@@ -1293,9 +1296,37 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
12931296
/// `cancel`, etc. Afterwards, `drop` is the only code that will be run on
12941297
/// `self`.
12951298
fn take_diag(&mut self) -> DiagInner {
1299+
if let Some(path) = &self.long_ty_path {
1300+
self.note(format!(
1301+
"the full name for the type has been written to '{}'",
1302+
path.display()
1303+
));
1304+
self.note("consider using `--verbose` to print the full type name to the console");
1305+
}
12961306
Box::into_inner(self.diag.take().unwrap())
12971307
}
12981308

1309+
/// This method allows us to access the path of the file where "long types" are written to.
1310+
///
1311+
/// When calling `Diag::emit`, as part of that we will check if a `long_ty_path` has been set,
1312+
/// and if it has been then we add a note mentioning the file where the "long types" were
1313+
/// written to.
1314+
///
1315+
/// When calling `tcx.short_string()` after a `Diag` is constructed, the preferred way of doing
1316+
/// so is `tcx.short_string(ty, diag.long_ty_path())`. The diagnostic itself is the one that
1317+
/// keeps the existence of a "long type" anywhere in the diagnostic, so the note telling the
1318+
/// user where we wrote the file to is only printed once at most, *and* it makes it much harder
1319+
/// to forget to set it.
1320+
///
1321+
/// If the diagnostic hasn't been created before a "short ty string" is created, then you should
1322+
/// ensure that this method is called to set it `*diag.long_ty_path() = path`.
1323+
///
1324+
/// As a rule of thumb, if you see or add at least one `tcx.short_string()` call anywhere, in a
1325+
/// scope, `diag.long_ty_path()` should be called once somewhere close by.
1326+
pub fn long_ty_path(&mut self) -> &mut Option<PathBuf> {
1327+
&mut self.long_ty_path
1328+
}
1329+
12991330
/// Most `emit_producing_guarantee` functions use this as a starting point.
13001331
fn emit_producing_nothing(mut self) {
13011332
let diag = self.take_diag();

compiler/rustc_hir_typeck/src/method/suggest.rs

+14-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use core::ops::ControlFlow;
77
use std::borrow::Cow;
8+
use std::path::PathBuf;
89

910
use hir::Expr;
1011
use rustc_ast::ast::Mutability;
@@ -362,14 +363,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
362363

363364
fn suggest_missing_writer(&self, rcvr_ty: Ty<'tcx>, rcvr_expr: &hir::Expr<'tcx>) -> Diag<'_> {
364365
let mut file = None;
365-
let ty_str = self.tcx.short_ty_string(rcvr_ty, &mut file);
366366
let mut err = struct_span_code_err!(
367367
self.dcx(),
368368
rcvr_expr.span,
369369
E0599,
370370
"cannot write into `{}`",
371-
ty_str
371+
self.tcx.short_string(rcvr_ty, &mut file),
372372
);
373+
*err.long_ty_path() = file;
373374
err.span_note(
374375
rcvr_expr.span,
375376
"must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method",
@@ -380,11 +381,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
380381
"a writer is needed before this format string",
381382
);
382383
};
383-
if let Some(file) = file {
384-
err.note(format!("the full type name has been written to '{}'", file.display()));
385-
err.note("consider using `--verbose` to print the full type name to the console");
386-
}
387-
388384
err
389385
}
390386

@@ -595,7 +591,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
595591
(predicates.to_string(), with_forced_trimmed_paths!(predicates.to_string()))
596592
} else {
597593
(
598-
tcx.short_ty_string(rcvr_ty, &mut ty_file),
594+
tcx.short_string(rcvr_ty, &mut ty_file),
599595
with_forced_trimmed_paths!(rcvr_ty.to_string()),
600596
)
601597
};
@@ -624,6 +620,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
624620
span,
625621
item_name,
626622
&short_ty_str,
623+
&mut ty_file,
627624
) {
628625
return guar;
629626
}
@@ -635,6 +632,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
635632
item_kind,
636633
item_name,
637634
&short_ty_str,
635+
&mut ty_file,
638636
) {
639637
return guar;
640638
}
@@ -728,10 +726,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
728726
ty_str = short_ty_str;
729727
}
730728

731-
if let Some(file) = ty_file {
732-
err.note(format!("the full type name has been written to '{}'", file.display(),));
733-
err.note("consider using `--verbose` to print the full type name to the console");
734-
}
735729
if rcvr_ty.references_error() {
736730
err.downgrade_to_delayed_bug();
737731
}
@@ -1314,7 +1308,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13141308
bound_list.into_iter().map(|(_, path)| path).collect::<Vec<_>>().join("\n");
13151309
let actual_prefix = rcvr_ty.prefix_string(self.tcx);
13161310
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
1317-
let mut long_ty_file = None;
13181311
let (primary_message, label, notes) = if unimplemented_traits.len() == 1
13191312
&& unimplemented_traits_only
13201313
{
@@ -1329,7 +1322,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13291322
}
13301323
let OnUnimplementedNote { message, label, notes, .. } = self
13311324
.err_ctxt()
1332-
.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
1325+
.on_unimplemented_note(trait_ref, &obligation, &mut ty_file);
13331326
(message, label, notes)
13341327
})
13351328
.unwrap()
@@ -1343,15 +1336,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13431336
)
13441337
});
13451338
err.primary_message(primary_message);
1346-
if let Some(file) = long_ty_file {
1347-
err.note(format!(
1348-
"the full name for the type has been written to '{}'",
1349-
file.display(),
1350-
));
1351-
err.note(
1352-
"consider using `--verbose` to print the full type name to the console",
1353-
);
1354-
}
13551339
if let Some(label) = label {
13561340
custom_span_label = true;
13571341
err.span_label(span, label);
@@ -2403,6 +2387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24032387
span: Span,
24042388
item_name: Ident,
24052389
ty_str: &str,
2390+
long_ty_path: &mut Option<PathBuf>,
24062391
) -> Result<(), ErrorGuaranteed> {
24072392
if let SelfSource::MethodCall(expr) = source {
24082393
for (_, parent) in tcx.hir().parent_iter(expr.hir_id).take(5) {
@@ -2460,7 +2445,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24602445
);
24612446
if pick.is_ok() {
24622447
let range_span = parent_expr.span.with_hi(expr.span.hi());
2463-
return Err(self.dcx().emit_err(errors::MissingParenthesesInRange {
2448+
let mut err = self.dcx().create_err(errors::MissingParenthesesInRange {
24642449
span,
24652450
ty_str: ty_str.to_string(),
24662451
method_name: item_name.as_str().to_string(),
@@ -2469,7 +2454,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24692454
left: range_span.shrink_to_lo(),
24702455
right: range_span.shrink_to_hi(),
24712456
}),
2472-
}));
2457+
});
2458+
*err.long_ty_path() = long_ty_path.take();
2459+
return Err(err.emit());
24732460
}
24742461
}
24752462
}
@@ -2486,6 +2473,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24862473
item_kind: &str,
24872474
item_name: Ident,
24882475
ty_str: &str,
2476+
long_ty_path: &mut Option<PathBuf>,
24892477
) -> Result<(), ErrorGuaranteed> {
24902478
let found_candidate = all_traits(self.tcx)
24912479
.into_iter()
@@ -2526,6 +2514,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25262514
item_name,
25272515
ty_str
25282516
);
2517+
*err.long_ty_path() = long_ty_path.take();
25292518
let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
25302519
match expr.kind {
25312520
ExprKind::Lit(lit) => {

0 commit comments

Comments
 (0)