Skip to content

Commit 7921810

Browse files
committed
Allow constant c-like enum to integral/float cast
1 parent b7e7297 commit 7921810

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/librustc/middle/trans/consts.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use core::prelude::*;
1212

13-
use lib::llvm::{llvm, ValueRef, True, TypeRef, False};
13+
use lib::llvm::{llvm, ValueRef, TypeRef, Bool, True, False};
1414
use middle::const_eval;
1515
use middle::trans::base;
1616
use middle::trans::base::get_insn_ctxt;
@@ -304,7 +304,7 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
304304
expr::cast_type_kind(ety)) {
305305

306306
(expr::cast_integral, expr::cast_integral) => {
307-
let s = if ty::type_is_signed(basety) { True } else { False };
307+
let s = ty::type_is_signed(basety) as Bool;
308308
llvm::LLVMConstIntCast(v, llty, s)
309309
}
310310
(expr::cast_integral, expr::cast_float) => {
@@ -321,6 +321,37 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
321321
if ty::type_is_signed(ety) { llvm::LLVMConstFPToSI(v, llty) }
322322
else { llvm::LLVMConstFPToUI(v, llty) }
323323
}
324+
(expr::cast_enum, expr::cast_integral) |
325+
(expr::cast_enum, expr::cast_float) => {
326+
let def = ty::resolve_expr(cx.tcx, base);
327+
let (enum_did, variant_did) = match def {
328+
ast::def_variant(enum_did, variant_did) => {
329+
(enum_did, variant_did)
330+
}
331+
_ => cx.sess.bug(~"enum cast source is not enum")
332+
};
333+
// Note that we know this is a C-like (nullary) enum
334+
// variant or we wouldn't have gotten here
335+
let variants = ty::enum_variants(cx.tcx, enum_did);
336+
let iv = if variants.len() == 1 {
337+
// Univariants don't have a discriminant field,
338+
// because there's only one value it could have:
339+
C_integral(T_i64(),
340+
variants[0].disr_val as u64, True)
341+
} else {
342+
base::get_discrim_val(cx, e.span, enum_did, variant_did)
343+
};
344+
let ety_cast = expr::cast_type_kind(ety);
345+
match ety_cast {
346+
expr::cast_integral => {
347+
let s = ty::type_is_signed(ety) as Bool;
348+
llvm::LLVMConstIntCast(iv, llty, s)
349+
}
350+
expr::cast_float => llvm::LLVMConstUIToFP(iv, llty),
351+
_ => cx.sess.bug(~"enum cast destination is not \
352+
integral or float")
353+
}
354+
}
324355
_ => {
325356
cx.sess.impossible_case(e.span,
326357
~"bad combination of types for cast")

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3017,7 +3017,7 @@ pub fn method_call_bounds(tcx: ctxt, method_map: typeck::method_map,
30173017
}
30183018
}
30193019

3020-
fn resolve_expr(tcx: ctxt, expr: @ast::expr) -> ast::def {
3020+
pub fn resolve_expr(tcx: ctxt, expr: @ast::expr) -> ast::def {
30213021
match tcx.def_map.find(&expr.id) {
30223022
Some(def) => def,
30233023
None => {

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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2013 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+
enum A { A1, A2 }
12+
enum B { B1=0, B2=2 }
13+
14+
fn main () {
15+
const c1: int = A2 as int;
16+
const c2: int = B2 as int;
17+
const c3: float = A2 as float;
18+
const c4: float = B2 as float;
19+
let a1 = A2 as int;
20+
let a2 = B2 as int;
21+
let a3 = A2 as float;
22+
let a4 = B2 as float;
23+
assert(c1 == 1);
24+
assert(c2 == 2);
25+
assert(c3 == 1.0);
26+
assert(c4 == 2.0);
27+
assert(a1 == 1);
28+
assert(a2 == 2);
29+
assert(a3 == 1.0);
30+
assert(a4 == 2.0);
31+
}

0 commit comments

Comments
 (0)