Skip to content

Commit 6544097

Browse files
committed
Only shown relevant type params in E0283 label
When we point at a binding to suggest giving it a type, erase all the type for ADTs that have been resolved, leaving only the ones that could not be inferred. For small shallow types this is not a problem, but for big nested types with lots of params, this can otherwise cause a lot of unnecessary visual output.
1 parent e5038e2 commit 6544097

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed

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

+99-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::hir::map::Map;
1010
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
1111
use rustc_middle::ty::print::Print;
1212
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
13-
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty, TyCtxt};
13+
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty, TyCtxt, TypeFoldable, TypeFolder};
1414
use rustc_span::symbol::kw;
1515
use rustc_span::Span;
1616
use std::borrow::Cow;
@@ -589,6 +589,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
589589
format!("the explicit type `{}`, with the type parameters specified", ty)
590590
}
591591
Some(ty) if is_named_and_not_impl_trait(ty) && ty.to_string() != arg_data.name => {
592+
let ty = ResolvedTypeParamEraser::new(self.tcx).fold_ty(ty);
593+
let ty = ErrTypeParamEraser(self.tcx).fold_ty(ty);
592594
let ty = ty_to_string(ty);
593595
format!(
594596
"the explicit type `{}`, where the type parameter `{}` is specified",
@@ -868,3 +870,99 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
868870
err
869871
}
870872
}
873+
874+
/// Turn resolved type params into `[type error]` to signal we don't want to display them.
875+
struct ResolvedTypeParamEraser<'tcx> {
876+
tcx: TyCtxt<'tcx>,
877+
level: usize,
878+
}
879+
880+
impl<'tcx> ResolvedTypeParamEraser<'tcx> {
881+
fn new(tcx: TyCtxt<'tcx>) -> Self {
882+
ResolvedTypeParamEraser { tcx, level: 0 }
883+
}
884+
}
885+
impl<'tcx> TypeFolder<'tcx> for ResolvedTypeParamEraser<'tcx> {
886+
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
887+
self.tcx
888+
}
889+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
890+
self.level += 1;
891+
let t = match t.kind() {
892+
// We'll hide this type only if all its type params are hidden as well.
893+
ty::Adt(def, substs) => {
894+
let generics = self.tcx().generics_of(def.did);
895+
// Account for params with default values, like `Vec`, where we
896+
// want to show `Vec<T>`, not `Vec<T, _>`. If we replaced that
897+
// subst, then we'd get the incorrect output, so we passthrough.
898+
let substs: Vec<_> = substs
899+
.iter()
900+
.zip(generics.params.iter())
901+
.map(|(subst, param)| match &param.kind {
902+
ty::GenericParamDefKind::Type { has_default: true, .. } => subst,
903+
_ => subst.super_fold_with(self),
904+
})
905+
.collect();
906+
if self.level == 1
907+
|| substs.iter().any(|subst| match subst.unpack() {
908+
ty::subst::GenericArgKind::Type(t) => match t.kind() {
909+
ty::Error(_) => false,
910+
_ => true,
911+
},
912+
// Account for `const` params here, otherwise `doesnt_infer.rs`
913+
// shows `_` instead of `Foo<{ _: u32 }>`
914+
ty::subst::GenericArgKind::Const(_) => true,
915+
_ => false,
916+
})
917+
{
918+
let substs = self.tcx().intern_substs(&substs[..]);
919+
self.tcx().mk_ty(ty::Adt(def, substs))
920+
} else {
921+
self.tcx().ty_error()
922+
}
923+
}
924+
ty::Ref(_, ty, _) => {
925+
let ty = self.fold_ty(ty);
926+
match ty.kind() {
927+
// Avoid `&_`, these can be safely presented as `_`.
928+
ty::Error(_) => self.tcx().ty_error(),
929+
_ => t.super_fold_with(self),
930+
}
931+
}
932+
// We could account for `()` if we wanted to replace it, but it's assured to be short.
933+
ty::Tuple(_)
934+
| ty::Slice(_)
935+
| ty::RawPtr(_)
936+
| ty::FnDef(..)
937+
| ty::FnPtr(_)
938+
| ty::Opaque(..)
939+
| ty::Projection(_)
940+
| ty::Never
941+
| ty::Array(..) => t.super_fold_with(self),
942+
// We don't want to hide type params that haven't been resolved yet.
943+
// This would be the type that will be written out with the type param
944+
// name in the output.
945+
ty::Infer(_) => t,
946+
// We don't want to hide the outermost type, only its type params.
947+
_ if self.level == 1 => t.super_fold_with(self),
948+
// Hide this type
949+
_ => self.tcx().ty_error(),
950+
};
951+
self.level -= 1;
952+
t
953+
}
954+
}
955+
956+
/// Replace `[type error]` with `ty::Infer(ty::Var)` to display `_`.
957+
struct ErrTypeParamEraser<'tcx>(TyCtxt<'tcx>);
958+
impl<'tcx> TypeFolder<'tcx> for ErrTypeParamEraser<'tcx> {
959+
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
960+
self.0
961+
}
962+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
963+
match t.kind() {
964+
ty::Error(_) => self.tcx().mk_ty_var(ty::TyVid::from_u32(0)),
965+
_ => t.super_fold_with(self),
966+
}
967+
}
968+
}

src/test/ui/const-generics/defaults/doesnt_infer.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `Foo<{_: u32}>`
44
LL | let foo = Foo::foo();
55
| --- ^^^^^^^^ cannot infer the value of const parameter `N`
66
| |
7-
| consider giving `foo` the explicit type `Foo<{_: u32}>`, where the type parameter `N` is specified
7+
| consider giving `foo` the explicit type `Foo<{_: _}>`, where the type parameter `N` is specified
88

99
error: aborting due to previous error
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let foo = new(1, ""); //~ ERROR E0283
3+
}
4+
5+
struct Bar<T, K, N: Default> {
6+
t: T,
7+
k: K,
8+
n: N,
9+
}
10+
11+
fn new<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
12+
Bar { t, k, n: Default::default() }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0283]: type annotations needed for `Bar<i32, &str, Z>`
2+
--> $DIR/erase-type-params-in-label.rs:2:15
3+
|
4+
LL | let foo = new(1, "");
5+
| --- ^^^ cannot infer type for type parameter `Z` declared on the function `new`
6+
| |
7+
| consider giving `foo` the explicit type `Bar<_, _, Z>`, where the type parameter `Z` is specified
8+
|
9+
= note: cannot satisfy `_: Default`
10+
note: required by a bound in `new`
11+
--> $DIR/erase-type-params-in-label.rs:11:17
12+
|
13+
LL | fn new<T, K, Z: Default>(t: T, k: K) -> Bar<T, K, Z> {
14+
| ^^^^^^^ required by this bound in `new`
15+
help: consider specifying the type arguments in the function call
16+
|
17+
LL | let foo = new::<T, K, Z>(1, "");
18+
| +++++++++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0283`.

src/test/ui/inference/issue-83606.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `[usize; _]`
44
LL | let _ = foo("foo"); //<- Do not suggest `foo::<N>("foo");`!
55
| - ^^^ cannot infer the value of const parameter `N` declared on the function `foo`
66
| |
7-
| consider giving this pattern the explicit type `[usize; _]`, where the type parameter `N` is specified
7+
| consider giving this pattern the explicit type `[_; _]`, where the type parameter `N` is specified
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)