Skip to content

Commit 55b9088

Browse files
authored
Rollup merge of rust-lang#76699 - lcnr:const-infer-err, r=varkor
improve const infer error cc rust-lang#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
2 parents f4be98a + 035f879 commit 55b9088

11 files changed

+73
-15
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use rustc_hir::def::{DefKind, Namespace};
66
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
77
use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
88
use rustc_middle::hir::map::Map;
9+
use rustc_middle::infer::unify_key::ConstVariableOriginKind;
910
use rustc_middle::ty::print::Print;
1011
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
11-
use rustc_middle::ty::{self, DefIdTree, Ty};
12+
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
1213
use rustc_span::source_map::DesugaringKind;
1314
use rustc_span::symbol::kw;
1415
use rustc_span::Span;
@@ -569,14 +570,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
569570
local_visitor.visit_expr(expr);
570571
}
571572

573+
let mut param_name = None;
574+
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
575+
let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
576+
if let ConstVariableOriginKind::ConstParameterDefinition(param) = origin.kind {
577+
param_name = Some(param);
578+
}
579+
origin.span
580+
} else {
581+
local_visitor.target_span
582+
};
583+
572584
let error_code = error_code.into();
573-
let mut err = self.tcx.sess.struct_span_err_with_code(
574-
local_visitor.target_span,
575-
"type annotations needed",
576-
error_code,
577-
);
585+
let mut err =
586+
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
578587

579-
err.note("unable to infer the value of a const parameter");
588+
if let Some(param_name) = param_name {
589+
err.note(&format!("cannot infer the value of the const parameter `{}`", param_name));
590+
} else {
591+
err.note("unable to infer the value of a const parameter");
592+
}
580593

581594
err
582595
}

compiler/rustc_middle/src/infer/unify_key.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub struct ConstVariableOrigin {
124124
pub enum ConstVariableOriginKind {
125125
MiscVariable,
126126
ConstInference,
127+
// FIXME(const_generics): Consider storing the `DefId` of the param here.
127128
ConstParameterDefinition(Symbol),
128129
SubstitutionPlaceholder,
129130
}

src/test/ui/const-generics/cannot-infer-const-args.full.stderr src/test/ui/const-generics/infer/cannot-infer-const-args.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `X`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/cannot-infer-const-args.min.stderr src/test/ui/const-generics/infer/cannot-infer-const-args.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0282]: type annotations needed
44
LL | foo();
55
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `X`
88

99
error: aborting due to previous error
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/method-chain.rs:21:33
3+
|
4+
LL | Foo.bar().bar().bar().bar().baz();
5+
| ^^^
6+
|
7+
= note: cannot infer the value of the const parameter `N`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/method-chain.rs:21:33
3+
|
4+
LL | Foo.bar().bar().bar().bar().baz();
5+
| ^^^
6+
|
7+
= note: cannot infer the value of the const parameter `N`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// revisions: full min
2+
3+
#![cfg_attr(full, feature(const_generics))]
4+
#![cfg_attr(full, allow(incomplete_features))]
5+
#![cfg_attr(min, feature(min_const_generics))]
6+
7+
struct Foo;
8+
9+
impl Foo {
10+
fn bar(self) -> Foo {
11+
Foo
12+
}
13+
14+
fn baz<const N: usize>(self) -> Foo {
15+
println!("baz: {}", N);
16+
Foo
17+
}
18+
}
19+
20+
fn main() {
21+
Foo.bar().bar().bar().bar().baz(); //~ ERROR type annotations needed
22+
}

src/test/ui/const-generics/uninferred-consts.full.stderr src/test/ui/const-generics/infer/uninferred-consts.full.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/uninferred-consts.rs:14:5
2+
--> $DIR/uninferred-consts.rs:14:9
33
|
44
LL | Foo.foo();
5-
| ^^^^^^^^^
5+
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

src/test/ui/const-generics/uninferred-consts.min.stderr src/test/ui/const-generics/infer/uninferred-consts.min.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/uninferred-consts.rs:14:5
2+
--> $DIR/uninferred-consts.rs:14:9
33
|
44
LL | Foo.foo();
5-
| ^^^^^^^^^
5+
| ^^^
66
|
7-
= note: unable to infer the value of a const parameter
7+
= note: cannot infer the value of the const parameter `N`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)