rust/compiler
bors 900cf5e890 Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726
Revise never type fallback algorithm

This is a rebase of https://github.com/rust-lang/rust/pull/84573, but dropping the stabilization of never type (and the accompanying large test diff).

Each commit builds & has tests updated alongside it, and could be reviewed in a more or less standalone fashion. But it may make more sense to review the PR as a whole, I'm not sure. It should be noted that tests being updated isn't really a good indicator of final behavior -- never_type_fallback is not enabled by default in this PR, so we can't really see the full effects of the commits here.

This combines the work by Niko, which is [documented in this gist](https://gist.github.com/nikomatsakis/7a07b265dc12f5c3b3bd0422018fa660), with some additional rules largely derived to target specific known patterns that regress with the algorithm solely derived by Niko. We build these from an intuition that:

* In general, fallback to `()` is *sound* in all cases
* But, in general, we *prefer* fallback to `!` as it accepts more code, particularly that written to intentionally use `!` (e.g., Result's with a Infallible/! variant).

When evaluating Niko's proposed algorithm, we find that there are certain cases where fallback to `!` leads to compilation failures in real-world code, and fallback to `()` fixes those errors. In order to allow for stabilization, we need to fix a good portion of these patterns.

The final rule set this PR proposes is that, by default, we fallback from `?T` to `!`, with the following exceptions:

1. `?T: Foo` and `Bar::Baz = ?T` and `(): Foo`, then fallback to `()`
2. Per [Niko's algorithm](https://gist.github.com/nikomatsakis/7a07b265dc12f5c3b3bd0422018fa660#proposal-fallback-chooses-between--and--based-on-the-coercion-graph), the "live" `?T` also fallback to `()`.

The first rule is necessary to address a fairly common pattern which boils down to something like the snippet below. Without rule 1, we do not see the closure's return type as needing a () fallback, which leads to compilation failure.

```rust
#![feature(never_type_fallback)]

trait Bar { }
impl Bar for () {  }
impl Bar for u32 {  }

fn foo<R: Bar>(_: impl Fn() -> R) {}

fn main() {
    foo(|| panic!());
}
```

r? `@jackh726`
2021-09-23 22:45:22 +00:00
..
rustc Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_apfloat Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_arena Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_ast Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_ast_lowering rustc_index: Add some map-like APIs to IndexVec 2021-09-22 03:11:29 +03:00
rustc_ast_passes Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_ast_pretty Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_attr Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_borrowck Check that TAIT generics are fully generic in mir typeck instead of wf-check, as wf-check can by definition only check TAIT in return position and not account for TAITs defined in the body of the function 2021-09-23 13:35:16 +00:00
rustc_builtin_macros Auto merge of #89139 - camsteffen:write-perf, r=Mark-Simulacrum 2021-09-23 02:10:26 +00:00
rustc_codegen_cranelift Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_codegen_llvm Rollup merge of #89041 - sticnarf:sticnarf/fat-lto-dwarf, r=nagisa 2021-09-22 19:03:21 +02:00
rustc_codegen_ssa Support #[track_caller] on closures and generators 2021-09-22 15:19:33 -05:00
rustc_const_eval Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_data_structures Auto merge of #89158 - the8472:rollup-3e4ijth, r=the8472 2021-09-21 22:07:32 +00:00
rustc_driver Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_error_codes Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_errors Rollup merge of #89046 - oli-obk:fix_oflo, r=estebank 2021-09-22 19:03:22 +02:00
rustc_expand Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_feature Support #[track_caller] on closures and generators 2021-09-22 15:19:33 -05:00
rustc_fs_util Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_graphviz Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_hir Auto merge of #89103 - Mark-Simulacrum:migrate-2021, r=estebank 2021-09-21 19:25:49 +00:00
rustc_hir_pretty Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_incremental Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_index rustc_index: Add some map-like APIs to IndexVec 2021-09-22 03:11:29 +03:00
rustc_infer Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726 2021-09-23 22:45:22 +00:00
rustc_interface Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_lexer Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_lint Auto merge of #88865 - guswynn:must_not_suspend, r=oli-obk 2021-09-22 06:43:33 +00:00
rustc_lint_defs Auto merge of #88865 - guswynn:must_not_suspend, r=oli-obk 2021-09-22 06:43:33 +00:00
rustc_llvm Rollup merge of #89041 - sticnarf:sticnarf/fat-lto-dwarf, r=nagisa 2021-09-22 19:03:21 +02:00
rustc_macros Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_metadata Auto merge of #89016 - lcnr:non_blanket_impls, r=nikomatsakis,michaelwoerister 2021-09-23 15:44:53 +00:00
rustc_middle Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726 2021-09-23 22:45:22 +00:00
rustc_mir_build Auto merge of #89158 - the8472:rollup-3e4ijth, r=the8472 2021-09-21 22:07:32 +00:00
rustc_mir_dataflow Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_mir_transform Support #[track_caller] on closures and generators 2021-09-22 15:19:33 -05:00
rustc_monomorphize Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_parse Rollup merge of #89046 - oli-obk:fix_oflo, r=estebank 2021-09-22 19:03:22 +02:00
rustc_parse_format Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_passes Auto merge of #88865 - guswynn:must_not_suspend, r=oli-obk 2021-09-22 06:43:33 +00:00
rustc_plugin_impl Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_privacy Auto merge of #89103 - Mark-Simulacrum:migrate-2021, r=estebank 2021-09-21 19:25:49 +00:00
rustc_query_impl Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_query_system Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_resolve Auto merge of #89158 - the8472:rollup-3e4ijth, r=the8472 2021-09-21 22:07:32 +00:00
rustc_save_analysis Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_serialize Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_session Rollup merge of #89133 - FabianWolff:issue-79546, r=michaelwoerister 2021-09-22 19:03:24 +02:00
rustc_span Auto merge of #87064 - Aaron1011:new-closure-track-caller, r=estebank 2021-09-23 12:26:51 +00:00
rustc_symbol_mangling Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_target Rollup merge of #89170 - rusticstuff:aarch64_macos_disable_leak_sanitizer, r=petrochenkov 2021-09-22 19:03:27 +02:00
rustc_trait_selection Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726 2021-09-23 22:45:22 +00:00
rustc_traits Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_ty_utils Auto merge of #89103 - Mark-Simulacrum:migrate-2021, r=estebank 2021-09-21 19:25:49 +00:00
rustc_type_ir Migrate to 2021 2021-09-20 22:21:42 -04:00
rustc_typeck Auto merge of #88804 - Mark-Simulacrum:never-algo-v2, r=nikomatsakis,jackh726 2021-09-23 22:45:22 +00:00