From 162da894a14272b02a1a06a1f4609ee8d153ffd4 Mon Sep 17 00:00:00 2001 From: Yanjun Chen Date: Thu, 5 Nov 2026 01:28:31 -0800 Subject: [PATCH 1/2] Report downcast into uninhabited enum variant as unsupported instead of crashing An enum whose variants are all uninhabited and contain only zero-sized fields (e.g. `enum E { A(!) }`) gets the `Variants::Empty` layout of `!`, so its goto struct has no fields. The Downcast projection passed the expression through unchanged, and the following Field projection panicked in `Expr::member`, aborting codegen of the whole crate (and the whole autoharness run). Return an `UnimplementedData` error for this case instead, so the place is codegened as an unsupported construct that only fails verification if it is reachable. Resolves #3596 --- .../codegen_cprover_gotoc/codegen/place.rs | 13 ++++++++- .../uninhabited-enum-downcast/expected | 17 +++++++++++ .../uninhabited-enum-downcast/main.rs | 29 +++++++++++++++++++ .../config.yml | 5 ++++ .../expected | 4 +++ .../run.sh | 8 +++++ .../uninhabited_enum_downcast.rs | 20 +++++++++++++ 7 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/expected/uninhabited-enum-downcast/expected create mode 100644 tests/expected/uninhabited-enum-downcast/main.rs create mode 100644 tests/script-based-pre/autoharness_uninhabited_enum_downcast/config.yml create mode 100644 tests/script-based-pre/autoharness_uninhabited_enum_downcast/expected create mode 100755 tests/script-based-pre/autoharness_uninhabited_enum_downcast/run.sh create mode 100644 tests/script-based-pre/autoharness_uninhabited_enum_downcast/uninhabited_enum_downcast.rs diff --git a/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs b/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs index 99dc5159ad20..ae4177616d6b 100644 --- a/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs +++ b/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs @@ -617,7 +617,18 @@ impl GotocCtx<'_, '_> { }; let layout = self.layout_of(rustc_internal::internal(self.tcx, ty)); let expr = match &layout.variants { - Variants::Empty | Variants::Single { .. } => before.goto_expr, + // An enum whose variants are all uninhabited and contain only zero-sized fields + // (e.g. `enum E { A(!) }`) gets the layout of `!`, so its goto struct has no + // field to project into. + Variants::Empty => { + return Err(Box::new(UnimplementedData::new( + "Downcast into a variant of an uninhabited enum", + "https://github.com/model-checking/kani/issues/3596", + self.codegen_ty_stable(ty), + *before.goto_expr.location(), + ))); + } + Variants::Single { .. } => before.goto_expr, Variants::Multiple { tag_encoding, .. } => match tag_encoding { TagEncoding::Direct => { let cases = if ty_kind.is_coroutine() { diff --git a/tests/expected/uninhabited-enum-downcast/expected b/tests/expected/uninhabited-enum-downcast/expected new file mode 100644 index 000000000000..a38dc56bf8c2 --- /dev/null +++ b/tests/expected/uninhabited-enum-downcast/expected @@ -0,0 +1,17 @@ +Checking harness check_match_ref... + +Status: FAILURE\ +Description: "Downcast into a variant of an uninhabited enum is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/3596" + +VERIFICATION:- FAILED + +Checking harness check_let_ref... + +Status: FAILURE\ +Description: "Downcast into a variant of an uninhabited enum is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/3596" + +VERIFICATION:- FAILED + +Verification failed for - check_match_ref +Verification failed for - check_let_ref +Complete - 0 successfully verified harnesses, 2 failures, 2 total. diff --git a/tests/expected/uninhabited-enum-downcast/main.rs b/tests/expected/uninhabited-enum-downcast/main.rs new file mode 100644 index 000000000000..06b9c7731a3a --- /dev/null +++ b/tests/expected/uninhabited-enum-downcast/main.rs @@ -0,0 +1,29 @@ +// Copyright Kani Contributors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! An enum whose only variant holds `!` gets the layout of `!`, with no fields to project into. +//! Downcasting into that variant used to crash the compiler +//! (). Check that it is reported as an +//! unsupported construct instead. + +#![feature(never_type)] +#[derive(Copy, Clone)] +pub enum E { + A(!), +} +pub union U { + u: (), + e: E, +} + +#[kani::proof] +fn check_let_ref() { + let E::A(ref _a) = unsafe { &(&U { u: () }).e }; +} + +#[kani::proof] +fn check_match_ref() { + match unsafe { &(&U { u: () }).e } { + E::A(_a) => {} + } +} diff --git a/tests/script-based-pre/autoharness_uninhabited_enum_downcast/config.yml b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/config.yml new file mode 100644 index 000000000000..482747bf1e99 --- /dev/null +++ b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/config.yml @@ -0,0 +1,5 @@ +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT +script: run.sh +expected: expected +exit_code: 1 diff --git a/tests/script-based-pre/autoharness_uninhabited_enum_downcast/expected b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/expected new file mode 100644 index 000000000000..7424a207a66a --- /dev/null +++ b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/expected @@ -0,0 +1,4 @@ +Failed Checks: Downcast into a variant of an uninhabited enum is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/3596 +| uninhabited_enum_downcast | ok_fn | #[kani::proof] | Success | +| uninhabited_enum_downcast | trigger | #[kani::proof] | Failure | +Complete - 1 successfully verified functions, 1 failures, 2 total. diff --git a/tests/script-based-pre/autoharness_uninhabited_enum_downcast/run.sh b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/run.sh new file mode 100755 index 000000000000..e2e59684ee3b --- /dev/null +++ b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/run.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT + +# Downcasting into a variant of an uninhabited enum used to crash the compiler and abort the +# whole run (https://github.com/model-checking/kani/issues/3596). It must now only fail the +# harness for `trigger`, while `ok_fn` in the same crate is still verified. +kani autoharness -Z autoharness --output-format=regular uninhabited_enum_downcast.rs diff --git a/tests/script-based-pre/autoharness_uninhabited_enum_downcast/uninhabited_enum_downcast.rs b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/uninhabited_enum_downcast.rs new file mode 100644 index 000000000000..0d5884fe8c7f --- /dev/null +++ b/tests/script-based-pre/autoharness_uninhabited_enum_downcast/uninhabited_enum_downcast.rs @@ -0,0 +1,20 @@ +// Copyright Kani Contributors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +#![feature(never_type)] +#[derive(Copy, Clone)] +pub enum E { + A(!), +} +pub union U { + u: (), + e: E, +} + +pub fn trigger() { + let E::A(ref _a) = unsafe { &(&U { u: () }).e }; +} + +pub fn ok_fn(x: u8) -> u8 { + x / 2 +} From 9902b74a449a8bb64f0d81d3ff607f5e90233c19 Mon Sep 17 00:00:00 2001 From: Yanjun Chen Date: Sat, 7 Nov 2026 21:47:20 -0800 Subject: [PATCH 2/2] Centralize uninhabited downcast issue URL and reword comment Move the #3596 issue URL into a module-level const, and reword the comment on the `Variants::Empty` arm so it describes the layout the arm matches rather than one way to reach it. --- .../src/codegen_cprover_gotoc/codegen/place.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs b/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs index 73397b0a8102..010d8f90b82b 100644 --- a/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs +++ b/kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs @@ -18,6 +18,10 @@ use rustc_public::rustc_internal; use rustc_public::ty::{RigidTy, Ty, TyKind, VariantDef, VariantIdx}; use tracing::{debug, trace, warn}; +/// Tracking issue for downcasts into variants of uninhabited enums. +const UNINHABITED_ENUM_DOWNCAST_ISSUE_URL: &str = + "https://github.com/model-checking/kani/issues/3596"; + /// A projection in Kani can either be to a type (the normal case), /// or a variant in the case of a downcast. #[derive(Copy, Clone, Debug)] @@ -619,13 +623,13 @@ impl GotocCtx<'_, '_> { }; let layout = self.layout_of(rustc_internal::internal(self.tcx, ty)); let expr = match &layout.variants { - // An enum whose variants are all uninhabited and contain only zero-sized fields - // (e.g. `enum E { A(!) }`) gets the layout of `!`, so its goto struct has no - // field to project into. + // `Variants::Empty` is the layout rustc gives to some uninhabited types, + // e.g. `enum E { A(!) }`. Its goto struct has no fields, so a following + // field projection has nothing to project into. Variants::Empty => { return Err(Box::new(UnimplementedData::new( "Downcast into a variant of an uninhabited enum", - "https://github.com/model-checking/kani/issues/3596", + UNINHABITED_ENUM_DOWNCAST_ISSUE_URL, self.codegen_ty_stable(ty), *before.goto_expr.location(), )));