]> git.lizzy.rs Git - rust.git/commitdiff
improve const infer err
authorBastian Kauschke <bastian_kauschke@hotmail.de>
Mon, 14 Sep 2020 08:08:32 +0000 (10:08 +0200)
committerBastian Kauschke <bastian_kauschke@hotmail.de>
Mon, 14 Sep 2020 08:08:32 +0000 (10:08 +0200)
compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
compiler/rustc_middle/src/infer/unify_key.rs
src/test/ui/const-generics/infer/cannot-infer-const-args.full.stderr
src/test/ui/const-generics/infer/cannot-infer-const-args.min.stderr
src/test/ui/const-generics/infer/method-chain.full.stderr
src/test/ui/const-generics/infer/method-chain.min.stderr
src/test/ui/const-generics/infer/method-chain.rs
src/test/ui/const-generics/infer/uninferred-consts.full.stderr
src/test/ui/const-generics/infer/uninferred-consts.min.stderr

index 9be234a7bb8bd23d320a481c56f0e78e03d78ef1..f87406c2ce469c97f15da28c3d9950abb90942d6 100644 (file)
@@ -6,6 +6,7 @@
 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 @@ pub fn need_type_info_err_const(
             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 @@ pub fn need_type_info_err_const(
         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
     }
index 2580ac6bebd86cbfa437c0671aaf433c9b6b46ba..a60a17befeffdce2b344112e4508b5910445d03e 100644 (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,
 }
index 053139787edf975a82216b0a7b25e098808436d1..84e75cc3764167ca8b3975039a06967e72beb156 100644 (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
 
index 053139787edf975a82216b0a7b25e098808436d1..84e75cc3764167ca8b3975039a06967e72beb156 100644 (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
 
index 06e4c2e7bc6bceebd64e57e1a642e107731dd43f..e65bc3f109681227c401e5010a61956cfcfbb29f 100644 (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
 
index 06e4c2e7bc6bceebd64e57e1a642e107731dd43f..e65bc3f109681227c401e5010a61956cfcfbb29f 100644 (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
 
index b3184642f36b4e2d5cb31a105b5588cc56d4a404..9389ca20d106fd7d54e3c7dc1dc2cfb6bf3bad9b 100644 (file)
@@ -19,4 +19,4 @@ fn baz<const N: usize>(self) -> Foo {
 
 fn main() {
     Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
-}
\ No newline at end of file
+}
index 6af49ba64523b7a49f2f301f99cab574663fc228..e47b6bd5dc691f49dc65c0a5cdd6db608ed97f35 100644 (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
 
index 6af49ba64523b7a49f2f301f99cab574663fc228..e47b6bd5dc691f49dc65c0a5cdd6db608ed97f35 100644 (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