Skip to content

Commit 65c2a7b

Browse files
committed
Alternative wording for inference failure
1 parent 8bb094d commit 65c2a7b

15 files changed

+42
-52
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
1515
hir_map: &'a hir::map::Map<'gcx>,
1616
found_local_pattern: Option<&'gcx Pat>,
1717
found_arg_pattern: Option<&'gcx Pat>,
18-
found_ty: Option<Ty<'tcx>>,
18+
found_ty: Option<String>,
1919
}
2020

2121
impl<'a, 'gcx, 'tcx> FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
@@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
5555
fn visit_local(&mut self, local: &'gcx Local) {
5656
if let (None, Some(ty)) = (self.found_local_pattern, self.node_matches_type(local.hir_id)) {
5757
self.found_local_pattern = Some(&*local.pat);
58-
self.found_ty = Some(ty);
58+
self.found_ty = Some(ty.to_string());
5959
}
6060
intravisit::walk_local(self, local);
6161
}
@@ -67,7 +67,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
6767
self.node_matches_type(argument.hir_id),
6868
) {
6969
self.found_arg_pattern = Some(&*argument.pat);
70-
self.found_ty = Some(ty);
70+
self.found_ty = Some(ty.to_string());
7171
}
7272
}
7373
intravisit::walk_body(self, body);
@@ -108,7 +108,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
108108
let name = self.extract_type_name(&ty, None);
109109

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

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

