Make tuple-like structs containing enums work as constants.

This commit is contained in:
Jed Davis 2013-01-30 23:59:06 -08:00
parent 30aae3d910
commit de8dc02634
3 changed files with 40 additions and 4 deletions

View File

@ -459,14 +459,12 @@ pub fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
ast::expr_call(callee, args, _) => {
match cx.tcx.def_map.find(&callee.id) {
Some(ast::def_struct(def_id)) => {
let ety = ty::expr_ty(cx.tcx, e);
let llty = type_of::type_of(cx, ety);
let llstructbody =
C_struct(args.map(|a| const_expr(cx, *a)));
if ty::ty_dtor(cx.tcx, def_id).is_present() {
C_named_struct(llty, ~[ llstructbody, C_u8(0) ])
C_struct(~[ llstructbody, C_u8(0) ])
} else {
C_named_struct(llty, ~[ llstructbody ])
C_struct(~[ llstructbody ])
}
}
Some(ast::def_variant(tid, vid)) => {

View File

@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
enum E { V16(u16), V32(u32) }
struct S(E, u16, u16);
const C: S = S(V16(0xDEAD), 0x600D, 0xBAD);
fn main() {
let S(_, n, _) = C;
assert n != 0xBAD;
assert n == 0x600D;
}

View File

@ -0,0 +1,19 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
enum E { V0, V16(u16) }
struct S(E, u16, u16);
const C: S = S(V0, 0x600D, 0xBAD);
fn main() {
let S(_, n, _) = C;
assert n != 0xBAD;
assert n == 0x600D;
}