Skip to content

Commit 2fe9b51

Browse files
authored
Unrolled build for #149664
Rollup merge of #149664 - Kivooeo:infallible-type-unreachable-code, r=davidtwco attempt to fix unreachable code regression For some reason it works, it checks function output type and suppress warning if type is uninhabited ~~This double negations in code breaks my mind actually~~ I'd love to revisit this part in future and try to find a proper solution maybe, but for now I feel like it's enough before release to fix the issue? I really wonder what team does think, especially `@cjgillot` and other people who are more confident in this part of compiler than I do I tried a lot of things here, it's only approach that pass all tests included new regression one fixes #149571 r? compiler
2 parents c61a3a4 + 2a2da78 commit 2fe9b51

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
839839
self.infcx.typing_env(self.param_env),
840840
);
841841

842+
// check if the function's return type is inhabited
843+
// this was added here because of this regression
844+
// https://github.com/rust-lang/rust/issues/149571
845+
let output_is_inhabited =
846+
if matches!(self.tcx.def_kind(self.def_id), DefKind::Fn | DefKind::AssocFn) {
847+
self.tcx
848+
.fn_sig(self.def_id)
849+
.instantiate_identity()
850+
.skip_binder()
851+
.output()
852+
.is_inhabited_from(
853+
self.tcx,
854+
self.parent_module,
855+
self.infcx.typing_env(self.param_env),
856+
)
857+
} else {
858+
true
859+
};
860+
842861
if !ty_is_inhabited {
843862
// Unreachable code warnings are already emitted during type checking.
844863
// However, during type checking, full type information is being
@@ -849,7 +868,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
849868
// uninhabited types (e.g. empty enums). The check above is used so
850869
// that we do not emit the same warning twice if the uninhabited type
851870
// is indeed `!`.
852-
if !ty.is_never() {
871+
if !ty.is_never() && output_is_inhabited {
853872
lints.push((target_bb, ty, term.source_info.span));
854873
}
855874

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![deny(unreachable_code)]
2+
//@ run-pass
3+
4+
use std::convert::Infallible;
5+
6+
pub fn foo(f: impl FnOnce() -> Infallible) -> Infallible {
7+
f()
8+
}
9+
10+
fn main() {}

0 commit comments

Comments
 (0)