Skip to content

Commit f1ffe82

Browse files
committed
Hide more of long types in E0271
Fix #40186.
1 parent b22c152 commit f1ffe82

File tree

7 files changed

+123
-16
lines changed

7 files changed

+123
-16
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ pub trait PrettyPrinter<'tcx>:
283283
/// This is typically the case for all non-`'_` regions.
284284
fn should_print_region(&self, region: ty::Region<'tcx>) -> bool;
285285

286+
fn reset_type_limit(&mut self) {}
287+
286288
// Defaults (should not be overridden):
287289

288290
/// 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> {
19811983
self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
19821984
}
19831985

1986+
fn reset_type_limit(&mut self) {
1987+
self.printed_type_count = 0;
1988+
}
1989+
19841990
fn const_infer_name(&self, id: ty::ConstVid<'tcx>) -> Option<Symbol> {
19851991
self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
19861992
}
@@ -2722,11 +2728,15 @@ define_print_and_forward_display! {
27222728
}
27232729

27242730
ty::SubtypePredicate<'tcx> {
2725-
p!(print(self.a), " <: ", print(self.b))
2731+
p!(print(self.a), " <: ");
2732+
cx.reset_type_limit();
2733+
p!(print(self.b))
27262734
}
27272735

27282736
ty::CoercePredicate<'tcx> {
2729-
p!(print(self.a), " -> ", print(self.b))
2737+
p!(print(self.a), " -> ");
2738+
cx.reset_type_limit();
2739+
p!(print(self.b))
27302740
}
27312741

27322742
ty::TraitPredicate<'tcx> {
@@ -2738,7 +2748,9 @@ define_print_and_forward_display! {
27382748
}
27392749

27402750
ty::ProjectionPredicate<'tcx> {
2741-
p!(print(self.projection_ty), " == ", print(self.term))
2751+
p!(print(self.projection_ty), " == ");
2752+
cx.reset_type_limit();
2753+
p!(print(self.term))
27422754
}
27432755

27442756
ty::Term<'tcx> {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,19 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17241724
.and_then(|(predicate, _, normalized_term, expected_term)| {
17251725
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term)
17261726
})
1727-
.unwrap_or_else(|| format!("type mismatch resolving `{}`", predicate));
1727+
.unwrap_or_else(|| {
1728+
with_forced_trimmed_paths!(format!(
1729+
"type mismatch resolving `{}`",
1730+
self.resolve_vars_if_possible(predicate)
1731+
.print(FmtPrinter::new_with_limit(
1732+
self.tcx,
1733+
Namespace::TypeNS,
1734+
rustc_session::Limit(10),
1735+
))
1736+
.unwrap()
1737+
.into_buffer()
1738+
))
1739+
});
17281740
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
17291741

17301742
let secondary_span = match predicate.kind().skip_binder() {
@@ -1755,7 +1767,20 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17551767
kind: hir::ImplItemKind::Type(ty),
17561768
..
17571769
}),
1758-
) => Some((ty.span, format!("type mismatch resolving `{}`", predicate))),
1770+
) => Some((
1771+
ty.span,
1772+
with_forced_trimmed_paths!(format!(
1773+
"type mismatch resolving `{}`",
1774+
self.resolve_vars_if_possible(predicate)
1775+
.print(FmtPrinter::new_with_limit(
1776+
self.tcx,
1777+
Namespace::TypeNS,
1778+
rustc_session::Limit(5),
1779+
))
1780+
.unwrap()
1781+
.into_buffer()
1782+
)),
1783+
)),
17591784
_ => None,
17601785
}),
17611786
_ => None,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -2622,11 +2622,25 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26222622
}
26232623
}
26242624
ObligationCauseCode::ObjectCastObligation(concrete_ty, object_ty) => {
2625-
err.note(&format!(
2626-
"required for the cast from `{}` to the object type `{}`",
2627-
self.ty_to_string(concrete_ty),
2628-
self.ty_to_string(object_ty)
2629-
));
2625+
let (concrete_ty, concrete_file) =
2626+
self.tcx.short_ty_string(self.resolve_vars_if_possible(concrete_ty));
2627+
let (object_ty, object_file) =
2628+
self.tcx.short_ty_string(self.resolve_vars_if_possible(object_ty));
2629+
err.note(&with_forced_trimmed_paths!(format!(
2630+
"required for the cast from `{concrete_ty}` to the object type `{object_ty}`",
2631+
)));
2632+
if let Some(file) = concrete_file {
2633+
err.note(&format!(
2634+
"the full name for the casted type has been written to '{}'",
2635+
file.display(),
2636+
));
2637+
}
2638+
if let Some(file) = object_file {
2639+
err.note(&format!(
2640+
"the full name for the object type has been written to '{}'",
2641+
file.display(),
2642+
));
2643+
}
26302644
}
26312645
ObligationCauseCode::Coercion { source: _, target } => {
26322646
err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));

