Skip to content

Commit f144122

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 cc452f6 + 3fa28cb commit f144122

26 files changed

+124
-56
lines changed

src/librustc/traits/error_reporting.rs

+65-13
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

+1-2
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

+1-2
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

+1-1
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

+1-1
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

+1-1
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

-1
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

+1-1
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

+1-1
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

+1-1
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]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
fn main() {
1212
// Unconstrained type:
1313
format!("{:?}", None);
14-
//~^ ERROR unable to infer enough type information about `T` [E0282]
14+
//~^ ERROR type annotations needed [E0282]
1515
}

src/test/compile-fail/issue-6458-3.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::mem;
1212

1313
fn main() {
1414
mem::transmute(0);
15-
//~^ ERROR unable to infer enough type information about `U` [E0282]
15+
//~^ ERROR type annotations needed [E0282]
1616
//~| NOTE cannot infer type for `U`
17-
//~| NOTE type annotations or generic parameter binding
1817
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ pub fn foo<State>(_: TypeWithState<State>) {}
1717

1818
pub fn bar() {
1919
foo(TypeWithState(marker::PhantomData));
20-
//~^ ERROR unable to infer enough type information about `State` [E0282]
20+
//~^ ERROR type annotations needed [E0282]
2121
//~| NOTE cannot infer type for `State`
22-
//~| NOTE type annotations or generic parameter binding
2322
}
2423

2524
fn main() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
fn main() {
1212
let v = &[];
13-
let it = v.iter(); //~ ERROR unable to infer enough type information about `T` [E0282]
13+
let it = v.iter(); //~ ERROR type annotations needed [E0282]
1414
//~| NOTE cannot infer type for `T`
15-
//~| NOTE type annotations or generic parameter binding
15+
//~| NOTE consider giving `it` a type
1616
}

src/test/compile-fail/method-ambig-one-trait-unknown-int-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl foo for Vec<isize> {
3232
fn m1() {
3333
// we couldn't infer the type of the vector just based on calling foo()...
3434
let mut x = Vec::new();
35-
//~^ ERROR unable to infer enough type information about `T` [E0282]
35+
//~^ ERROR type annotations needed [E0282]
3636
x.foo();
3737
}
3838

src/test/compile-fail/traits-multidispatch-convert-ambig-dest.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ where T : Convert<U>
3434

3535
fn a() {
3636
test(22, std::default::Default::default());
37-
//~^ ERROR unable to infer enough type information about `U` [E0282]
37+
//~^ ERROR type annotations needed [E0282]
3838
//~| NOTE cannot infer type for `U`
39-
//~| NOTE type annotations or generic parameter binding
4039
}
4140

4241
fn main() {}

src/test/compile-fail/unconstrained-none.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// Issue #5062
1212

1313
fn main() {
14-
None; //~ ERROR unable to infer enough type information about `T` [E0282]
14+
None; //~ ERROR type annotations needed [E0282]
1515
//~| NOTE cannot infer type for `T`
16-
//~| NOTE type annotations or generic parameter binding
1716
}

src/test/compile-fail/unconstrained-ref.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct S<'a, T:'a> {
1313
}
1414

1515
fn main() {
16-
S { o: &None }; //~ ERROR unable to infer enough type information about `T` [E0282]
16+
S { o: &None }; //~ ERROR type annotations needed [E0282]
1717
//~| NOTE cannot infer type for `T`
18-
//~| NOTE type annotations or generic parameter binding
1918
}

src/test/compile-fail/vector-no-ann.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
fn main() {
1313
let _foo = Vec::new();
14-
//~^ ERROR unable to infer enough type information about `T` [E0282]
14+
//~^ ERROR type annotations needed [E0282]
1515
//~| NOTE cannot infer type for `T`
16-
//~| NOTE type annotations or generic parameter binding
16+
//~| NOTE consider giving `_foo` a type
1717
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let (x,) = (vec![],);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-38812-2.rs:12:17
3+
|
4+
12 | let (x,) = (vec![],);
5+
| ---- ^^^^^^ cannot infer type for `T`
6+
| |
7+
| consider giving a type to pattern
8+
|
9+
= note: this error originates in a macro outside of the current crate
10+
11+
error: aborting due to previous error
12+

src/test/ui/codemap_tests/repair_span_std_macros.rs src/test/ui/codemap_tests/issue-38812.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-38812.rs:12:13
3+
|
4+
12 | let x = vec![];
5+
| - ^^^^^^ cannot infer type for `T`
6+
| |
7+
| consider giving `x` a type
8+
|
9+
= note: this error originates in a macro outside of the current crate
10+
11+
error: aborting due to previous error
12+

src/test/ui/codemap_tests/repair_span_std_macros.stderr

-11
This file was deleted.

src/test/ui/missing-items/missing-type-parameter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0282]: unable to infer enough type information about `X`
1+
error[E0282]: type annotations needed
22
--> $DIR/missing-type-parameter.rs:14:5
33
|
44
14 | foo();
55
| ^^^ cannot infer type for `X`
6-
|
7-
= note: type annotations or generic parameter binding required
86

97
error: aborting due to previous error
108

0 commit comments

Comments
 (0)