Skip to content

Commit

Permalink
Alternative wording for inference failure
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jun 1, 2019
1 parent 8bb094d commit 65c2a7b
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 52 deletions.
36 changes: 13 additions & 23 deletions src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
hir_map: &'a hir::map::Map<'gcx>, hir_map: &'a hir::map::Map<'gcx>,
found_local_pattern: Option<&'gcx Pat>, found_local_pattern: Option<&'gcx Pat>,
found_arg_pattern: Option<&'gcx Pat>, found_arg_pattern: Option<&'gcx Pat>,
found_ty: Option<Ty<'tcx>>, found_ty: Option<String>,
} }


impl<'a, 'gcx, 'tcx> FindLocalByTypeVisitor<'a, 'gcx, 'tcx> { impl<'a, 'gcx, 'tcx> FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
fn visit_local(&mut self, local: &'gcx Local) { fn visit_local(&mut self, local: &'gcx Local) {
if let (None, Some(ty)) = (self.found_local_pattern, self.node_matches_type(local.hir_id)) { if let (None, Some(ty)) = (self.found_local_pattern, self.node_matches_type(local.hir_id)) {
self.found_local_pattern = Some(&*local.pat); self.found_local_pattern = Some(&*local.pat);
self.found_ty = Some(ty); self.found_ty = Some(ty.to_string());
} }
intravisit::walk_local(self, local); intravisit::walk_local(self, local);
} }
Expand All @@ -67,7 +67,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
self.node_matches_type(argument.hir_id), self.node_matches_type(argument.hir_id),
) { ) {
self.found_arg_pattern = Some(&*argument.pat); self.found_arg_pattern = Some(&*argument.pat);
self.found_ty = Some(ty); self.found_ty = Some(ty.to_string());
} }
} }
intravisit::walk_body(self, body); intravisit::walk_body(self, body);
Expand Down Expand Up @@ -108,7 +108,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let name = self.extract_type_name(&ty, None); let name = self.extract_type_name(&ty, None);


let mut err_span = span; let mut err_span = span;
let mut labels = vec![(span, InferCtxt::missing_type_msg(&name))];


let mut local_visitor = FindLocalByTypeVisitor { let mut local_visitor = FindLocalByTypeVisitor {
infcx: &self, infcx: &self,
Expand All @@ -124,10 +123,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
local_visitor.visit_expr(expr); local_visitor.visit_expr(expr);
} }


let ty_msg = match local_visitor.found_ty { let ty_msg = match &local_visitor.found_ty {
Some(ty) if &ty.to_string() != "_" => format!(" for `{}`", ty), Some(ty) if &ty[..] != "_" && ty != &name => format!(" in `{}`", ty),
_ => String::new(), _ => String::new(),
}; };
let mut labels = vec![(span, InferCtxt::missing_type_msg(&name, &ty_msg))];

