Skip to content

Commit

Permalink
Rollup merge of #76699 - lcnr:const-infer-err, r=varkor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dylan-DPC committed Sep 16, 2020
2 parents 54d7728 + 035f879 commit 3f9e7fc
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 15 deletions.
27 changes: 20 additions & 7 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,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;
Expand Down Expand Up @@ -569,14 +570,26 @@ 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 {
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
};

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 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
11 changes: 11 additions & 0 deletions src/test/ui/const-generics/infer/method-chain.full.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions src/test/ui/const-generics/infer/method-chain.min.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions src/test/ui/const-generics/infer/method-chain.rs
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed
--> $DIR/uninferred-consts.rs:14:5
--> $DIR/uninferred-consts.rs:14:9
|
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
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed
--> $DIR/uninferred-consts.rs:14:5
--> $DIR/uninferred-consts.rs:14:9
|
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 3f9e7fc

Please sign in to comment.