tests/ui/diagnostic-width/E0271.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// compile-flags: --diagnostic-width=40
2+
// normalize-stderr-test: "long-type-\d+" -> "long-type-hash"
3+
trait Future {
4+
type Error;
5+
}
6+
7+
impl<T, E> Future for Result<T, E> {
8+
type Error = E;
9+
}
10+
11+
impl<T> Future for Option<T> {
12+
type Error = ();
13+
}
14+
15+
struct Foo;
16+
17+
fn foo() -> Box<dyn Future<Error=Foo>> {
18+
Box::new( //~ ERROR E0271
19+
Ok::<_, ()>(
20+
Err::<(), _>(
21+
Ok::<_, ()>(
22+
Err::<(), _>(
23+
Ok::<_, ()>(
24+
Err::<(), _>(Some(5))
25+
)
26+
)
27+
)
28+
)
29+
)
30+
)
31+
}
32+
fn main() {
33+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0271]: type mismatch resolving `<Result<Result<(), Result<Result<(), Result<Result<(), Option<{integer}>>, ...>>, ...>>, ...> as Future>::Error == Foo`
2+
--> $DIR/E0271.rs:18:5
3+
|
4+
LL | / Box::new(
5+
LL | | Ok::<_, ()>(
6+
LL | | Err::<(), _>(
7+
LL | | Ok::<_, ()>(
8+
... |
9+
LL | | )
10+
LL | | )
11+
| |_____^ type mismatch resolving `<Result<Result<(), Result<Result<(), ...>, ...>>, ...> as Future>::Error == Foo`
12+
|
13+
note: expected this to be `Foo`
14+
--> $DIR/E0271.rs:8:18
15+
|
16+
LL | type Error = E;
17+
| ^
18+
= note: required for the cast from `Result<Result<..., ...>, ...>` to the object type `dyn Future<Error = Foo>`
19+
= note: the full name for the casted type has been written to '$TEST_BUILD_DIR/diagnostic-width/E0271/E0271.long-type-hash.txt'
20+
21+
error: aborting due to previous error
22+
23+
For more information about this error, try `rustc --explain E0271`.

tests/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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`
1+
error[E0271]: type mismatch resolving `for<'r> <L<[[email protected]:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
22
--> $DIR/issue-62203-hrtb-ice.rs:39:9
33
|
44
LL | let v = Unit2.m(
@@ -10,7 +10,7 @@ LL | | f: |x| {
1010
... |
1111
LL | | },
1212
LL | | },
13-
| |_________^ 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`
13+
| |_________^ type mismatch resolving `for<'r> <L<[[email protected]:42:16]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
1414
|
1515
note: expected this to be `<_ as Ty<'_>>::V`
1616
--> $DIR/issue-62203-hrtb-ice.rs:21:14

tests/ui/impl-trait/bound-normalization-fail.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
1+
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait>::Assoc`
22
--> $DIR/bound-normalization-fail.rs:25:32
33
|
44
LL | fn foo_fail<T: Trait>() -> impl FooLike<Output = T::Assoc> {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait>::Assoc`
66
LL |
77
LL | Foo(())
88
| ------- return type was inferred to be `Foo<()>` here
@@ -28,11 +28,11 @@ LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output = T::Assoc> {
2828
= note: see issue #103532 <https://github.com/rust-lang/rust/issues/103532> for more information
2929
= help: add `#![feature(impl_trait_projections)]` to the crate attributes to enable
3030

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

0 commit comments

Comments
 (0)