Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions compiler/rustc_mir_build/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,20 +861,46 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

/// Starting at a target unreachable block, find some user code to lint as unreachable
fn find_unreachable_code_from(
bb: BasicBlock,
bbs: &IndexVec<BasicBlock, BasicBlockData<'_>>,
) -> Option<(SourceInfo, &'static str)> {
let bb = &bbs[bb];
for stmt in &bb.statements {
match &stmt.kind {
// Ignore the implicit `()` return place assignment for unit functions/blocks
StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(const_))))
if const_.ty().is_unit() =>
{
continue;
}
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {
continue;
}
StatementKind::FakeRead(..) => return Some((stmt.source_info, "definition")),
_ => return Some((stmt.source_info, "expression")),
}
}

let term = bb.terminator();
match term.kind {
// No user code in this bb, and our goto target may be reachable via other paths
TerminatorKind::Goto { .. } | TerminatorKind::Return => None,
_ => Some((term.source_info, "expression")),
}
}

for (target_bb, orig_ty, orig_span) in lints {
if orig_span.in_external_macro(self.tcx.sess.source_map()) {
continue;
}
let target_bb = &self.cfg.basic_blocks[target_bb];
let (target_loc, descr) = target_bb
.statements
.iter()
.find_map(|stmt| match stmt.kind {
StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => None,
StatementKind::FakeRead(..) => Some((stmt.source_info, "definition")),
_ => Some((stmt.source_info, "expression")),
})
.unwrap_or_else(|| (target_bb.terminator().source_info, "expression"));

let Some((target_loc, descr)) =
find_unreachable_code_from(target_bb, &self.cfg.basic_blocks)
else {
continue;
};
let lint_root = self.source_scopes[target_loc.scope]
.local_data
.as_ref()
Expand Down
26 changes: 26 additions & 0 deletions tests/ui/uninhabited/unreachable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Test that a diverging function as the final expression in a block does not
//! raise an 'unreachable code' lint.

//@ check-pass
#![deny(unreachable_code)]

enum Never {}

fn make_never() -> Never {
loop {}
}

fn func() {
make_never();
}

fn block() {
{
make_never();
}
}

fn main() {
func();
block();
}
6 changes: 4 additions & 2 deletions tests/ui/uninhabited/void-branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ enum Void {}
fn with_void() {
if false {
unsafe {
//~^ ERROR unreachable expression
std::mem::uninitialized::<Void>();
println!();
//~^ ERROR unreachable expression
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the error point on the unchanged test? Do you mind adding another if false block with the println an keep the original one?

Copy link
Contributor Author

@clubby789 clubby789 Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No error is raised within the block without the additional println (similarly to fn block in the other test I added).

Do you mind adding another if false block with the println an keep the original one?

Sorry, can you add an example? I'm not quite sure what this should look like

}

Expand All @@ -20,8 +21,9 @@ fn infallible() -> std::convert::Infallible {

fn with_infallible() {
if false {
//~^ ERROR unreachable expression
infallible();
println!()
//~^ ERROR unreachable expression
}

println!()
Expand Down
29 changes: 13 additions & 16 deletions tests/ui/uninhabited/void-branch.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
error: unreachable expression
--> $DIR/void-branch.rs:8:9
--> $DIR/void-branch.rs:10:13
|
LL | / unsafe {
LL | |
LL | | std::mem::uninitialized::<Void>();
| | --------------------------------- any code following this expression is unreachable
LL | | }
| |_________^ unreachable expression
LL | std::mem::uninitialized::<Void>();
| --------------------------------- any code following this expression is unreachable
LL | println!();
| ^^^^^^^^^^ unreachable expression
|
note: this expression has type `Void`, which is uninhabited
--> $DIR/void-branch.rs:10:13
--> $DIR/void-branch.rs:9:13
|
LL | std::mem::uninitialized::<Void>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -18,23 +16,22 @@ note: the lint level is defined here
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in the macro `$crate::format_args` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unreachable expression
--> $DIR/void-branch.rs:22:14
--> $DIR/void-branch.rs:25:9
|
LL | if false {
| ______________^
LL | |
LL | | infallible();
| | ------------ any code following this expression is unreachable
LL | | }
| |_____^ unreachable expression
LL | infallible();
| ------------ any code following this expression is unreachable
LL | println!()
| ^^^^^^^^^^ unreachable expression
|
note: this expression has type `Infallible`, which is uninhabited
--> $DIR/void-branch.rs:24:9
|
LL | infallible();
| ^^^^^^^^^^^^
= note: this error originates in the macro `$crate::format_args` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

Loading