Skip to content

Commit 9d2e3ff

Browse files
Rollup merge of #157009 - qaijuang:unreachable-return-tail, r=cjgillot
Avoid `unreachable_code` on required return values This PR skips return-place assignment plumbing when selecting user code to lint after an uninhabited call. Fixes #152559 r? cjgillot
2 parents c576c93 + 7451f1d commit 9d2e3ff

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
890890
{
891891
continue;
892892
}
893+
// Ignore return value plumbing. After a call returning a non-`!`
894+
// uninhabited type, a tail expression can be unreachable while
895+
// still being needed to satisfy the surrounding return type.
896+
StatementKind::Assign((place, _)) if place.as_local() == Some(RETURN_PLACE) => {
897+
continue;
898+
}
893899
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {
894900
continue;
895901
}

tests/ui/uninhabited/unreachable.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//@ check-pass
55
#![deny(unreachable_code)]
66

7+
use std::process::ExitCode;
8+
79
enum Never {}
810

911
fn make_never() -> Never {
@@ -28,6 +30,14 @@ fn branchy() {
2830
}
2931
}
3032

33+
// Regression test for https://github.com/rust-lang/rust/issues/152559.
34+
// The final expression is unreachable at runtime, but it cannot be removed
35+
// because it supplies the function's required return type.
36+
fn required_return_value() -> ExitCode {
37+
make_never();
38+
ExitCode::FAILURE
39+
}
40+
3141
fn main() {
3242
func();
3343
block();

0 commit comments

Comments
 (0)