1
Fork 0

Hide more of long types in E0271

Fix #40186.
This commit is contained in:
Esteban Küber 2023-01-08 18:41:09 +00:00
parent b22c152958
commit f1ffe823cf
7 changed files with 123 additions and 16 deletions

View file

@ -283,6 +283,8 @@ pub trait PrettyPrinter<'tcx>:
/// This is typically the case for all non-`'_` regions.
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool;
fn reset_type_limit(&mut self) {}
// Defaults (should not be overridden):
/// If possible, this returns a global path resolving to `def_id` that is visible
@ -1981,6 +1983,10 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
}
fn reset_type_limit(&mut self) {
self.printed_type_count = 0;
}
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<Symbol> {
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
}
@ -2722,11 +2728,15 @@ define_print_and_forward_display! {
}
ty::SubtypePredicate<'tcx> {
p!(print(self.a), " <: ", print(self.b))
p!(print(self.a), " <: ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::CoercePredicate<'tcx> {
p!(print(self.a), " -> ", print(self.b))
p!(print(self.a), " -> ");
cx.reset_type_limit();
p!(print(self.b))
}
ty::TraitPredicate<'tcx> {
@ -2738,7 +2748,9 @@ define_print_and_forward_display! {
}
ty::ProjectionPredicate<'tcx> {
p!(print(self.projection_ty), " == ", print(self.term))
p!(print(self.projection_ty), " == ");
cx.reset_type_limit();
p!(print(self.term))
}
ty::Term<'tcx> {

View file

@ -1724,7 +1724,19 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.and_then(|(predicate, _, normalized_term, expected_term)| {
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term)
})
.unwrap_or_else(|| format!("type mismatch resolving `{}`", predicate));
.unwrap_or_else(|| {
with_forced_trimmed_paths!(format!(
"type mismatch resolving `{}`",
self.resolve_vars_if_possible(predicate)
.print(FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(10),
))
.unwrap()
.into_buffer()
))
});
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
let secondary_span = match predicate.kind().skip_binder() {
@ -1755,7 +1767,20 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
kind: hir::ImplItemKind::Type(ty),
..
}),
) => Some((ty.span, format!("type mismatch resolving `{}`", predicate))),
) => Some((
ty.span,
with_forced_trimmed_paths!(format!(
"type mismatch resolving `{}`",
self.resolve_vars_if_possible(predicate)
.print(FmtPrinter::new_with_limit(
self.tcx,
Namespace::TypeNS,
rustc_session::Limit(5),
))
.unwrap()
.into_buffer()
)),
)),
_ => None,
}),
_ => None,

View file

@ -2622,11 +2622,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
}
ObligationCauseCode::ObjectCastObligation(concrete_ty, object_ty) => {
err.note(&format!(
"required for the cast from `{}` to the object type `{}`",
self.ty_to_string(concrete_ty),
self.ty_to_string(object_ty)
));
let (concrete_ty, concrete_file) =
self.tcx.short_ty_string(self.resolve_vars_if_possible(concrete_ty));
let (object_ty, object_file) =
self.tcx.short_ty_string(self.resolve_vars_if_possible(object_ty));
err.note(&with_forced_trimmed_paths!(format!(
"required for the cast from `{concrete_ty}` to the object type `{object_ty}`",
)));
if let Some(file) = concrete_file {
err.note(&format!(
"the full name for the casted type has been written to '{}'",
file.display(),
));
}
if let Some(file) = object_file {
err.note(&format!(
"the full name for the object type has been written to '{}'",
file.display(),
));
}
}
ObligationCauseCode::Coercion { source: _, target } => {
err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));