1010
1111use core:: prelude:: * ;
1212
13- use lib:: llvm:: { llvm, ValueRef , True , TypeRef , False } ;
13+ use lib:: llvm:: { llvm, ValueRef , TypeRef , Bool , True , False } ;
1414use middle:: const_eval;
1515use middle:: trans:: base;
1616use 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")
0 commit comments