diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 5b263f7680131..1fd7e9aff7a4b 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -1082,9 +1082,13 @@ where } if variant_index != dataful_variant { let variants_start = niche_variants.start().as_u32(); - let variant_index_relative = variant_index.as_u32() - .checked_sub(variants_start) - .expect("overflow computing relative variant idx"); + + let (variant_index_relative, op) = if variant_index.as_u32() >= variants_start { + (variant_index.as_u32() - variants_start, mir::BinOp::Add) + } else { + (variants_start - variant_index.as_u32(), mir::BinOp::Sub) + }; + // We need to use machine arithmetic when taking into account `niche_start`: // discr_val = variant_index_relative + niche_start_val let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?; @@ -1092,7 +1096,7 @@ where let variant_index_relative_val = ImmTy::from_uint(variant_index_relative, discr_layout); let discr_val = self.binary_op( - mir::BinOp::Add, + op, variant_index_relative_val, niche_start_val, )?; diff --git a/src/test/ui/consts/issue-66787.rs b/src/test/ui/consts/issue-66787.rs new file mode 100644 index 0000000000000..39f6ce4ec1095 --- /dev/null +++ b/src/test/ui/consts/issue-66787.rs @@ -0,0 +1,39 @@ +// build-pass +// compile-flags: --crate-type lib + +// Regression test for ICE which occurred when const propagating an enum whose discriminant +// niche triggered an integer underflow conmupting a delta. + +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 for Api +{ + type Source = ApiError; + fn into_error(self, error: Self::Source) -> Error { + Error::Api { + source: (|v| v)(error), + } + } +} + +pub trait IntoError +{ + /// The underlying error + type Source; + + /// Combine the information to produce the error + fn into_error(self, source: Self::Source) -> E; +}