Skip to content

Commit 035f879

Browse files
committed
improve const infer err
1 parent dd57275 commit 035f879

9 files changed

+20
-9
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::def::{DefKind, Namespace};
66
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
77
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
88
use rustc_middle::hir::map::Map;
9+
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
910
use rustc_middle::ty::print::Print;
1011
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
1112
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
@@ -569,8 +570,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
569570
local_visitor.visit_expr(expr);
570571
}
571572

573+
let mut param_name = None;
572574
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
573-
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin.span
575+
let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
576+
if let ConstVariableOriginKind::ConstParameterDefinition(param) = origin.kind {
577+
param_name = Some(param);
578+
}
579+
origin.span
574580
} else {
575581
local_visitor.target_span
576582
};
@@ -579,7 +585,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
579585
let mut err =
580586
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
581587

582-
err.note("unable to infer the value of a const parameter");
588+
if let Some(param_name) = param_name {
589+
err.note(&format!("cannot infer the value of the const parameter `{}`", param_name));
590+
} else {
591+
err.note("unable to infer the value of a const parameter");
592+
}
583593

584594
err
585595
}

compiler/rustc_middle/src/infer/unify_key.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub struct ConstVariableOrigin {
124124
pub enum ConstVariableOriginKind {
125125
MiscVariable,
126126
ConstInference,
127+
// FIXME(const_generics): Consider storing the `DefId` of the param here.
127128
ConstParameterDefinition(Symbol),
128129
SubstitutionPlaceholder,
129130
}

src/test/ui/const-generics/infer/cannot-infer-const-args.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `X`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/infer/cannot-infer-const-args.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `X`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/infer/method-chain.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | Foo.bar().bar().bar().bar().baz();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/infer/method-chain.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | Foo.bar().bar().bar().bar().baz();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/infer/method-chain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ impl Foo {
1919

2020
fn main() {
2121
Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
22-
}
22+
}

src/test/ui/const-generics/infer/uninferred-consts.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | Foo.foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/infer/uninferred-consts.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | Foo.foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)