Skip to content

Commit 25c47a4

Browse files
committed
rustc_codegen_llvm: don't treat i1 as signed, even for #[repr(i8)] enums.
1 parent 68cee8b commit 25c47a4

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/librustc_codegen_llvm/mir/place.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
275275
layout::Variants::Single { .. } => bug!(),
276276
layout::Variants::Tagged { ref tag, .. } => {
277277
let signed = match tag.value {
278-
layout::Int(_, signed) => signed,
278+
// We use `i1` for bytes that are always `0` or `1`,
279+
// e.g. `#[repr(i8)] enum E { A, B }`, but we can't
280+
// let LLVM interpret the `i1` as signed, because
281+
// then `i1 1` (i.e. E::B) is effectively `i8 -1`.
282+
layout::Int(_, signed) => !tag.is_bool() && signed,
279283
_ => false
280284
};
281285
bx.intcast(lldiscr, cast_to, signed)

src/librustc_codegen_llvm/mir/rvalue.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
298298
let mut signed = false;
299299
if let layout::Abi::Scalar(ref scalar) = operand.layout.abi {
300300
if let layout::Int(_, s) = scalar.value {
301-
signed = s;
301+
// We use `i1` for bytes that are always `0` or `1`,
302+
// e.g. `#[repr(i8)] enum E { A, B }`, but we can't
303+
// let LLVM interpret the `i1` as signed, because
304+
// then `i1 1` (i.e. E::B) is effectively `i8 -1`.
305+
signed = !scalar.is_bool() && s;
302306

303307
if scalar.valid_range.end() > scalar.valid_range.start() {
304308
// We want `table[e as usize]` to not

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

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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+
#![feature(core_intrinsics)]
12+
13+
#[repr(i8)]
14+
pub enum Enum {
15+
VariantA,
16+
VariantB,
17+
}
18+
19+
fn make_b() -> Enum { Enum::VariantB }
20+
21+
fn main() {
22+
assert_eq!(1, make_b() as i8);
23+
assert_eq!(1, make_b() as u8);
24+
assert_eq!(1, make_b() as i32);
25+
assert_eq!(1, make_b() as u32);
26+
assert_eq!(1, unsafe { std::intrinsics::discriminant_value(&make_b()) });
27+
}

0 commit comments

Comments
 (0)