Skip to content

Commit 632aaa4

Browse files
noxalexcrichton
authored andcommitted
Properly handle ranges of signed enums using both extremums (fixes #49973)
1 parent 992e2f8 commit 632aaa4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/librustc/ty/layout.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1677,18 +1677,19 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
16771677
}
16781678
}
16791679

1680-
let discr = Scalar {
1680+
let tag_mask = !0u128 >> (128 - ity.size().bits());
1681+
let tag = Scalar {
16811682
value: Int(ity, signed),
1682-
valid_range: (min as u128)..=(max as u128)
1683+
valid_range: (min as u128 & tag_mask)..=(max as u128 & tag_mask),
16831684
};
1684-
let abi = if discr.value.size(dl) == size {
1685-
Abi::Scalar(discr.clone())
1685+
let abi = if tag.value.size(dl) == size {
1686+
Abi::Scalar(tag.clone())
16861687
} else {
16871688
Abi::Aggregate { sized: true }
16881689
};
16891690
tcx.intern_layout(LayoutDetails {
16901691
variants: Variants::Tagged {
1691-
discr,
1692+
discr: tag,
16921693
variants
16931694
},
16941695
fields: FieldPlacement::Arbitrary {

src/test/run-pass/issue-49973.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[derive(Debug)]
12+
#[repr(i32)]
13+
enum E {
14+
Min = -2147483648i32,
15+
_Max = 2147483647i32,
16+
}
17+
18+
fn main() {
19+
assert_eq!(Some(E::Min).unwrap() as i32, -2147483648i32);
20+
}

0 commit comments

Comments
 (0)