Skip to content

Commit

Permalink
Propagate upstream representation change for enums with no variants
Browse files Browse the repository at this point in the history
Propagated from rust-lang/rust#133702.
Empty enums now have `Variants::Empty` as variants instead of
`Variants::Single{ index: None }`.
  • Loading branch information
Remi Delmas committed Jan 7, 2025
1 parent 1c8fd4b commit feca7ab
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ impl GotocCtx<'_> {
};
let layout = self.layout_of(rustc_internal::internal(self.tcx, ty));
let expr = match &layout.variants {
Variants::Single { .. } => before.goto_expr,
Variants::Empty | Variants::Single { .. } => before.goto_expr,
Variants::Multiple { tag_encoding, .. } => match tag_encoding {
TagEncoding::Direct => {
let cases = if ty_kind.is_coroutine() {
Expand Down
4 changes: 4 additions & 0 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ impl GotocCtx<'_> {
let variant_expr = variant_proj.goto_expr.clone();
let layout = self.layout_of_stable(res_ty);
let fields = match &layout.variants {
Variants::Empty => {
unreachable!("Aggregate expression for uninhabited enum with no variants")
}
Variants::Single { index } => {
if *index != rustc_internal::internal(self.tcx, variant_index) {
// This may occur if all variants except for the one pointed by
Expand Down Expand Up @@ -960,6 +963,7 @@ impl GotocCtx<'_> {
pub fn codegen_get_discriminant(&mut self, e: Expr, ty: Ty, res_ty: Ty) -> Expr {
let layout = self.layout_of_stable(ty);
match &layout.variants {
Variants::Empty => unreachable!("Discriminant for uninhabited enum with no variants"),
Variants::Single { index } => {
let discr_val = layout
.ty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl GotocCtx<'_> {
let variant_index_internal = rustc_internal::internal(self.tcx, variant_index);
let layout = self.layout_of(dest_ty_internal);
match &layout.variants {
Variants::Single { .. } => Stmt::skip(location),
Variants::Empty | Variants::Single { .. } => Stmt::skip(location),
Variants::Multiple { tag, tag_encoding, .. } => match tag_encoding {
TagEncoding::Direct => {
let discr = dest_ty_internal
Expand Down
9 changes: 7 additions & 2 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,12 +1191,17 @@ impl<'tcx> GotocCtx<'tcx> {
let layout = self.layout_of(ty);
// variants appearing in mir code
match &layout.variants {
Variants::Empty => {
// an empty enum with no variants (its value cannot be instantiated)
self.ensure_struct(self.ty_mangled_name(ty), pretty_name, |_, _| vec![])
}
Variants::Single { index } => {
self.ensure_struct(self.ty_mangled_name(ty), pretty_name, |gcx, _| {
match source_variants.get(*index) {
None => {
// an empty enum with no variants (its value cannot be instantiated)
vec![]
unreachable!(
"Enum with no variants must be represented as Variants::Empty"
);
}
Some(variant) => {
// a single enum is pretty much like a struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ fn data_bytes_for_ty(
// Support basic enumeration forms
let ty_variants = def.variants();
match layout.variants {
VariantsShape::Empty => Ok(vec![]),
VariantsShape::Single { index } => {
// Only one variant is reachable. This behaves like a struct.
let fields = ty_variants[index.to_index()].fields();
Expand Down
1 change: 1 addition & 0 deletions kani-compiler/src/kani_middle/transform/check_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ pub fn ty_validity_per_offset(
// Support basic enumeration forms
let ty_variants = def.variants();
match layout.variants {
VariantsShape::Empty => Ok(vec![]),
VariantsShape::Single { index } => {
// Only one variant is reachable. This behaves like a struct.
let fields = ty_variants[index.to_index()].fields();
Expand Down

0 comments on commit feca7ab

Please sign in to comment.