Skip to content

Commit 5d8b835

Browse files
committed
Refer to uninferred const params by their name, instead of { _: _ }
When the value of a const param isn't inferred, replace it with the param name from the definition.
1 parent 6544097 commit 5d8b835

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::infer::type_variable::TypeVariableOriginKind;
2-
use crate::infer::InferCtxt;
2+
use crate::infer::{InferCtxt, Symbol};
33
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
44
use rustc_hir as hir;
55
use rustc_hir::def::{DefKind, Namespace};
@@ -898,8 +898,17 @@ impl<'tcx> TypeFolder<'tcx> for ResolvedTypeParamEraser<'tcx> {
898898
let substs: Vec<_> = substs
899899
.iter()
900900
.zip(generics.params.iter())
901-
.map(|(subst, param)| match &param.kind {
902-
ty::GenericParamDefKind::Type { has_default: true, .. } => subst,
901+
.map(|(subst, param)| match &(subst.unpack(), &param.kind) {
902+
(_, ty::GenericParamDefKind::Type { has_default: true, .. }) => subst,
903+
(crate::infer::GenericArgKind::Const(c), _) => {
904+
match c.val {
905+
ty::ConstKind::Infer(..) => {
906+
// Replace not yet inferred const params with their def name.
907+
self.tcx().mk_const_param(param.index, param.name, c.ty).into()
908+
}
909+
_ => subst,
910+
}
911+
}
903912
_ => subst.super_fold_with(self),
904913
})
905914
.collect();
@@ -937,8 +946,19 @@ impl<'tcx> TypeFolder<'tcx> for ResolvedTypeParamEraser<'tcx> {
937946
| ty::FnPtr(_)
938947
| ty::Opaque(..)
939948
| ty::Projection(_)
940-
| ty::Never
941-
| ty::Array(..) => t.super_fold_with(self),
949+
| ty::Never => t.super_fold_with(self),
950+
ty::Array(ty, c) => {
951+
self.tcx().mk_ty(ty::Array(
952+
self.fold_ty(ty),
953+
match c.val {
954+
ty::ConstKind::Infer(..) => {
955+
// Replace not yet inferred const params with their def name.
956+
self.tcx().mk_const_param(0, Symbol::intern("N"), c.ty).into()
957+
}
958+
_ => c,
959+
},
960+
))
961+
}
942962
// We don't want to hide type params that haven't been resolved yet.
943963
// This would be the type that will be written out with the type param
944964
// name in the output.

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<{_: _}>`, where the type parameter `N` is specified
7+
| consider giving `foo` the explicit type `Foo<N>`, where the type parameter `N` is specified
88

99
error: aborting due to previous error
1010

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 `[_; _]`, where the type parameter `N` is specified
7+
| consider giving this pattern the explicit type `[_; N]`, where the type parameter `N` is specified
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)