Skip to content

Commit df996e8

Browse files
committed
Make need_type_info_err more conservative
1 parent 219380d commit df996e8

File tree

6 files changed

+178
-10
lines changed

6 files changed

+178
-10
lines changed

src/librustc_infer/infer/error_reporting/need_type_info.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
8888
if let (None, Some(ty)) =
8989
(self.found_local_pattern, self.node_ty_contains_target(local.hir_id))
9090
{
91+
// There's a trade-off here - we can either check that our target span
92+
// is contained in `local.span` or not. If we choose to check containment
93+
// we can avoid some spurious suggestions (see #72690), but we lose
94+
// the ability to report on things like:
95+
//
96+
// ```
97+
// let x = vec![];
98+
// ```
99+
//
100+
// because the target span will be in the macro expansion of `vec![]`.
101+
// At present we choose not to check containment.
91102
self.found_local_pattern = Some(&*local.pat);
92103
self.found_node_ty = Some(ty);
93104
}
@@ -99,8 +110,10 @@ impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
99110
if let (None, Some(ty)) =
100111
(self.found_arg_pattern, self.node_ty_contains_target(param.hir_id))
101112
{
102-
self.found_arg_pattern = Some(&*param.pat);
103-
self.found_node_ty = Some(ty);
113+
if self.target_span.contains(param.pat.span) {
114+
self.found_arg_pattern = Some(&*param.pat);
115+
self.found_node_ty = Some(ty);
116+
}
104117
}
105118
}
106119
intravisit::walk_body(self, body);

src/test/ui/closure-expected-type/expect-two-infer-vars-supply-ty-with-bound-region.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/expect-two-infer-vars-supply-ty-with-bound-region.rs:8:27
2+
--> $DIR/expect-two-infer-vars-supply-ty-with-bound-region.rs:8:5
33
|
44
LL | with_closure(|x: u32, y| {});
5-
| ^ consider giving this closure parameter a type
5+
| ^^^^^^^^^^^^ cannot infer type for type parameter `B` declared on the function `with_closure`
66

77
error: aborting due to previous error
88

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn let_<'var, VAR, F: for<'v> Fn(Expr<'v, VAR>) -> Expr<'v, VAR>>
1414
}
1515

