Skip to content

Commit 95c0747

Browse files
committed
auto merge of #5182 : youknowone/rust/enum-cast, r=brson
Fix #4926
2 parents 8c3728f + 7921810 commit 95c0747

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;
@@ -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")

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3040,7 +3040,7 @@ pub fn method_call_bounds(tcx: ctxt, method_map: typeck::method_map,
30403040
}
30413041
}
30423042

3043-
fn resolve_expr(tcx: ctxt, expr: @ast::expr) -> ast::def {
3043+
pub fn resolve_expr(tcx: ctxt, expr: @ast::expr) -> ast::def {
30443044
match tcx.def_map.find(&expr.id) {
30453045
Some(def) => def,
30463046
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)