127-
let ty_msg = match local_visitor.found_ty {
128-
Some(ty) if &ty.to_string() != "_" => format!(" for `{}`", ty),
126+
let ty_msg = match &local_visitor.found_ty {
127+
Some(ty) if &ty[..] != "_" && ty != &name => format!(" in `{}`", ty),
129128
_ => String::new(),
130129
};
130+
let mut labels = vec![(span, InferCtxt::missing_type_msg(&name, &ty_msg))];
131+
131132
if let Some(pattern) = local_visitor.found_arg_pattern {
132133
err_span = pattern.span;
133134
// We don't want to show the default label for closures.
@@ -148,8 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
148149
labels.clear();
149150
labels.push((pattern.span, format!(
150151
"consider giving this closure parameter {}",
151-
match local_visitor.found_ty {
152-
Some(ty) if &ty.to_string() != "_" => format!(
152+
match &local_visitor.found_ty {
153+
Some(ty) if &ty[..] != "_" && ty != &name => format!(
153154
"the type `{}` with the type parameter `{}` specified",
154155
ty,
155156
name,
@@ -162,18 +163,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
162163
match pattern.span.compiler_desugaring_kind() {
163164
None => labels.push((
164165
pattern.span,
165-
format!(
166-
"consider giving `{}` {}",
167-
simple_ident,
168-
match local_visitor.found_ty {
169-
Some(ty) if &ty.to_string() != "_" => format!(
170-
"the type `{}` with the type parameter `{}` specified",
171-
ty,
172-
name,
173-
),
174-
_ => "a type".to_owned(),
175-
},
176-
),
166+
format!("consider giving `{}` a type", simple_ident),
177167
)),
178168
Some(CompilerDesugaringKind::ForLoop) => labels.push((
179169
pattern.span,
@@ -213,15 +203,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
213203
span,
214204
E0698,
215205
"type inside generator must be known in this context");
216-
err.span_label(span, InferCtxt::missing_type_msg(&name));
206+
err.span_label(span, InferCtxt::missing_type_msg(&name, ""));
217207
err
218208
}
219209

220-
fn missing_type_msg(type_name: &str) -> String {
210+
fn missing_type_msg(type_name: &str, postfix: &str) -> String {
221211
if type_name == "_" {
222212
"cannot infer type".to_owned()
223213
} else {
224-
format!("cannot infer type for `{}`", type_name)
214+
format!("cannot infer type for `{}`{}", type_name, postfix)
225215
}
226216
}
227217
}

src/test/ui/issues/issue-12187-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `&_`
1+
error[E0282]: type annotations needed in `&_`
22
--> $DIR/issue-12187-1.rs:6:10
33
|
44
LL | let &v = new();

src/test/ui/issues/issue-12187-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `&_`
1+
error[E0282]: type annotations needed in `&_`
22
--> $DIR/issue-12187-2.rs:6:10
33
|
44
LL | let &v = new();

src/test/ui/issues/issue-17551.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `B<_>`
1+
error[E0282]: type annotations needed in `B<_>`
22
--> $DIR/issue-17551.rs:6:15
33
|
44
LL | let foo = B(marker::PhantomData);
5-
| --- ^ cannot infer type for `T`
5+
| --- ^ cannot infer type for `T` in `B<_>`
66
| |
7-
| consider giving `foo` the type `B<_>` with the type parameter `T` specified
7+
| consider giving `foo` a type
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-20261.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `&(_,)`
1+
error[E0282]: type annotations needed in `&(_,)`
22
--> $DIR/issue-20261.rs:4:11
33
|
44
LL | for (ref i,) in [].iter() {

src/test/ui/issues/issue-23046.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0282]: type annotations needed for `Expr<'_, _>`
1+
error[E0282]: type annotations needed in `Expr<'_, _>`
22
--> $DIR/issue-23046.rs:17:15
33
|
44
LL | let ex = |x| {

src/test/ui/issues/issue-25368.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0282]: type annotations needed for `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)`
1+
error[E0282]: type annotations needed in `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)`
22
--> $DIR/issue-25368.rs:11:17
33
|
44
LL | let (tx, rx) = channel();
55
| -------- consider giving the pattern a type
66
...
77
LL | tx.send(Foo{ foo: PhantomData });
8-
| ^^^ cannot infer type for `T`
8+
| ^^^ cannot infer type for `T` in `(std::sync::mpsc::Sender<Foo<_>>, std::sync::mpsc::Receiver<Foo<_>>)`
99

1010
error: aborting due to previous error
1111

src/test/ui/issues/issue-7813.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `&[_; 0]`
1+
error[E0282]: type annotations needed in `&[_; 0]`
22
--> $DIR/issue-7813.rs:2:13
33
|
44
LL | let v = &[];
55
| - ^^^ cannot infer type
66
| |
7-
| consider giving `v` the type `&[_; 0]` with the type parameter `_` specified
7+
| consider giving `v` a type
88

99
error: aborting due to previous error
1010

src/test/ui/methods/method-ambig-one-trait-unknown-int-type.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `std::vec::Vec<_>`
1+
error[E0282]: type annotations needed in `std::vec::Vec<_>`
22
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:24:17
33
|
44
LL | let mut x = Vec::new();
5-
| ----- ^^^^^^^^ cannot infer type for `T`
5+
| ----- ^^^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
66
| |
7-
| consider giving `x` the type `std::vec::Vec<_>` with the type parameter `T` specified
7+
| consider giving `x` a type
88

99
error[E0308]: mismatched types
1010
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:33:20

src/test/ui/span/issue-42234-unknown-receiver-type.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `std::option::Option<_>`
1+
error[E0282]: type annotations needed in `std::option::Option<_>`
22
--> $DIR/issue-42234-unknown-receiver-type.rs:7:5
33
|
44
LL | let x: Option<_> = None;
5-
| - consider giving `x` the type `std::option::Option<_>` with the type parameter `T` specified
5+
| - consider giving `x` a type
66
LL | x.unwrap().method_that_could_exist_on_some_type();
7-
| ^^^^^^^^^^ cannot infer type for `T`
7+
| ^^^^^^^^^^ cannot infer type for `T` in `std::option::Option<_>`
88
|
99
= note: type must be known at this point
1010

src/test/ui/type/type-check/cannot_infer_local_or_array.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `[_; 0]`
1+
error[E0282]: type annotations needed in `[_; 0]`
22
--> $DIR/cannot_infer_local_or_array.rs:2:13
33
|
44
LL | let x = [];
55
| - ^^ cannot infer type
66
| |
7-
| consider giving `x` the type `[_; 0]` with the type parameter `_` specified
7+
| consider giving `x` a type
88

99
error: aborting due to previous error
1010

src/test/ui/type/type-check/cannot_infer_local_or_vec.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `std::vec::Vec<_>`
1+
error[E0282]: type annotations needed in `std::vec::Vec<_>`
22
--> $DIR/cannot_infer_local_or_vec.rs:2:13
33
|
44
LL | let x = vec![];
5-
| - ^^^^^^ cannot infer type for `T`
5+
| - ^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
66
| |
7-
| consider giving `x` the type `std::vec::Vec<_>` with the type parameter `T` specified
7+
| consider giving `x` a type
88
|
99
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1010

src/test/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0282]: type annotations needed for `(std::vec::Vec<_>,)`
1+
error[E0282]: type annotations needed in `(std::vec::Vec<_>,)`
22
--> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:18
33
|
44
LL | let (x, ) = (vec![], );
5-
| ----- ^^^^^^ cannot infer type for `T`
5+
| ----- ^^^^^^ cannot infer type for `T` in `(std::vec::Vec<_>,)`
66
| |
77
| consider giving the pattern a type
88
|

src/test/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0282]: type annotations needed for `std::option::Option<_>`
1+
error[E0282]: type annotations needed in `std::option::Option<_>`
22
--> $DIR/unboxed-closures-failed-recursive-fn-2.rs:16:32
33
|
44
LL | let mut closure0 = None;
5-
| ------------ consider giving `closure0` the type `std::option::Option<_>` with the type parameter `_` specified
5+
| ------------ consider giving `closure0` a type
66
...
77
LL | return c();
88
| ^^^ cannot infer type

src/test/ui/vector-no-ann.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0282]: type annotations needed for `std::vec::Vec<_>`
1+
error[E0282]: type annotations needed in `std::vec::Vec<_>`
22
--> $DIR/vector-no-ann.rs:2:16
33
|
44
LL | let _foo = Vec::new();
5-
| ---- ^^^^^^^^ cannot infer type for `T`
5+
| ---- ^^^^^^^^ cannot infer type for `T` in `std::vec::Vec<_>`
66
| |
7-
| consider giving `_foo` the type `std::vec::Vec<_>` with the type parameter `T` specified
7+
| consider giving `_foo` a type
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)