Skip to content
Merged
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
17 changes: 16 additions & 1 deletion kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -619,7 +623,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,
// `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",
UNINHABITED_ENUM_DOWNCAST_ISSUE_URL,
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() {
Expand Down
17 changes: 17 additions & 0 deletions tests/expected/uninhabited-enum-downcast/expected
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions tests/expected/uninhabited-enum-downcast/main.rs
Original file line number Diff line number Diff line change
@@ -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
//! (<https://github.com/model-checking/kani/issues/3596>). 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) => {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT
script: run.sh
expected: expected
exit_code: 1
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Loading