Skip to content

Commit 7160ae5

Browse files
committed
add check for uninhabited types along side never
1 parent 29e035e commit 7160ae5

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,7 +839,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
839839
self.infcx.typing_env(self.param_env),
840840
);
841841

842-
if !ty_is_inhabited {
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+
861+
if !ty_is_inhabited && output_is_inhabited {
843862
// Unreachable code warnings are already emitted during type checking.
844863
// However, during type checking, full type information is being
845864
// calculated but not yet available, so the check for diverging
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)