rust/compiler
Matthias Krüger 413f490677
Rollup merge of #92183 - tmandry:issue-74256, r=estebank
Point at correct argument when async fn output type lifetime disagrees with signature

Fixes most of #74256.

## Problems fixed

This PR fixes a couple of related problems in the error reporting code.

### Highlighting the wrong argument

First, the error reporting code was looking at the desugared return type of an `async fn` to decide which parameter to highlight. For example, a function like

```rust
async fn async_fn(self: &Struct, f: &u32) -> &u32
{ f }
```

desugars to

```rust
async fn async_fn<'a, 'b>(self: &'a Struct, f: &'b u32)
-> impl Future<Output = &'a u32> + 'a + 'b
{ f }
```

Since `f: &'b u32` is returned but the output type is `&'a u32`, the error would occur when checking that `'a: 'b`.

The reporting code would look to see if the "offending" lifetime `'b` was included in the return type, and because the code was looking at the desugared future type, it was included. So it defaulted to reporting that the source of the other lifetime `'a` (the `self` type) was the problem, when it was really the type of `f`. (Note that if it had chosen instead to look at `'a` first, it too would have been included in the output type, and it would have arbitrarily reported the error (correctly this time) on the type of `f`.)

Looking at the actual future type isn't useful for this reason; it captures all input lifetimes. Using the written return type for `async fn` solves this problem and results in less confusing error messages for the user.

This isn't a perfect fix, unfortunately; writing the "manually desugared" form of the above function still results in the wrong parameter being highlighted. Looking at the output type of every `impl Future` return type doesn't feel like a very principled approach, though it might work. The problem would remain for function signatures that look like the desugared one above but use different traits. There may be deeper changes required to pinpoint which part of each type is conflicting.

### Lying about await point capture causing lifetime conflicts

The second issue fixed by this PR is the unnecessary complexity in `try_report_anon_anon_conflict`. It turns out that the root cause I suggested in https://github.com/rust-lang/rust/issues/76547#issuecomment-692863608 wasn't really the root cause. Adding special handling to report that a variable was captured over an await point only made the error messages less correct and pointed to a problem other than the one that actually occurred.

Given the above discussion, it's easy to see why: `async fn`s capture all input lifetimes in their return type, so holding an argument across an await point should never cause a lifetime conflict! Removing the special handling simplified the code and improved the error messages (though they still aren't very good!)

## Future work

* Fix error reporting on the "desugared" form of this code
* Get the `suggest_adding_lifetime_params` suggestion firing on these examples
  * cc #42703, I think

