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::intravisit::{self, NestedVisitorMap, Visitor};
|
||||||
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
|
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
|
||||||
use rustc_middle::hir::map::Map;
|
use rustc_middle::hir::map::Map;
|
||||||
|
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
|
||||||
use rustc_middle::ty::print::Print;
|
use rustc_middle::ty::print::Print;
|
||||||
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
|
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::source_map::DesugaringKind;
|
||||||
use rustc_span::symbol::kw;
|
use rustc_span::symbol::kw;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
@ -569,14 +570,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
local_visitor.visit_expr(expr);
|
local_visitor.visit_expr(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
let error_code = error_code.into();
|
let mut param_name = None;
|
||||||
let mut err = self.tcx.sess.struct_span_err_with_code(
|
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
|
||||||
local_visitor.target_span,
|
let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
|
||||||
"type annotations needed",
|
if let ConstVariableOriginKind::ConstParameterDefinition(param) = origin.kind {
|
||||||
error_code,
|
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
|
err
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,6 +124,7 @@ pub struct ConstVariableOrigin {
|
||||||
pub enum ConstVariableOriginKind {
|
pub enum ConstVariableOriginKind {
|
||||||
MiscVariable,
|
MiscVariable,
|
||||||
ConstInference,
|
ConstInference,
|
||||||
|
// FIXME(const_generics): Consider storing the `DefId` of the param here.
|
||||||
ConstParameterDefinition(Symbol),
|
ConstParameterDefinition(Symbol),
|
||||||
SubstitutionPlaceholder,
|
SubstitutionPlaceholder,
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||||
LL | foo();
|
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
|
error: aborting due to previous error
|
||||||
|
|
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||||
LL | foo();
|
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
|
error: aborting due to previous error
|
||||||
|
|
11
src/test/ui/const-generics/infer/method-chain.full.stderr
Normal file
11
src/test/ui/const-generics/infer/method-chain.full.stderr
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0282`.
|
11
src/test/ui/const-generics/infer/method-chain.min.stderr
Normal file
11
src/test/ui/const-generics/infer/method-chain.min.stderr
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
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`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0282`.
|
22
src/test/ui/const-generics/infer/method-chain.rs
Normal file
22
src/test/ui/const-generics/infer/method-chain.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
// revisions: full min
|
||||||
|
|
||||||
|
#![cfg_attr(full, feature(const_generics))]
|
||||||
|
#![cfg_attr(full, allow(incomplete_features))]
|
||||||
|
#![cfg_attr(min, feature(min_const_generics))]
|
||||||
|
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl Foo {
|
||||||
|
fn bar(self) -> Foo {
|
||||||
|
Foo
|
||||||
|
}
|
||||||
|
|
||||||
|
fn baz<const N: usize>(self) -> Foo {
|
||||||
|
println!("baz: {}", N);
|
||||||
|
Foo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
error[E0282]: type annotations needed
|
error[E0282]: type annotations needed
|
||||||
--> $DIR/uninferred-consts.rs:14:5
|
--> $DIR/uninferred-consts.rs:14:9
|
||||||
|
|
|
|
||||||
LL | Foo.foo();
|
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
|
error: aborting due to previous error
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
error[E0282]: type annotations needed
|
error[E0282]: type annotations needed
|
||||||
--> $DIR/uninferred-consts.rs:14:5
|
--> $DIR/uninferred-consts.rs:14:9
|
||||||
|
|
|
|
||||||
LL | Foo.foo();
|
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
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue