diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-05-25 17:37:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-25 17:37:18 +0200 |
| commit | d858d280e4778e556e8b8595e20d36d229bda6e9 (patch) | |
| tree | d31325fed3a238e971c648845e4f4c5d86090643 | |
| parent | fe9c64d0af379e6e48ac1e798d65e2031265aa22 (diff) | |
| parent | e9215bb7a97d61b5368f58fa299474a8606655d4 (diff) | |
| download | rust-d858d280e4778e556e8b8595e20d36d229bda6e9.tar.gz rust-d858d280e4778e556e8b8595e20d36d229bda6e9.zip | |
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.
| -rw-r--r-- | compiler/rustc_typeck/src/check/writeback.rs | 3 | ||||
| -rw-r--r-- | src/test/ui/type/type-check/unknown_type_for_closure.rs | 18 | ||||
| -rw-r--r-- | src/test/ui/type/type-check/unknown_type_for_closure.stderr | 29 |
3 files changed, 43 insertions, 7 deletions
diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index 16096ea3d74..4fe5b26dc05 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -263,8 +263,6 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> { self.fix_scalar_builtin_expr(e); self.fix_index_builtin_expr(e); - self.visit_node_id(e.span, e.hir_id); - match e.kind { hir::ExprKind::Closure(_, _, body, _, _) => { let body = self.fcx.tcx.hir().body(body); @@ -291,6 +289,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> { _ => {} } + self.visit_node_id(e.span, e.hir_id); intravisit::walk_expr(self, e); } diff --git a/src/test/ui/type/type-check/unknown_type_for_closure.rs b/src/test/ui/type/type-check/unknown_type_for_closure.rs index 0dbf82453a2..0089d86e340 100644 --- a/src/test/ui/type/type-check/unknown_type_for_closure.rs +++ b/src/test/ui/type/type-check/unknown_type_for_closure.rs @@ -1,3 +1,17 @@ -fn main() { - let x = |_| { }; //~ ERROR type annotations needed +fn infer_in_arg() { + let x = |b: Vec<_>| {}; //~ ERROR E0282 } + +fn empty_pattern() { + let x = |_| {}; //~ ERROR type annotations needed +} + +fn infer_ty() { + let x = |k: _| {}; //~ ERROR type annotations needed +} + +fn ambig_return() { + let x = || -> Vec<_> { Vec::new() }; //~ ERROR type annotations needed for the closure `fn() -> Vec<_>` +} + +fn main() {} diff --git a/src/test/ui/type/type-check/unknown_type_for_closure.stderr b/src/test/ui/type/type-check/unknown_type_for_closure.stderr index 5971f56c97f..c3accad5f25 100644 --- a/src/test/ui/type/type-check/unknown_type_for_closure.stderr +++ b/src/test/ui/type/type-check/unknown_type_for_closure.stderr @@ -1,9 +1,32 @@ -error[E0282]: type annotations needed +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/unknown_type_for_closure.rs:2:14 | -LL | let x = |_| { }; +LL | let x = |b: Vec<_>| {}; | ^ consider giving this closure parameter a type -error: aborting due to previous error +error[E0282]: type annotations needed + --> $DIR/unknown_type_for_closure.rs:6:14 + | +LL | let x = |_| {}; + | ^ consider giving this closure parameter a type + +error[E0282]: type annotations needed + --> $DIR/unknown_type_for_closure.rs:10:14 + | +LL | let x = |k: _| {}; + | ^ consider giving this closure parameter a type + +error[E0282]: type annotations needed for the closure `fn() -> Vec<_>` + --> $DIR/unknown_type_for_closure.rs:14:28 + | +LL | let x = || -> Vec<_> { Vec::new() }; + | ^^^^^^^^ cannot infer type for type parameter `T` + | +help: give this closure an explicit return type without `_` placeholders + | +LL | let x = || -> Vec<_> { Vec::new() }; + | ~~~~~~ + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0282`. |
