Skip to content

Commit 1997791

Browse files
authored
Rollup merge of rust-lang#39361 - cengizIO:master, r=arielb1
Improve error message for uninferrable types rust-lang#38812 Hello, I tried to improve the error message for uninferrable types. The error code is `E0282`. ```rust error[E0282]: type annotations needed --> /home/cengizIO/issue38812.rs:2:11 | 2 | let x = vec![]; | - ^^^^^^ cannot infer type for `T` | | | consider giving `x` a type | = note: this error originates in a macro outside of the current crate ``` and ```rust error[E0282]: type annotations needed --> /home/cengizIO/issue38812.rs:2:15 | 2 | let (x,) = (vec![],); | ---- ^^^^^^ cannot infer type for `T` | | | consider giving a type to pattern | = note: this error originates in a macro outside of the current crate ``` Rust compiler now tries to find uninferred `local`s with type `_` and adds them into the error message. I'm probably wrong on wording that I used. Please feel free to suggest better alternatives. Thanks @nikomatsakis for mentoring 🍺 Any comments/feedback is more than welcome! Thank you
2 parents 4cde461 + 3fa28cb commit 1997791

26 files changed

+124
-56
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ use util::nodemap::{FxHashMap, FxHashSet};
3939
use std::cmp;
4040
use std::fmt;
4141
use syntax::ast;
42+
use hir::{intravisit, Local, Pat};
43+
use hir::intravisit::{Visitor, NestedVisitorMap};
4244
use syntax_pos::{DUMMY_SP, Span};
4345
use errors::DiagnosticBuilder;
4446

@@ -60,6 +62,30 @@ impl<'a, 'gcx, 'tcx> TraitErrorKey<'tcx> {
6062
}
6163
}
6264

65+
struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
66+
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
67+
target_ty: &'a Ty<'tcx>,
68+
found_pattern: Option<&'a Pat>,
69+
}
70+
71+
impl<'a, 'gcx, 'tcx> Visitor<'a> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
72+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'a> {
73+
NestedVisitorMap::None
74+
}
75+
76+
fn visit_local(&mut self, local: &'a Local) {
77+
if let Some(&ty) = self.infcx.tables.borrow().node_types.get(&local.id) {
78+
let ty = self.infcx.resolve_type_vars_if_possible(&ty);
79+
let is_match = ty.walk().any(|t| t == *self.target_ty);
80+
81+
if is_match && self.found_pattern.is_none() {
82+
self.found_pattern = Some(&*local.pat);
83+
}
84+
}
85+
intravisit::walk_local(self, local);
86+
}
87+
}
88+
6389
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6490
pub fn report_fulfillment_errors(&self, errors: &Vec<FulfillmentError<'tcx>>) {
6591
for error in errors {
@@ -775,7 +801,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
775801
self.tcx.lang_items.sized_trait()
776802
.map_or(false, |sized_id| sized_id == trait_ref.def_id())
777803
{
778-
self.need_type_info(obligation.cause.span, self_ty);
804+
self.need_type_info(obligation, self_ty);
779805
} else {
780806
let mut err = struct_span_err!(self.tcx.sess,
781807
obligation.cause.span, E0283,
@@ -793,7 +819,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
793819
// Same hacky approach as above to avoid deluging user
794820
// with error messages.
795821
if !ty.references_error() && !self.tcx.sess.has_errors() {
796-
self.need_type_info(obligation.cause.span, ty);
822+
self.need_type_info(obligation, ty);
797823
}
798824
}
799825

@@ -857,27 +883,53 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
857883
})
858884
}
859885

