Rollup merge of #76699 - lcnr:const-infer-err, r=varkor
improve const infer error cc #72328 reduces it from ``` error[E0282]: type annotations needed --> src/main.rs:17:5 | 17 | Foo.bar().bar().bar().bar().baz(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: unable to infer the value of a const parameter ``` to ``` error[E0282]: type annotations needed --> $DIR/method-chain.rs:21:33 | LL | Foo.bar().bar().bar().bar().baz(); | ^^^ | = note: cannot infer the value of the const parameter `N` ``` r? @varkor
This commit is contained in:
commit
3f9e7fc049
11 changed files with 74 additions and 16 deletions
|
@ -6,9 +6,10 @@ 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, Ty};
|
||||
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
|
||||
use rustc_span::source_map::DesugaringKind;
|
||||
use rustc_span::symbol::kw;
|
||||
use rustc_span::Span;
|
||||
|
@ -569,14 +570,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||
local_visitor.visit_expr(expr);
|
||||
}
|
||||
|
||||
let error_code = error_code.into();
|
||||
let mut err = self.tcx.sess.struct_span_err_with_code(
|
||||
local_visitor.target_span,
|
||||
"type annotations needed",
|
||||
error_code,
|
||||
);
|
||||
let mut param_name = None;
|
||||
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
|
||||
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
|
||||
};
|
||||
|
||||
err.note("unable to infer the value of a const parameter");
|
||||
let error_code = error_code.into();
|
||||
let mut err =
|
||||
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue