From b63b83922c84dba84455199ccca19e2e844d37b1 Mon Sep 17 00:00:00 2001 From: qaijuang <237468078+qaijuang@users.noreply.github.com> Date: Wed, 27 May 2026 04:37:36 -0400 Subject: [PATCH 1/2] add red test --- tests/ui/uninhabited/unreachable.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/ui/uninhabited/unreachable.rs b/tests/ui/uninhabited/unreachable.rs index e4a5ba406a72e..10e77d27370ef 100644 --- a/tests/ui/uninhabited/unreachable.rs +++ b/tests/ui/uninhabited/unreachable.rs @@ -4,6 +4,8 @@ //@ check-pass #![deny(unreachable_code)] +use std::process::ExitCode; + enum Never {} fn make_never() -> Never { @@ -28,6 +30,14 @@ fn branchy() { } } +// Regression test for https://github.com/rust-lang/rust/issues/152559. +// The final expression is unreachable at runtime, but it cannot be removed +// because it supplies the function's required return type. +fn required_return_value() -> ExitCode { + make_never(); + ExitCode::FAILURE +} + fn main() { func(); block(); From 7451f1da2e6380e013264914f38375b9ee226836 Mon Sep 17 00:00:00 2001 From: qaijuang <237468078+qaijuang@users.noreply.github.com> Date: Wed, 27 May 2026 04:51:08 -0400 Subject: [PATCH 2/2] avoid `unreachable_code` on required return values --- compiler/rustc_mir_build/src/builder/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_mir_build/src/builder/mod.rs b/compiler/rustc_mir_build/src/builder/mod.rs index e9f9da9c8cd91..d158f55a3111c 100644 --- a/compiler/rustc_mir_build/src/builder/mod.rs +++ b/compiler/rustc_mir_build/src/builder/mod.rs @@ -890,6 +890,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { { continue; } + // Ignore return value plumbing. After a call returning a non-`!` + // uninhabited type, a tail expression can be unreachable while + // still being needed to satisfy the surrounding return type. + StatementKind::Assign((place, _)) if place.as_local() == Some(RETURN_PLACE) => { + continue; + } StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => { continue; }