Skip to content

Commit

Permalink
Hide more of long types in E0271
Browse files Browse the repository at this point in the history
Fix #40186.
  • Loading branch information
estebank committed Jan 11, 2023
1 parent b22c152 commit f1ffe82
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
18 changes: 15 additions & 3 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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> {
Expand All @@ -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> {
Expand Down
29 changes: 27 additions & 2 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
33 changes: 33 additions & 0 deletions tests/ui/diagnostic-width/E0271.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// compile-flags: --diagnostic-width=40
// normalize-stderr-test: "long-type-\d+" -> "long-type-hash"
trait Future {
type Error;
}

impl<T, E> Future for Result<T, E> {
type Error = E;
}

impl<T> Future for Option<T> {
type Error = ();
}

struct Foo;

fn foo() -> Box<dyn Future<Error=Foo>> {
Box::new( //~ ERROR E0271
Ok::<_, ()>(
Err::<(), _>(
Ok::<_, ()>(
Err::<(), _>(
Ok::<_, ()>(
Err::<(), _>(Some(5))
)
)
)
)
)
)
}
fn main() {
}
23 changes: 23 additions & 0 deletions tests/ui/diagnostic-width/E0271.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0271]: type mismatch resolving `<Result<Result<(), Result<Result<(), Result<Result<(), Option<{integer}>>, ...>>, ...>>, ...> as Future>::Error == Foo`
--> $DIR/E0271.rs:18:5
|
LL | / Box::new(
LL | | Ok::<_, ()>(
LL | | Err::<(), _>(
LL | | Ok::<_, ()>(
... |
LL | | )
LL | | )
| |_____^ type mismatch resolving `<Result<Result<(), Result<Result<(), ...>, ...>>, ...> as Future>::Error == Foo`
|
note: expected this to be `Foo`
--> $DIR/E0271.rs:8:18
|
LL | type Error = E;
| ^
= note: required for the cast from `Result<Result<..., ...>, ...>` to the object type `dyn Future<Error = Foo>`
= note: the full name for the casted type has been written to '$TEST_BUILD_DIR/diagnostic-width/E0271/E0271.long-type-hash.txt'

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.
4 changes: 2 additions & 2 deletions tests/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0271]: type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
error[E0271]: type mismatch resolving `for<'r> <L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
--> $DIR/issue-62203-hrtb-ice.rs:39:9
|
LL | let v = Unit2.m(
Expand All @@ -10,7 +10,7 @@ LL | | f: |x| {
... |
LL | | },
LL | | },
| |_________^ type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
| |_________^ type mismatch resolving `for<'r> <L<[closure@issue-62203-hrtb-ice.rs:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
|
note: expected this to be `<_ as Ty<'_>>::V`
--> $DIR/issue-62203-hrtb-ice.rs:21:14
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/impl-trait/bound-normalization-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait>::Assoc`
--> $DIR/bound-normalization-fail.rs:25:32
|
LL | fn foo_fail<T: Trait>() -> impl FooLike<Output = T::Assoc> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait>::Assoc`
LL |
LL | Foo(())
| ------- return type was inferred to be `Foo<()>` here
Expand All @@ -28,11 +28,11 @@ LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output = T::Assoc> {
= note: see issue #103532 <https://github.com/rust-lang/rust/issues/103532> for more information
= help: add `#![feature(impl_trait_projections)]` to the crate attributes to enable

error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as lifetimes::Trait<'a>>::Assoc`
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait<'a>>::Assoc`
--> $DIR/bound-normalization-fail.rs:41:41
|
LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output = T::Assoc> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as lifetimes::Trait<'a>>::Assoc`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait<'a>>::Assoc`
...
LL | Foo(())
| ------- return type was inferred to be `Foo<()>` here
Expand Down

0 comments on commit f1ffe82

Please sign in to comment.