Skip to content

[const-prop] Fix ICE calculating enum discriminant #66960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2019
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
23 changes: 13 additions & 10 deletions src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,23 +1038,26 @@ where
variant_index: VariantIdx,
dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
let variant_scalar = Scalar::from_u32(variant_index.as_u32()).into();

// Layout computation excludes uninhabited variants from consideration
// therefore there's no way to represent those variants in the given layout.
if dest.layout.for_variant(self, variant_index).abi.is_uninhabited() {
throw_ub!(Unreachable);
}

match dest.layout.variants {
layout::Variants::Single { index } => {
if index != variant_index {
throw_ub!(InvalidDiscriminant(variant_scalar));
}
assert_eq!(index, variant_index);
}
layout::Variants::Multiple {
discr_kind: layout::DiscriminantKind::Tag,
discr: ref discr_layout,
discr_index,
..
} => {
if !dest.layout.ty.variant_range(*self.tcx).unwrap().contains(&variant_index) {
throw_ub!(InvalidDiscriminant(variant_scalar));
}
// No need to validate that the discriminant here because the
// `TyLayout::for_variant()` call earlier already checks the variant is valid.

let discr_val =
dest.layout.ty.discriminant_for_variant(*self.tcx, variant_index).unwrap().val;

Expand All @@ -1077,9 +1080,9 @@ where
discr_index,
..
} => {
if !variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len() {
throw_ub!(InvalidDiscriminant(variant_scalar));
}
// No need to validate that the discriminant here because the
// `TyLayout::for_variant()` call earlier already checks the variant is valid.

if variant_index != dataful_variant {
let variants_start = niche_variants.start().as_u32();
let variant_index_relative = variant_index.as_u32()
Expand Down
39 changes: 39 additions & 0 deletions src/test/ui/consts/issue-66787.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// build-pass
// compile-flags: --crate-type lib

// Regression test for ICE which occurred when const propagating an enum with three variants
// one of which is uninhabited.

pub enum ApiError {}
#[allow(dead_code)]
pub struct TokioError {
b: bool,
}
pub enum Error {
Api {
source: ApiError,
},
Ethereum,
Tokio {
source: TokioError,
},
}
struct Api;
impl IntoError<Error> for Api
{
type Source = ApiError;
fn into_error(self, error: Self::Source) -> Error {
Error::Api {
source: (|v| v)(error),
}
}
}

pub trait IntoError<E>
{
/// The underlying error
type Source;

/// Combine the information to produce the error
fn into_error(self, source: Self::Source) -> E;
}