Skip to content

Commit 1a381fa

Browse files
committed
librustc: Use *signed* extension when converting enums to floats.
Previously, constants used unsigned extension, while non-constants used signed extension. This unifies both paths to use signed extension. If this breaks your code, take a deep breath, go for a walk, and consider why you're relying on the sign extension semantics of enum-to-float casts. Closes #8230. [breaking-change]
1 parent 907d961 commit 1a381fa

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
503503
let s = ty::type_is_signed(ety) as Bool;
504504
llvm::LLVMConstIntCast(iv, llty.to_ref(), s)
505505
}
506-
expr::cast_float => llvm::LLVMConstUIToFP(iv, llty.to_ref()),
506+
expr::cast_float => llvm::LLVMConstSIToFP(iv, llty.to_ref()),
507507
_ => cx.sess().bug("enum cast destination is not \
508508
integral or float")
509509
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
// Tests that enum-to-float-casts do *signed* integer-to-float conversion.
12+
13+
enum E {
14+
L0 = -1,
15+
H0 = 1
16+
}
17+
18+
enum F {
19+
L1 = 1,
20+
H1 = 0xFFFFFFFFFFFFFFFF
21+
}
22+
23+
static C0: f32 = L0 as f32;
24+
static C1: f32 = H1 as f32;
25+
26+
pub fn main() {
27+
let a = L0 as f32;
28+
let b = C0;
29+
let c = H1 as f32;
30+
let d = C1;
31+
assert_eq!(a, -1.0f32);
32+
assert_eq!(b, -1.0f32);
33+
assert_eq!(c, -1.0f32);
34+
assert_eq!(d, -1.0f32);
35+
}

0 commit comments

Comments
 (0)