r? `@estebank`
2022-01-20 17:10:34 +01:00
..
rustc
rustc_apfloat
rustc_arena
rustc_ast Rollup merge of #93018 - pierwill:rm-unused-ord, r=davidtwco 2022-01-18 22:00:50 +01:00
rustc_ast_lowering Formally implement let chains 2022-01-18 19:38:17 -03:00
rustc_ast_passes Formally implement let chains 2022-01-18 19:38:17 -03:00
rustc_ast_pretty Rollup merge of #93065 - dtolnay:ringbuffer, r=lcnr 2022-01-19 19:19:51 +01:00
rustc_attr
rustc_borrowck Use Term in ProjectionPredicate 2022-01-17 17:44:56 +00:00
rustc_builtin_macros Auto merge of #92816 - tmiasko:rm-llvm-asm, r=Amanieu 2022-01-17 09:40:29 +00:00
rustc_codegen_cranelift remove is_noop 2022-01-19 13:58:29 +01:00
rustc_codegen_gcc Use Symbol for target features in asm handling 2022-01-17 18:06:27 +01:00
rustc_codegen_llvm Rollup merge of #92425 - calebzulawski:simd-cast, r=workingjubilee 2022-01-18 22:00:45 +01:00
rustc_codegen_ssa Rollup merge of #91606 - joshtriplett:stabilize-print-link-args, r=pnkfelix 2022-01-20 17:10:32 +01:00
rustc_const_eval Auto merge of #92816 - tmiasko:rm-llvm-asm, r=Amanieu 2022-01-17 09:40:29 +00:00
rustc_data_structures Auto merge of #92740 - cuviper:update-rayons, r=Mark-Simulacrum 2022-01-16 08:12:23 +00:00
rustc_driver Rollup merge of #91606 - joshtriplett:stabilize-print-link-args, r=pnkfelix 2022-01-20 17:10:32 +01:00
rustc_error_codes Rollup merge of #92752 - jamestiotio:error-codes-typos, r=nagisa 2022-01-17 20:07:04 +01:00
rustc_errors Auto merge of #92070 - rukai:replace_vec_into_iter_with_array_into_iter, r=Mark-Simulacrum 2022-01-11 14:23:24 +00:00
rustc_expand Auto merge of #92473 - petrochenkov:ltrattr2, r=Aaron1011 2022-01-17 02:06:54 +00:00
rustc_feature Formally implement let chains 2022-01-18 19:38:17 -03:00
rustc_fs_util
rustc_graphviz eplace usages of vec![].into_iter with [].into_iter 2022-01-09 14:09:25 +11:00
rustc_hir NiceRegionError: Use written return type for async fn 2022-01-19 21:33:57 +00:00
rustc_hir_pretty Use Term in ProjectionPredicate 2022-01-17 17:44:56 +00:00
rustc_incremental Rollup merge of #92825 - pierwill:rustc-version-force-rename, r=Mark-Simulacrum 2022-01-17 20:07:06 +01:00
rustc_index remove unused FIXME 2022-01-12 16:09:01 +01:00
rustc_infer Simplify error reporting code, remove await point wording 2022-01-19 21:33:57 +00:00
rustc_interface Rollup merge of #91606 - joshtriplett:stabilize-print-link-args, r=pnkfelix 2022-01-20 17:10:32 +01:00
rustc_lexer
rustc_lint Replace NestedVisitorMap with NestedFilter 2022-01-16 16:02:36 -06:00
rustc_lint_defs Fix lints documents 2022-01-13 03:57:44 +00:00
rustc_llvm Remove LLVMRustMarkAllFunctionsNounwind 2022-01-14 00:36:12 +00:00
rustc_log
rustc_macros
rustc_metadata Rollup merge of #90782 - ricobbe:binutils-dlltool, r=michaelwoerister 2022-01-18 22:00:42 +01:00
rustc_middle Rollup merge of #93077 - lcnr:write_substs, r=oli-obk 2022-01-19 19:19:52 +01:00
rustc_mir_build Formally implement let chains 2022-01-18 19:38:17 -03:00
rustc_mir_dataflow Remove deprecated LLVM-style inline assembly 2022-01-12 18:51:31 +01:00
rustc_mir_transform Rollup merge of #89764 - tmiasko:uninhabited-enums, r=wesleywiser 2022-01-20 17:10:31 +01:00
rustc_monomorphize Auto merge of #92805 - BoxyUwU:revert-lazy-anon-const-substs, r=lcnr 2022-01-16 11:19:21 +00:00
rustc_parse Rollup merge of #91150 - dtolnay:qpath, r=davidtwco 2022-01-18 22:00:43 +01:00
rustc_parse_format
rustc_passes Rollup merge of #92783 - FabianWolff:issue-92726, r=nikomatsakis 2022-01-19 10:42:16 +01:00
rustc_plugin_impl
rustc_privacy Auto merge of #87648 - JulianKnodt:const_eq_constrain, r=oli-obk 2022-01-18 09:58:39 +00:00
rustc_query_impl attempt to re-add ty::Unevaluated visitor and friends 2022-01-15 01:16:55 +00:00
rustc_query_system Auto merge of #92740 - cuviper:update-rayons, r=Mark-Simulacrum 2022-01-16 08:12:23 +00:00
rustc_resolve Replace NestedVisitorMap with NestedFilter 2022-01-16 16:02:36 -06:00
rustc_save_analysis Replace NestedVisitorMap with NestedFilter 2022-01-16 16:02:36 -06:00
rustc_serialize Auto merge of #92604 - nnethercote:optimize-impl_read_unsigned_leb128, r=michaelwoerister 2022-01-15 07:27:30 +00:00
rustc_session Rollup merge of #91606 - joshtriplett:stabilize-print-link-args, r=pnkfelix 2022-01-20 17:10:32 +01:00
rustc_span Rollup merge of #92425 - calebzulawski:simd-cast, r=workingjubilee 2022-01-18 22:00:45 +01:00
rustc_symbol_mangling Rollup merge of #92316 - petrochenkov:extmangle, r=wesleywiser 2022-01-19 19:19:45 +01:00
rustc_target Pass target_features set instead of has_feature closure 2022-01-17 18:06:30 +01:00
rustc_trait_selection remove is_noop 2022-01-19 13:58:29 +01:00
rustc_traits Auto merge of #87648 - JulianKnodt:const_eq_constrain, r=oli-obk 2022-01-18 09:58:39 +00:00
rustc_ty_utils Auto merge of #92805 - BoxyUwU:revert-lazy-anon-const-substs, r=lcnr 2022-01-16 11:19:21 +00:00
rustc_type_ir initial revert 2022-01-15 01:16:55 +00:00
rustc_typeck Rollup merge of #92183 - tmandry:issue-74256, r=estebank 2022-01-20 17:10:34 +01:00