Skip to content

Commit 4379c86

Browse files
committed
Auto merge of #49403 - oli-obk:try2, r=eddyb
Trim discriminants to their final type size r? @eddyb fixes #49181
2 parents 051050d + 422efd7 commit 4379c86

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/librustc_mir/hair/pattern/mod.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -851,13 +851,38 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
851851
ty::TyAdt(adt_def, substs) if adt_def.is_enum() => {
852852
match cv.val {
853853
ConstVal::Value(val) => {
854-
let discr = const_discr(
854+
let discr_val = const_discr(
855855
self.tcx, self.param_env, instance, val, cv.ty
856-
).unwrap();
857-
let variant_index = adt_def
858-
.discriminants(self.tcx)
859-
.position(|var| var.val == discr)
860-
.unwrap();
856+
).expect("const_discr failed");
857+
let layout = self
858+
.tcx
859+
.layout_of(self.param_env.and(cv.ty))
860+
.expect("layout of enum not available");
861+
let variant_index = match layout.variants {
862+
ty::layout::Variants::Single { index } => index,
863+
ty::layout::Variants::Tagged { ref discr, .. } => {
864+
// raw discriminants for enums are isize or bigger during
865+
// their computation, but later shrunk to the smallest possible
866+
// representation
867+
let size = discr.value.size(self.tcx).bits();
868+
let amt = 128 - size;
869+
adt_def
870+
.discriminants(self.tcx)
871+
.position(|var| ((var.val << amt) >> amt) == discr_val)
872+
.unwrap_or_else(|| {
873+
bug!("discriminant {} not found in {:#?}",
874+
discr_val,
875+
adt_def
876+
.discriminants(self.tcx)
877+
.collect::<Vec<_>>(),
878+
);
879+
})
880+
}
881+
ty::layout::Variants::NicheFilling { .. } => {
882+
assert_eq!(discr_val as usize as u128, discr_val);
883+
discr_val as usize
884+
},
885+
};
861886
let subpatterns = adt_subpatterns(
862887
adt_def.variants[variant_index].fields.len(),
863888
Some(variant_index),

src/test/run-pass/match-arm-statics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ fn issue_14576() {
9494
const F : C = C::D;
9595

9696
assert_eq!(match C::D { F => 1, _ => 2, }, 1);
97+
98+
// test gaps
99+
#[derive(PartialEq, Eq)]
100+
enum G { H = 3, I = 5 }
101+
const K : G = G::I;
102+
103+
assert_eq!(match G::I { K => 1, _ => 2, }, 1);
97104
}
98105

99106
fn issue_13731() {

0 commit comments

Comments
 (0)