Skip to content

Commit

Permalink
improve const infer err
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 14, 2020
1 parent dd57275 commit 035f879
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 9 deletions.
14 changes: 12 additions & 2 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
};
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/infer/unify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/infer/method-chain.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/infer/method-chain.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/infer/method-chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ impl Foo {

fn main() {
Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 035f879

Please sign in to comment.