improve const infer err

This commit is contained in:
Bastian Kauschke 2020-09-14 10:08:32 +02:00
parent dd57275c3e
commit 035f879166
9 changed files with 20 additions and 9 deletions

View File

@ -6,6 +6,7 @@ use rustc_hir::def::{DefKind, Namespace};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
use rustc_middle::hir::map::Map;
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
use rustc_middle::ty::print::Print;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
@ -569,8 +570,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
local_visitor.visit_expr(expr);
}
let mut param_name = None;
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin.span
let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
if let ConstVariableOriginKind::ConstParameterDefinition(param) = origin.kind {
param_name = Some(param);
}
origin.span
} else {
local_visitor.target_span
};
@ -579,7 +585,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let mut err =
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
err.note("unable to infer the value of a const parameter");
if let Some(param_name) = param_name {
err.note(&format!("cannot infer the value of the const parameter `{}`", param_name));
} else {
err.note("unable to infer the value of a const parameter");
}
err
}

View File

@ -124,6 +124,7 @@ pub struct ConstVariableOrigin {
pub enum ConstVariableOriginKind {
MiscVariable,
ConstInference,
// FIXME(const_generics): Consider storing the `DefId` of the param here.
ConstParameterDefinition(Symbol),
SubstitutionPlaceholder,
}

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | foo();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `X`
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | foo();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `X`
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | Foo.bar().bar().bar().bar().baz();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `N`
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | Foo.bar().bar().bar().bar().baz();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `N`
error: aborting due to previous error

View File

@ -19,4 +19,4 @@ impl Foo {
fn main() {
Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
}
}

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | Foo.foo();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `N`
error: aborting due to previous error

View File

@ -4,7 +4,7 @@ error[E0282]: type annotations needed
LL | Foo.foo();
| ^^^
|
= note: unable to infer the value of a const parameter
= note: cannot infer the value of the const parameter `N`
error: aborting due to previous error