860-
861-
fn need_type_info(&self, span: Span, ty: Ty<'tcx>) {
862-
let ty = self.resolve_type_vars_if_possible(&ty);
863-
let name = if let ty::TyInfer(ty::TyVar(ty_vid)) = ty.sty {
886+
fn extract_type_name(&self, ty: &'a Ty<'tcx>) -> String {
887+
if let ty::TyInfer(ty::TyVar(ty_vid)) = (*ty).sty {
864888
let ty_vars = self.type_variables.borrow();
865889
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
866-
*ty_vars.var_origin(ty_vid)
867-
{
890+
*ty_vars.var_origin(ty_vid) {
868891
name.to_string()
869892
} else {
870893
ty.to_string()
871894
}
872895
} else {
873896
ty.to_string()
897+
}
898+
}
899+
900+
fn need_type_info(&self, obligation: &PredicateObligation<'tcx>, ty: Ty<'tcx>) {
901+
let ty = self.resolve_type_vars_if_possible(&ty);
902+
let name = self.extract_type_name(&ty);
903+
let ref cause = obligation.cause;
904+
905+
let mut err = struct_span_err!(self.tcx.sess,
906+
cause.span,
907+
E0282,
908+
"type annotations needed");
909+
910+
err.span_label(cause.span, &format!("cannot infer type for `{}`", name));
911+
912+
let expr = self.tcx.hir.expect_expr(cause.body_id);
913+
914+
let mut local_visitor = FindLocalByTypeVisitor {
915+
infcx: &self,
916+
target_ty: &ty,
917+
found_pattern: None,
874918
};
875919

876-
let mut err = struct_span_err!(self.tcx.sess, span, E0282,
877-
"unable to infer enough type information about `{}`",
878-
name);
879-
err.note("type annotations or generic parameter binding required");
880-
err.span_label(span, &format!("cannot infer type for `{}`", name));
920+
local_visitor.visit_expr(expr);
921+
922+
if let Some(pattern) = local_visitor.found_pattern {
923+
let pattern_span = pattern.span;
924+
if let Some(simple_name) = pattern.simple_name() {
925+
err.span_label(pattern_span,
926+
&format!("consider giving `{}` a type",
927+
simple_name));
928+
} else {
929+
err.span_label(pattern_span, &format!("consider giving a type to pattern"));
930+
}
931+
}
932+
881933
err.emit();
882934
}
883935

src/test/compile-fail/issue-12187-1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn new<T>() -> &'static T {
1414

1515
fn main() {
1616
let &v = new();
17-
//~^ ERROR unable to infer enough type information about `_` [E0282]
17+
//~^ ERROR type annotations needed [E0282]
1818
//~| NOTE cannot infer type for `_`
19-
//~| NOTE type annotations or generic parameter binding
2019
}

src/test/compile-fail/issue-12187-2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn new<'r, T>() -> &'r T {
1414

1515
fn main() {
1616
let &v = new();
17-
//~^ ERROR unable to infer enough type information about `_` [E0282]
17+
//~^ ERROR type annotations needed [E0282]
1818
//~| NOTE cannot infer type for `_`
19-
//~| NOTE type annotations or generic parameter binding
2019
}

src/test/compile-fail/issue-16966.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:type annotations or generic parameter binding required
11+
// error-pattern:type annotations needed
1212
fn main() {
1313
panic!(
1414
std::default::Default::default()

src/test/compile-fail/issue-17551.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ use std::marker;
1313
struct B<T>(marker::PhantomData<T>);
1414

1515
fn main() {
16-
let foo = B(marker::PhantomData); //~ ERROR unable to infer enough type information
16+
let foo = B(marker::PhantomData); //~ ERROR type annotations needed
1717
let closure = || foo;
1818
}

src/test/compile-fail/issue-18159.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let x; //~ ERROR unable to infer enough type information
12+
let x; //~ ERROR type annotations needed
1313
}

src/test/compile-fail/issue-23041.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ fn main()
1515
let b:Box<Any> = Box::new(bar as fn(_)->_);
1616
b.downcast_ref::<fn(_)->_>(); //~ ERROR E0282
1717
//~| NOTE cannot infer type for `_`
18-
//~| NOTE type annotations or generic parameter binding required
1918
}

src/test/compile-fail/issue-23046.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ pub fn let_<'var, VAR, F: for<'v: 'var> Fn(Expr<'v, VAR>) -> Expr<'v, VAR>>
2525

2626
fn main() {
2727
let ex = |x| {
28-
let_(add(x,x), |y| { //~ ERROR unable to infer enough type information about `VAR`
28+
let_(add(x,x), |y| { //~ ERROR type annotations needed
2929
let_(add(x, x), |x|x)})};
3030
}

src/test/compile-fail/issue-24013.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ fn main() {
1313
let a = 1;
1414
let b = 2;
1515
unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
16-
//~^ ERROR unable to infer enough type information about `_`
16+
//~^ ERROR type annotations needed
1717
}

src/test/compile-fail/issue-5062.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
// except according to those terms.
1010

1111
fn main() { format!("{:?}", None); }
12-
//~^ ERROR unable to infer enough type information about `T` [E0282]
12+
//~^ ERROR type annotations needed [E0282]

0 commit comments

Comments
 (0)