Skip to content

Commit dd57275

Browse files
committed
shrink const infer error
1 parent 56d8a93 commit dd57275

10 files changed

+57
-10
lines changed

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
88
use rustc_middle::hir::map::Map;
99
use rustc_middle::ty::print::Print;
1010
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
11-
use rustc_middle::ty::{self, DefIdTree, Ty};
11+
use rustc_middle::ty::{self, DefIdTree, InferConst, Ty};
1212
use rustc_span::source_map::DesugaringKind;
1313
use rustc_span::symbol::kw;
1414
use rustc_span::Span;
@@ -569,12 +569,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
569569
local_visitor.visit_expr(expr);
570570
}
571571

572+
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
573+
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin.span
574+
} else {
575+
local_visitor.target_span
576+
};
577+
572578
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-
);
579+
let mut err =
580+
self.tcx.sess.struct_span_err_with_code(span, "type annotations needed", error_code);
578581

579582
err.note("unable to infer the value of a const parameter");
580583

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: unable to infer the value of a const parameter
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: unable to infer the value of a const parameter
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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
|
77
= note: unable to infer the value of a const parameter
88

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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
|
77
= note: unable to infer the value of a const parameter
88

0 commit comments

Comments
 (0)