Skip to content

Commit dbd29ea

Browse files
committed
auto merge of #14874 : pcwalton/rust/enum-to-float-casts-part-deux, r=alexcrichton
Closes #14794. If you're casting from an enum to a float, cast through an integer first. [breaking-change] r? @alexcrichton
2 parents 18c451f + 30772d9 commit dbd29ea

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,8 @@ 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::LLVMConstSIToFP(iv, llty.to_ref()),
507506
_ => cx.sess().bug("enum cast destination is not \
508-
integral or float")
507+
integral")
509508
}
510509
}
511510
(expr::cast_pointer, expr::cast_pointer) => {

src/librustc/middle/ty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,14 @@ pub fn type_is_scalar(ty: t) -> bool {
16851685
}
16861686
}
16871687

1688+
/// Returns true if this type is a floating point type and false otherwise.
1689+
pub fn type_is_floating_point(ty: t) -> bool {
1690+
match get(ty).sty {
1691+
ty_float(_) => true,
1692+
_ => false,
1693+
}
1694+
}
1695+
16881696
pub fn type_needs_drop(cx: &ctxt, ty: t) -> bool {
16891697
type_contents(cx, ty).needs_drop(cx)
16901698
}

src/librustc/middle/typeck/check/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3042,12 +3042,24 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30423042
let t_1_is_scalar = type_is_scalar(fcx, expr.span, t_1);
30433043
let t_1_is_char = type_is_char(fcx, expr.span, t_1);
30443044
let t_1_is_bare_fn = type_is_bare_fn(fcx, expr.span, t_1);
3045+
let t_1_is_float = type_is_floating_point(fcx,
3046+
expr.span,
3047+
t_1);
30453048

30463049
// casts to scalars other than `char` and `bare fn` are trivial
30473050
let t_1_is_trivial = t_1_is_scalar &&
30483051
!t_1_is_char && !t_1_is_bare_fn;
30493052

3050-
if type_is_c_like_enum(fcx, expr.span, t_e) && t_1_is_trivial {
3053+
if type_is_c_like_enum(fcx, expr.span, t_e) &&
3054+
t_1_is_trivial {
3055+
if t_1_is_float {
3056+
fcx.type_error_message(expr.span, |actual| {
3057+
format!("illegal cast; cast through an \
3058+
integer first: `{}` as `{}`",
3059+
actual,
3060+
fcx.infcx().ty_to_str(t_1))
3061+
}, t_e, None);
3062+
}
30513063
// casts from C-like enums are allowed
30523064
} else if t_1_is_char {
30533065
let te = fcx.infcx().resolve_type_vars_if_possible(te);
@@ -4206,6 +4218,11 @@ pub fn type_is_bare_fn(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
42064218
return ty::type_is_bare_fn(typ_s);
42074219
}
42084220

4221+
pub fn type_is_floating_point(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
4222+
let typ_s = structurally_resolved_type(fcx, sp, typ);
4223+
return ty::type_is_floating_point(typ_s);
4224+
}
4225+
42094226
pub fn type_is_unsafe_ptr(fcx: &FnCtxt, sp: Span, typ: ty::t) -> bool {
42104227
let typ_s = structurally_resolved_type(fcx, sp, typ);
42114228
return ty::type_is_unsafe_ptr(typ_s);

src/test/run-pass/enum-to-float-cast.rs renamed to src/test/compile-fail/enum-to-float-cast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Tests that enum-to-float-casts do *signed* integer-to-float conversion.
11+
// Tests that enum-to-float casts are disallowed.
1212

1313
enum E {
1414
L0 = -1,
@@ -20,13 +20,13 @@ enum F {
2020
H1 = 0xFFFFFFFFFFFFFFFF
2121
}
2222

23-
static C0: f32 = L0 as f32;
24-
static C1: f32 = H1 as f32;
23+
static C0: f32 = L0 as f32; //~ ERROR illegal cast
24+
static C1: f32 = H1 as f32; //~ ERROR illegal cast
2525

2626
pub fn main() {
27-
let a = L0 as f32;
27+
let a = L0 as f32; //~ ERROR illegal cast
2828
let b = C0;
29-
let c = H1 as f32;
29+
let c = H1 as f32; //~ ERROR illegal cast
3030
let d = C1;
3131
assert_eq!(a, -1.0f32);
3232
assert_eq!(b, -1.0f32);

src/test/run-pass/const-enum-cast.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ enum B { B1=0, B2=2 }
1414
pub fn main () {
1515
static c1: int = A2 as int;
1616
static c2: int = B2 as int;
17-
static c3: f64 = A2 as f64;
18-
static c4: f64 = B2 as f64;
1917
let a1 = A2 as int;
2018
let a2 = B2 as int;
21-
let a3 = A2 as f64;
22-
let a4 = B2 as f64;
2319
assert_eq!(c1, 1);
2420
assert_eq!(c2, 2);
25-
assert_eq!(c3, 1.0);
26-
assert_eq!(c4, 2.0);
2721
assert_eq!(a1, 1);
2822
assert_eq!(a2, 2);
29-
assert_eq!(a3, 1.0);
30-
assert_eq!(a4, 2.0);
3123
}

src/test/run-pass/enum-disr-val-pretty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ pub fn main() {
2121

2222
fn test_color(color: color, val: int, _name: String) {
2323
assert!(color as int == val);
24-
assert!(color as f64 == val as f64);
2524
}

src/test/run-pass/tag-variant-disr-val.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub fn main() {
4040
fn test_color(color: color, val: int, name: String) {
4141
//assert!(unsafe::transmute(color) == val);
4242
assert_eq!(color as int, val);
43-
assert_eq!(color as f64, val as f64);
4443
assert!(get_color_alt(color) == name);
4544
assert!(get_color_if(color) == name);
4645
}

0 commit comments

Comments
 (0)