Skip to content

Commit a7c4b5c

Browse files
committed
Properly handle ranges of signed enums using both extremums (fixes #49973)
1 parent bc001fa commit a7c4b5c

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
@@ -1700,18 +1700,19 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
17001700
}
17011701
}
17021702

1703-
let discr = Scalar {
1703+
let tag_mask = !0u128 >> (128 - ity.size().bits());
1704+
let tag = Scalar {
17041705
value: Int(ity, signed),
1705-
valid_range: (min as u128)..=(max as u128)
1706+
valid_range: (min as u128 & tag_mask)..=(max as u128 & tag_mask),
17061707
};
1707-
let abi = if discr.value.size(dl) == size {
1708-
Abi::Scalar(discr.clone())
1708+
let abi = if tag.value.size(dl) == size {
1709+
Abi::Scalar(tag.clone())
17091710
} else {
17101711
Abi::Aggregate { sized: true }
17111712
};
17121713
tcx.intern_layout(LayoutDetails {
17131714
variants: Variants::Tagged {
1714-
discr,
1715+
discr: tag,
17151716
variants
17161717
},
17171718
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)