Skip to content

Commit 65d201f

Browse files
committed
Auto merge of #49981 - nox:fix-signed-niches, r=eddyb
Properly handle ranges of signed enums using both extremums (fixes #49973) Fixes #49973.
2 parents f4bb956 + a7c4b5c commit 65d201f

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
@@ -1705,18 +1705,19 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
17051705
}
17061706
}
17071707

1708-
let discr = Scalar {
1708+
let tag_mask = !0u128 >> (128 - ity.size().bits());
1709+
let tag = Scalar {
17091710
value: Int(ity, signed),
1710-
valid_range: (min as u128)..=(max as u128)
1711+
valid_range: (min as u128 & tag_mask)..=(max as u128 & tag_mask),
17111712
};
1712-
let abi = if discr.value.size(dl) == size {
1713-
Abi::Scalar(discr.clone())
1713+
let abi = if tag.value.size(dl) == size {
1714+
Abi::Scalar(tag.clone())
17141715
} else {
17151716
Abi::Aggregate { sized: true }
17161717
};
17171718
tcx.intern_layout(LayoutDetails {
17181719
variants: Variants::Tagged {
1719-
discr,
1720+
discr: tag,
17201721
variants
17211722
},
17221723
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)