rust/compiler
bors 5ca596f486 Auto merge of #85556 - FabianWolff:issue-85071, r=estebank,jackh726
Warn about unreachable code following an expression with an uninhabited type

This pull request fixes #85071. The issue is that liveness analysis currently is "smarter" than reachability analysis when it comes to detecting uninhabited types: Unreachable code is detected during type checking, where full type information is not yet available. Therefore, the check for type inhabitedness is quite crude:
fc81ad22c4/compiler/rustc_typeck/src/check/expr.rs (L202-L205)

i.e. it only checks for `!`, but not other, non-trivially uninhabited types, such as empty enums, structs containing an uninhabited type, etc. By contrast, liveness analysis, which runs after type checking, can benefit from the more sophisticated `tcx.is_ty_uninhabited_from()`:
fc81ad22c4/compiler/rustc_passes/src/liveness.rs (L981)
fc81ad22c4/compiler/rustc_passes/src/liveness.rs (L996)

This can lead to confusing warnings when a variable is reported as unused, but the use of the variable is not reported as unreachable. For instance:
```rust
enum Foo {}
fn f() -> Foo {todo!()}

fn main() {
    let x = f();
    let _ = x;
}
```
currently leads to
```
warning: unused variable: `x`
 --> t1.rs:5:9
  |
5 |     let x = f();
  |         ^ help: if this is intentional, prefix it with an underscore: `_x`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted
```
which is confusing, because `x` _appears_ to be used in line 6. With my changes, I get:
```
warning: unreachable expression
 --> t1.rs:6:13
  |
5 |     let x = f();
  |             --- any code following this expression is unreachable
6 |     let _ = x;
  |             ^ unreachable expression
  |
  = note: `#[warn(unreachable_code)]` on by default
note: this expression has type `Foo`, which is uninhabited
 --> t1.rs:5:13
  |
5 |     let x = f();
  |             ^^^

warning: unused variable: `x`
 --> t1.rs:5:9
  |
5 |     let x = f();
  |         ^ help: if this is intentional, prefix it with an underscore: `_x`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: 2 warnings emitted
```
My implementation is slightly inelegant because unreachable code warnings can now be issued in two different places (during type checking and during liveness analysis), but I think it is the solution with the least amount of unnecessary code duplication, given that the new warning integrates nicely with liveness analysis, where unreachable code is already implicitly detected for the purpose of finding unused variables.
2021-08-24 01:36:09 +00:00
..
rustc rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_apfloat rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_arena rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_ast Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_ast_lowering Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_ast_passes Auto merge of #86860 - fee1-dead:stabilize, r=LeSeulArtichaut 2021-08-18 01:00:17 +00:00
rustc_ast_pretty Introduce hir::ExprKind::Let - Take 2 2021-08-15 16:18:26 -03:00
rustc_attr rustc: Fill out remaining parts of C-unwind ABI 2021-08-03 07:06:19 -07:00
rustc_builtin_macros Auto merge of #83302 - camsteffen:write-piece-unchecked, r=dtolnay 2021-08-23 22:55:19 +00:00
rustc_codegen_cranelift Auto merge of #88135 - crlf0710:trait_upcasting_part_3, r=nikomatsakis 2021-08-21 21:14:07 +00:00
rustc_codegen_llvm Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_codegen_ssa Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_data_structures Fix more “a”/“an” typos 2021-08-22 16:35:29 +02:00
rustc_driver Reduce verbosity of RUSTC_LOG 2021-08-09 21:39:39 -04:00
rustc_error_codes Auto merge of #88134 - rylev:force-warn-improvements, r=nikomatsakis 2021-08-21 15:51:50 +00:00
rustc_errors review comments 2021-08-23 14:31:48 +00:00
rustc_expand Introduce hir::ExprKind::Let - Take 2 2021-08-15 16:18:26 -03:00
rustc_feature Auto merge of #87570 - nikic:llvm-13, r=nagisa 2021-08-21 09:25:28 +00:00
rustc_fs_util rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_graphviz rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_hir Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
rustc_hir_pretty Introduce hir::ExprKind::Let - Take 2 2021-08-15 16:18:26 -03:00
rustc_incremental rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_index rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_infer Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_interface Replace #[plugin_registrar] with exporting __rustc_plugin_registrar 2021-08-10 14:20:48 +02:00
rustc_lexer Fix more “a”/“an” typos 2021-08-22 17:27:18 +02:00
rustc_lint Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_lint_defs Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_llvm Rollup merge of #88164 - durin42:llvm-14-san-opts, r=nikic 2021-08-22 20:52:53 +02:00
rustc_macros Rollup merge of #86123 - Aaron1011:query-span, r=cjgillot 2021-08-19 19:30:04 +02:00
rustc_metadata Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
rustc_middle Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_mir Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
rustc_mir_build Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
rustc_parse Fix more “a”/“an” typos 2021-08-22 17:27:18 +02:00
rustc_parse_format rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_passes Auto merge of #85556 - FabianWolff:issue-85071, r=estebank,jackh726 2021-08-24 01:36:09 +00:00
rustc_plugin_impl Replace #[plugin_registrar] with exporting __rustc_plugin_registrar 2021-08-10 14:20:48 +02:00
rustc_privacy Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_query_impl Fold vtable_trait_upcasting_coercion_new_vptr_slot logic into obligation processing. 2021-08-18 13:00:27 +08:00
rustc_query_system Prevent double panic when handling incremental fingerprint mismatch 2021-08-12 15:11:39 -05:00
rustc_resolve Rollup merge of #88232 - m-ou-se:macro-name-imported-but-not-macro, r=estebank 2021-08-23 20:45:50 +02:00
rustc_save_analysis Include (potentially remapped) working dir in crate hash 2021-08-15 15:17:37 -05:00
rustc_serialize Fix more “a”/“an” typos 2021-08-22 16:35:29 +02:00
rustc_session Do not mark -Z thir-unsafeck as unsound anymore 2021-08-23 14:52:42 +02:00
rustc_span Fix typos “an”→“a” and a few different ones that appeared in the same search 2021-08-22 18:15:49 +02:00
rustc_symbol_mangling Auto merge of #85296 - bjorn3:plugin_cleanup, r=petrochenkov 2021-08-12 04:30:41 +00:00
rustc_target Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_trait_selection Rollup merge of #88230 - steffahn:a_an, r=oli-obk 2021-08-23 20:45:49 +02:00
rustc_traits Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_ty_utils Fix typos “a”→“an” 2021-08-22 15:35:11 +02:00
rustc_type_ir rfc3052: Remove authors field from Cargo manifests 2021-07-29 14:56:05 -07:00
rustc_typeck Auto merge of #88265 - m-ou-se:rollup-soymv20, r=m-ou-se 2021-08-23 20:10:29 +00:00