rust/compiler
bors 35fff69d04 Auto merge of #85343 - Aaron1011:variance-diag, r=estebank
Add variance-related information to lifetime error messages

This PR adds a basic framework for displaying variance-related information in error messages. For example:

```
error: lifetime may not live long enough
  --> $DIR/type-check-pointer-comparisons.rs:12:5
   |
LL | fn compare_mut<'a, 'b>(x: *mut &'a i32, y: *mut &'b i32) {
   |                --  -- lifetime `'b` defined here
   |                |
   |                lifetime `'a` defined here
LL |     x == y;
   |     ^ requires that `'a` must outlive `'b`
   |
   = help: consider adding the following bound: `'a: 'b`
   = note: requirement occurs because of a mutable pointer to &i32
   = note: mutable pointers are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
```

The last three lines are new.

This is accomplished by adding a new struct `VarianceDiagInfo`, and passing it along through the various relation methods. When relating types that change the variance (e.g. `&mut T` or `*mut T`), we pass a more specific `VarianceDiagInfo` storing information about the cause of the variance change. When an error, we use the `VarianceDiagInfo` to add additional information to the error message.

This PR doesn't change any variance-related computation or behavior - only diagnostic messages. Therefore, the implementation is quite incomplete - more detailed error messages can be filled in in subsequent PRs.

Limitations:
* We only attempt to deal with invariance - since it's at the bottom of the 'variance lattice', our variance will never change again after it becomes invariant. Handling contravariance would be trickier, since we can change between contravariance and covariance multiple times (e.g. `fn(fn(&'static u8))`). Since contravariance (AFAIK) is only used for function arguments, we can probably get away without a very fancy message for cases involving contravariance.
* `VarianceDiagInfo` currently only handles mutable pointers/references. However, user-defined types (structs, enums, and unions) have the variance of their type parameters inferred, so it would be good to eventually display information about that. We'll want to try to find a balance between displaying too much and too little information about how the variance was inferred.
* The improved error messages are only displayed when `#![feature(nll)]` / `-Z borrowck=mir` is enabled.  If issue https://github.com/rust-lang/rust/issues/58781 is not resolved relatively soon, then we might want to duplicate some of this logic in the 'current' (non-NLL) region/outlives handling code.
2021-06-06 19:12:05 +00:00
..
rustc use jemallocator in rustc/rustdoc 2021-04-04 14:10:26 -07:00
rustc_apfloat remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_arena Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_ast Auto merge of #84995 - petrochenkov:tcollect, r=Aaron1011 2021-06-06 14:00:43 +00:00
rustc_ast_lowering Rename take_trait_map. 2021-06-01 20:53:04 +02:00
rustc_ast_passes Remove doc(include) 2021-06-04 08:05:54 -04:00
rustc_ast_pretty remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_attr remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_builtin_macros Auto merge of #84863 - ABouttefeux:libtest, r=m-ou-se 2021-06-06 09:13:59 +00:00
rustc_codegen_cranelift Auto merge of #84171 - ricobbe:raw-dylib-via-llvm, r=petrochenkov 2021-06-06 03:59:17 +00:00
rustc_codegen_llvm Auto merge of #84171 - ricobbe:raw-dylib-via-llvm, r=petrochenkov 2021-06-06 03:59:17 +00:00
rustc_codegen_ssa linker: Re-apply Solaris fixes for -z ignore 2021-06-06 17:48:18 +03:00
rustc_data_structures Rollup merge of #85436 - tamird:save-clone, r=estebank 2021-06-06 19:11:16 +09:00
rustc_driver Auto merge of #83842 - LeSeulArtichaut:thir-vec, r=nikomatsakis 2021-05-19 18:41:23 +00:00
rustc_error_codes swap function order for better read flow 2021-05-13 13:22:24 +02:00
rustc_errors Miscellaneous inlining improvements 2021-06-02 08:49:58 +02:00
rustc_expand Remove doc(include) 2021-06-04 08:05:54 -04:00
rustc_feature Auto merge of #79608 - alessandrod:bpf, r=nagisa 2021-06-06 01:02:32 +00:00
rustc_fs_util
rustc_graphviz Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_hir Rollup merge of #85850 - bjorn3:less_feature_gates, r=jyn514 2021-06-04 13:42:54 +09:00
rustc_hir_pretty remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_incremental Auto merge of #85337 - cjgillot:less-anon, r=michaelwoerister 2021-06-02 13:11:41 +00:00
rustc_index Remove unused feature gates 2021-05-31 13:55:43 +02:00
rustc_infer Add variance-related information to lifetime error messages 2021-06-06 12:37:42 -05:00
rustc_interface Auto merge of #84449 - alexcrichton:metadata-in-object, r=nagisa 2021-06-04 19:29:50 +00:00
rustc_lexer Fix outdated crate names in compiler docs 2021-04-08 11:12:14 -05:00
rustc_lint Auto merge of #85457 - jyn514:remove-doc-include, r=GuillaumeGomez 2021-06-05 03:36:26 +00:00
rustc_lint_defs Fix deny(invalid_doc_attributes) 2021-06-04 15:20:26 -04:00
rustc_llvm Auto merge of #84171 - ricobbe:raw-dylib-via-llvm, r=petrochenkov 2021-06-06 03:59:17 +00:00
rustc_macros Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_metadata Auto merge of #84171 - ricobbe:raw-dylib-via-llvm, r=petrochenkov 2021-06-06 03:59:17 +00:00
rustc_middle Add variance-related information to lifetime error messages 2021-06-06 12:37:42 -05:00
rustc_mir Add variance-related information to lifetime error messages 2021-06-06 12:37:42 -05:00
rustc_mir_build Rollup merge of #85850 - bjorn3:less_feature_gates, r=jyn514 2021-06-04 13:42:54 +09:00
rustc_parse parser: Ensure that all nonterminals have tokens after parsing 2021-06-06 14:21:12 +03:00
rustc_parse_format remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_passes Rollup merge of #86047 - jyn514:doc-attrs, r=petrochenkov 2021-06-06 19:11:24 +09:00
rustc_plugin_impl Use () for plugin_registrar_fn. 2021-05-12 13:58:43 +02:00
rustc_privacy Revert "Reduce the amount of untracked state in TyCtxt" 2021-06-01 09:05:22 +02:00
rustc_query_impl Rollup merge of #85850 - bjorn3:less_feature_gates, r=jyn514 2021-06-04 13:42:54 +09:00
rustc_query_system Rollup merge of #85850 - bjorn3:less_feature_gates, r=jyn514 2021-06-04 13:42:54 +09:00
rustc_resolve Rollup merge of #85896 - BoxyUwU:remove-fixme-fwd-declared-const-default, r=petrochenkov 2021-06-03 14:35:36 +09:00
rustc_save_analysis Remove doc(include) 2021-06-04 08:05:54 -04:00
rustc_serialize Remove unused feature gates 2021-05-31 13:55:43 +02:00
rustc_session Auto merge of #84234 - jyn514:blanket-hash, r=Aaron1011 2021-06-05 01:08:51 +00:00
rustc_span Auto merge of #79608 - alessandrod:bpf, r=nagisa 2021-06-06 01:02:32 +00:00
rustc_symbol_mangling Restrict access to crate_name. 2021-06-02 18:35:32 +02:00
rustc_target Rollup merge of #85920 - luqmana:wasm-linker-tweaks, r=petrochenkov 2021-06-06 19:11:18 +09:00
rustc_trait_selection don't suggest unsized indirection in where-clauses 2021-06-03 17:19:57 -05:00
rustc_traits Auto merge of #83207 - oli-obk:valtree2, r=lcnr 2021-04-02 10:28:12 +00:00
rustc_ty_utils Restrict access to crate_name. 2021-06-02 18:35:32 +02:00
rustc_type_ir Remove unused feature gates 2021-05-31 13:55:43 +02:00
rustc_typeck Add variance-related information to lifetime error messages 2021-06-06 12:37:42 -05:00