if let Some(pattern) = local_visitor.found_arg_pattern { if let Some(pattern) = local_visitor.found_arg_pattern {
err_span = pattern.span; err_span = pattern.span;
// We don't want to show the default label for closures. // We don't want to show the default label for closures.
Expand All @@ -148,8 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
labels.clear(); labels.clear();
labels.push((pattern.span, format!( labels.push((pattern.span, format!(
"consider giving this closure parameter {}", "consider giving this closure parameter {}",
match local_visitor.found_ty { match &local_visitor.found_ty {
Some(ty) if &ty.to_string() != "_" => format!( Some(ty) if &ty[..] != "_" && ty != &name => format!(
"the type `{}` with the type parameter `{}` specified", "the type `{}` with the type parameter `{}` specified",
ty, ty,
name, name,
Expand All @@ -162,18 +163,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
match pattern.span.compiler_desugaring_kind() { match pattern.span.compiler_desugaring_kind() {
None => labels.push(( None => labels.push((
pattern.span, pattern.span,
format!( format!("consider giving `{}` a type", simple_ident),
"consider giving `{}` {}",
simple_ident,
match local_visitor.found_ty {
Some(ty) if &ty.to_string() != "_" => format!(
"the type `{}` with the type parameter `{}` specified",
ty,
name,
),
_ => "a type".to_owned(),
},
),
)), )),
Some(CompilerDesugaringKind::ForLoop) => labels.push(( Some(CompilerDesugaringKind::ForLoop) => labels.push((
pattern.span, pattern.span,
Expand Down Expand Up @@ -213,15 +203,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
span, span,
E0698, E0698,
"type inside generator must be known in this context"); "type inside generator must be known in this context");
err.span_label(span, InferCtxt::missing_type_msg(&name)); err.span_label(span, InferCtxt::missing_type_msg(&name, ""));
err err
} }


fn missing_type_msg(type_name: &str) -> String { fn missing_type_msg(type_name: &str, postfix: &str) -> String {
if type_name == "_" { if type_name == "_" {
"cannot infer type".to_owned() "cannot infer type".to_owned()
} else { } else {
format!("cannot infer type for `{}`", type_name) format!("cannot infer type for `{}`{}", type_name, postfix)
} }
} }
} }
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-12187-1.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0282]: type annotations needed for `&_` error[E0282]: type annotations needed in `&_`
--> $DIR/issue-12187-1.rs:6:10 --> $DIR/issue-12187-1.rs:6:10
| |
LL | let &v = new(); LL | let &v = new();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-12187-2.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0282]: type annotations needed for `&_` error[E0282]: type annotations needed in `&_`
--> $DIR/issue-12187-2.rs:6:10 --> $DIR/issue-12187-2.rs:6:10
| |
LL | let &v = new(); LL | let &v = new();
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-17551.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `B<_>` error[E0282]: type annotations needed in `B<_>`
--> $DIR/issue-17551.rs:6:15 --> $DIR/issue-17551.rs:6:15
| |
LL | let foo = B(marker::PhantomData); LL | let foo = B(marker::PhantomData);
| --- ^ cannot infer type for `T` | --- ^ cannot infer type for `T` in `B<_>`
| | | |
| consider giving `foo` the type `B<_>` with the type parameter `T` specified | consider giving `foo` a type


error: aborting due to previous error error: aborting due to previous error


Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-20261.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0282]: type annotations needed for `&(_,)` error[E0282]: type annotations needed in `&(_,)`
--> $DIR/issue-20261.rs:4:11 --> $DIR/issue-20261.rs:4:11
| |
LL | for (ref i,) in [].iter() { LL | for (ref i,) in [].iter() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-23046.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0282]: type annotations needed for `Expr<'_, _>` error[E0282]: type annotations needed in `Expr<'_, _>`
--> $DIR/issue-23046.rs:17:15 --> $DIR/issue-23046.rs:17:15
| |
LL | let ex = |x| { LL | let ex = |x| {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-25368.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0282]: type annotations needed for `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)` error[E0282]: type annotations needed in `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)`
--> $DIR/issue-25368.rs:11:17 --> $DIR/issue-25368.rs:11:17
| |
LL | let (tx, rx) = channel(); LL | let (tx, rx) = channel();
| -------- consider giving the pattern a type | -------- consider giving the pattern a type
... ...
LL | tx.send(Foo{ foo: PhantomData }); LL | tx.send(Foo{ foo: PhantomData });
| ^^^ cannot infer type for `T` | ^^^ cannot infer type for `T` in `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)`


error: aborting due to previous error error: aborting due to previous error


Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-7813.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `&[_; 0]` error[E0282]: type annotations needed in `&[_; 0]`
--> $DIR/issue-7813.rs:2:13 --> $DIR/issue-7813.rs:2:13
| |
LL | let v = &[]; LL | let v = &[];
| - ^^^ cannot infer type | - ^^^ cannot infer type
| | | |
| consider giving `v` the type `&[_; 0]` with the type parameter `_` specified | consider giving `v` a type


error: aborting due to previous error error: aborting due to previous error


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `std::vec::Vec<_>` error[E0282]: type annotations needed in `std::vec::Vec<_>`
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:24:17 --> $DIR/method-ambig-one-trait-unknown-int-type.rs:24:17
| |
LL | let mut x = Vec::new(); LL | let mut x = Vec::new();
| ----- ^^^^^^^^ cannot infer type for `T` | ----- ^^^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
| | | |
| consider giving `x` the type `std::vec::Vec<_>` with the type parameter `T` specified | consider giving `x` a type


error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:33:20 --> $DIR/method-ambig-one-trait-unknown-int-type.rs:33:20
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/span/issue-42234-unknown-receiver-type.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `std::option::Option<_>` error[E0282]: type annotations needed in `std::option::Option<_>`
--> $DIR/issue-42234-unknown-receiver-type.rs:7:5 --> $DIR/issue-42234-unknown-receiver-type.rs:7:5
| |
LL | let x: Option<_> = None; LL | let x: Option<_> = None;
| - consider giving `x` the type `std::option::Option<_>` with the type parameter `T` specified | - consider giving `x` a type
LL | x.unwrap().method_that_could_exist_on_some_type(); LL | x.unwrap().method_that_could_exist_on_some_type();
| ^^^^^^^^^^ cannot infer type for `T` | ^^^^^^^^^^ cannot infer type for `T` in `std::option::Option<_>`
| |
= note: type must be known at this point = note: type must be known at this point


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `[_; 0]` error[E0282]: type annotations needed in `[_; 0]`
--> $DIR/cannot_infer_local_or_array.rs:2:13 --> $DIR/cannot_infer_local_or_array.rs:2:13
| |
LL | let x = []; LL | let x = [];
| - ^^ cannot infer type | - ^^ cannot infer type
| | | |
| consider giving `x` the type `[_; 0]` with the type parameter `_` specified | consider giving `x` a type


error: aborting due to previous error error: aborting due to previous error


Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/type/type-check/cannot_infer_local_or_vec.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `std::vec::Vec<_>` error[E0282]: type annotations needed in `std::vec::Vec<_>`
--> $DIR/cannot_infer_local_or_vec.rs:2:13 --> $DIR/cannot_infer_local_or_vec.rs:2:13
| |
LL | let x = vec![]; LL | let x = vec![];
| - ^^^^^^ cannot infer type for `T` | - ^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
| | | |
| consider giving `x` the type `std::vec::Vec<_>` with the type parameter `T` specified | consider giving `x` a type
| |
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0282]: type annotations needed for `(std::vec::Vec<_>,)` error[E0282]: type annotations needed in `(std::vec::Vec<_>,)`
--> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:18 --> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:18
| |
LL | let (x, ) = (vec![], ); LL | let (x, ) = (vec![], );
| ----- ^^^^^^ cannot infer type for `T` | ----- ^^^^^^ cannot infer type for `T` in `(std::vec::Vec<_>,)`
| | | |
| consider giving the pattern a type | consider giving the pattern a type
| |
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0282]: type annotations needed for `std::option::Option<_>` error[E0282]: type annotations needed in `std::option::Option<_>`
--> $DIR/unboxed-closures-failed-recursive-fn-2.rs:16:32 --> $DIR/unboxed-closures-failed-recursive-fn-2.rs:16:32
| |
LL | let mut closure0 = None; LL | let mut closure0 = None;
| ------------ consider giving `closure0` the type `std::option::Option<_>` with the type parameter `_` specified | ------------ consider giving `closure0` a type
... ...
LL | return c(); LL | return c();
| ^^^ cannot infer type | ^^^ cannot infer type
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/vector-no-ann.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0282]: type annotations needed for `std::vec::Vec<_>` error[E0282]: type annotations needed in `std::vec::Vec<_>`
--> $DIR/vector-no-ann.rs:2:16 --> $DIR/vector-no-ann.rs:2:16
| |
LL | let _foo = Vec::new(); LL | let _foo = Vec::new();
| ---- ^^^^^^^^ cannot infer type for `T` | ---- ^^^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
| | | |
| consider giving `_foo` the type `std::vec::Vec<_>` with the type parameter `T` specified | consider giving `_foo` a type


error: aborting due to previous error error: aborting due to previous error


Expand Down

0 comments on commit 65c2a7b

Please sign in to comment.