1616
fn main() {
17-
let ex = |x| { //~ ERROR type annotations needed
18-
let_(add(x,x), |y| {
17+
let ex = |x| {
18+
let_(add(x,x), |y| { //~ ERROR type annotations needed
1919
let_(add(x, x), |x|x)})};
2020
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error[E0282]: type annotations needed for `Expr<'_, VAR>`
2-
--> $DIR/issue-23046.rs:17:15
1+
error[E0282]: type annotations needed for the closure `fn(Expr<'_, _>) -> Expr<'_, _>`
2+
--> $DIR/issue-23046.rs:18:9
33
|
4-
LL | let ex = |x| {
5-
| ^ consider giving this closure parameter the explicit type `Expr<'_, VAR>`, where the type parameter `VAR` is specified
4+
LL | let_(add(x,x), |y| {
5+
| ^^^^ cannot infer type for type parameter `VAR` declared on the function `let_`
6+
|
7+
help: give this closure an explicit return type without `_` placeholders
8+
|
9+
LL | let_(add(x, x), |x|-> Expr<'_, _> { x })})};
10+
| ^^^^^^^^^^^^^^^^ ^
611

712
error: aborting due to previous error
813

src/test/ui/issues/issue-72690.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
fn no_err() {
2+
|x: String| x;
3+
let _ = String::from("x");
4+
}
5+
6+
fn err() {
7+
String::from("x".as_ref()); //~ ERROR type annotations needed
8+
}
9+
10+
fn arg_pat_closure_err() {
11+
|x| String::from("x".as_ref()); //~ ERROR type annotations needed
12+
}
13+
14+
fn local_pat_closure_err() {
15+
let _ = "x".as_ref(); //~ ERROR type annotations needed
16+
}
17+
18+
fn err_first_arg_pat() {
19+
String::from("x".as_ref()); //~ ERROR type annotations needed
20+
|x: String| x;
21+
}
22+
23+
fn err_second_arg_pat() {
24+
|x: String| x;
25+
String::from("x".as_ref()); //~ ERROR type annotations needed
26+
}
27+
28+
fn err_mid_arg_pat() {
29+
|x: String| x;
30+
|x: String| x;
31+
|x: String| x;
32+
|x: String| x;
33+
String::from("x".as_ref()); //~ ERROR type annotations needed
34+
|x: String| x;
35+
|x: String| x;
36+
|x: String| x;
37+
|x: String| x;
38+
}
39+
40+
fn err_first_local_pat() {
41+
String::from("x".as_ref()); //~ ERROR type annotations needed
42+
let _ = String::from("x");
43+
}
44+
45+
fn err_second_local_pat() {
46+
let _ = String::from("x");
47+
String::from("x".as_ref()); //~ ERROR type annotations needed
48+
}
49+
50+
fn err_mid_local_pat() {
51+
let _ = String::from("x");
52+
let _ = String::from("x");
53+
let _ = String::from("x");
54+
let _ = String::from("x");
55+
String::from("x".as_ref()); //~ ERROR type annotations needed
56+
let _ = String::from("x");
57+
let _ = String::from("x");
58+
let _ = String::from("x");
59+
let _ = String::from("x");
60+
}
61+
62+
fn main() {}

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

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/issue-72690.rs:7:5
3+
|
4+
LL | String::from("x".as_ref());
5+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
6+
|
7+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
8+
= note: required by `std::convert::From::from`
9+
10+
error[E0282]: type annotations needed
11+
--> $DIR/issue-72690.rs:11:6
12+
|
13+
LL | |x| String::from("x".as_ref());
14+
| ^ consider giving this closure parameter a type
15+
16+
error[E0283]: type annotations needed
17+
--> $DIR/issue-72690.rs:15:17
18+
|
19+
LL | let _ = "x".as_ref();
20+
| ^^^^^^ cannot infer type for type `str`
21+
|
22+
= note: cannot satisfy `str: std::convert::AsRef<_>`
23+
24+
error[E0283]: type annotations needed
25+
--> $DIR/issue-72690.rs:19:5
26+
|
27+
LL | String::from("x".as_ref());
28+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
29+
|
30+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
31+
= note: required by `std::convert::From::from`
32+
33+
error[E0283]: type annotations needed
34+
--> $DIR/issue-72690.rs:25:5
35+
|
36+
LL | String::from("x".as_ref());
37+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
38+
|
39+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
40+
= note: required by `std::convert::From::from`
41+
42+
error[E0283]: type annotations needed
43+
--> $DIR/issue-72690.rs:33:5
44+
|
45+
LL | String::from("x".as_ref());
46+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
47+
|
48+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
49+
= note: required by `std::convert::From::from`
50+
51+
error[E0283]: type annotations needed for `std::string::String`
52+
--> $DIR/issue-72690.rs:41:5
53+
|
54+
LL | String::from("x".as_ref());
55+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
56+
LL | let _ = String::from("x");
57+
| - consider giving this pattern a type
58+
|
59+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
60+
= note: required by `std::convert::From::from`
61+
62+
error[E0283]: type annotations needed for `std::string::String`
63+
--> $DIR/issue-72690.rs:47:5
64+
|
65+
LL | let _ = String::from("x");
66+
| - consider giving this pattern a type
67+
LL | String::from("x".as_ref());
68+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
69+
|
70+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
71+
= note: required by `std::convert::From::from`
72+
73+
error[E0283]: type annotations needed for `std::string::String`
74+
--> $DIR/issue-72690.rs:55:5
75+
|
76+
LL | let _ = String::from("x");
77+
| - consider giving this pattern a type
78+
...
79+
LL | String::from("x".as_ref());
80+
| ^^^^^^^^^^^^ cannot infer type for struct `std::string::String`
81+
|
82+
= note: cannot satisfy `std::string::String: std::convert::From<&_>`
83+
= note: required by `std::convert::From::from`
84+
85+
error: aborting due to 9 previous errors
86+
87+
Some errors have detailed explanations: E0282, E0283.
88+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)