Skip to content

unreachable code is not detected with never to any coercion in a closure #146085

@WaffleLapkin

Description

@WaffleLapkin

I tried this code:

fn main() {
    (|| panic!())();
    ();
}

I expected to see this happen: (); is marked as unreachable code

Instead, this happened: there is no warning.

This happens because we emit unreachable code lint in rustc_hir_typeck, while checking expressions (we track it via FnCtxt::diverges), which means that we can have unresolved inference variables.

In this case, when checking code reachibility we see that the type of (|| panic!())() is a (diverging) inference variable and not ! so we don't set FnCtxt::diverges to "always". Later this inference variable is set to ! (NB: currently only on edition = 2024), but at this point we don't check for unreachable code.

Meta

rustc --version --verbose:

1.91.0-nightly (2025-08-31 07d246fc6dc227903da2)

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`L-false-negativeLint: False negative (should have fired but didn't).L-unreachable_codeLint: unreachable_codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions