Skip to content

Commit d858d28

Browse files
authored
Rollup merge of #97302 - compiler-errors:writeback-ascending, r=cjgillot
Do writeback of Closure params before visiting the parent expression This means that given the expression: ``` let x = |a: Vec<_>| {}; ``` We will visit the HIR node for `a` before `x`, and report the ambiguity on the former instead of the latter. This also moves writeback for struct field ids and const blocks before, but the ordering of this and walking the expr doesn't seem to matter.
2 parents fe9c64d + e9215bb commit d858d28

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

compiler/rustc_typeck/src/check/writeback.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,6 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
263263
self.fix_scalar_builtin_expr(e);
264264
self.fix_index_builtin_expr(e);
265265

266-
self.visit_node_id(e.span, e.hir_id);
267-
268266
match e.kind {
269267
hir::ExprKind::Closure(_, _, body, _, _) => {
270268
let body = self.fcx.tcx.hir().body(body);
@@ -291,6 +289,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
291289
_ => {}
292290
}
293291

292+
self.visit_node_id(e.span, e.hir_id);
294293
intravisit::walk_expr(self, e);
295294
}
296295

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
fn main() {
2-
let x = |_| { }; //~ ERROR type annotations needed
1+
fn infer_in_arg() {
2+
let x = |b: Vec<_>| {}; //~ ERROR E0282
33
}
4+
5+
fn empty_pattern() {
6+
let x = |_| {}; //~ ERROR type annotations needed
7+
}
8+
9+
fn infer_ty() {
10+
let x = |k: _| {}; //~ ERROR type annotations needed
11+
}
12+
13+
fn ambig_return() {
14+
let x = || -> Vec<_> { Vec::new() }; //~ ERROR type annotations needed for the closure `fn() -> Vec<_>`
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
error[E0282]: type annotations needed
1+
error[E0282]: type annotations needed for `Vec<_>`
22
--> $DIR/unknown_type_for_closure.rs:2:14
33
|
4-
LL | let x = |_| { };
4+
LL | let x = |b: Vec<_>| {};
55
| ^ consider giving this closure parameter a type
66

7-
error: aborting due to previous error
7+
error[E0282]: type annotations needed
8+
--> $DIR/unknown_type_for_closure.rs:6:14
9+
|
10+
LL | let x = |_| {};
11+
| ^ consider giving this closure parameter a type
12+
13+
error[E0282]: type annotations needed
14+
--> $DIR/unknown_type_for_closure.rs:10:14
15+
|
16+
LL | let x = |k: _| {};
17+
| ^ consider giving this closure parameter a type
18+
19+
error[E0282]: type annotations needed for the closure `fn() -> Vec<_>`
20+
--> $DIR/unknown_type_for_closure.rs:14:28
21+
|
22+
LL | let x = || -> Vec<_> { Vec::new() };
23+
| ^^^^^^^^ cannot infer type for type parameter `T`
24+
|
25+
help: give this closure an explicit return type without `_` placeholders
26+
|
27+
LL | let x = || -> Vec<_> { Vec::new() };
28+
| ~~~~~~
29+
30+
error: aborting due to 4 previous errors
831

932
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)