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;
@@ -323,7 +323,7 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
323323 expr:: cast_type_kind ( ety) ) {
324324
325325 ( expr:: cast_integral, expr:: cast_integral) => {
326- let s = if ty:: type_is_signed ( basety) { True } else { False } ;
326+ let s = ty:: type_is_signed ( basety) as Bool ;
327327 llvm:: LLVMConstIntCast ( v, llty, s)
328328 }
329329 ( expr:: cast_integral, expr:: cast_float) => {
@@ -340,6 +340,37 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
340340 if ty:: type_is_signed ( ety) { llvm:: LLVMConstFPToSI ( v, llty) }
341341 else { llvm:: LLVMConstFPToUI ( v, llty) }
342342 }
343+ ( expr:: cast_enum, expr:: cast_integral) |
344+ ( expr:: cast_enum, expr:: cast_float) => {
345+ let def = ty:: resolve_expr ( cx. tcx , base) ;
346+ let ( enum_did, variant_did) = match def {
347+ ast:: def_variant( enum_did, variant_did) => {
348+ ( enum_did, variant_did)
349+ }
350+ _ => cx. sess . bug ( ~"enum cast source is not enum ")
351+ } ;
352+ // Note that we know this is a C-like (nullary) enum
353+ // variant or we wouldn't have gotten here
354+ let variants = ty:: enum_variants ( cx. tcx , enum_did) ;
355+ let iv = if variants. len ( ) == 1 {
356+ // Univariants don't have a discriminant field,
357+ // because there's only one value it could have:
358+ C_integral ( T_i64 ( ) ,
359+ variants[ 0 ] . disr_val as u64 , True )
360+ } else {
361+ base:: get_discrim_val ( cx, e. span , enum_did, variant_did)
362+ } ;
363+ let ety_cast = expr:: cast_type_kind ( ety) ;
364+ match ety_cast {
365+ expr:: cast_integral => {
366+ let s = ty:: type_is_signed ( ety) as Bool ;
367+ llvm:: LLVMConstIntCast ( iv, llty, s)
368+ }
369+ expr:: cast_float => llvm:: LLVMConstUIToFP ( iv, llty) ,
370+ _ => cx. sess . bug ( ~"enum cast destination is not \
371+ integral or float")
372+ }
373+ }
343374 _ => {
344375 cx. sess . impossible_case ( e. span ,
345376 ~"bad combination of types for cast")
0 commit comments