Skip to content

Commit 79a7ad4

Browse files
committed
---
yaml --- r: 48495 b: refs/heads/snap-stage3 c: 95c0747 h: refs/heads/master i: 48493: 05e7a1c 48491: 45af499 48487: 7e7420a 48479: 8839830 v: v3
1 parent e18c5c2 commit 79a7ad4

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 3bbcac322669cff3abde5be937cc4ec3860f3985
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 8c3728f839acd5bc1a3e1e110be832506777f746
4+
refs/heads/snap-stage3: 95c07479dd9c7a0438a749638987deb15977cd92
55
refs/heads/try: 2a8fb58d79e685d5ca07b039badcf2ae3ef077ea
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/librustc/middle/trans/consts.rs

Lines changed: 33 additions & 2 deletions
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")

branches/snap-stage3/src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
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 => {
Lines changed: 31 additions & 0 deletions
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)