Skip to content

Commit de8dc02

Browse files
committed
Make tuple-like structs containing enums work as constants.
1 parent 30aae3d commit de8dc02

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/librustc/middle/trans/consts.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,12 @@ pub fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
459459
ast::expr_call(callee, args, _) => {
460460
match cx.tcx.def_map.find(&callee.id) {
461461
Some(ast::def_struct(def_id)) => {
462-
let ety = ty::expr_ty(cx.tcx, e);
463-
let llty = type_of::type_of(cx, ety);
464462
let llstructbody =
465463
C_struct(args.map(|a| const_expr(cx, *a)));
466464
if ty::ty_dtor(cx.tcx, def_id).is_present() {
467-
C_named_struct(llty, ~[ llstructbody, C_u8(0) ])
465+
C_struct(~[ llstructbody, C_u8(0) ])
468466
} else {
469-
C_named_struct(llty, ~[ llstructbody ])
467+
C_struct(~[ llstructbody ])
470468
}
471469
}
472470
Some(ast::def_variant(tid, vid)) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 E { V16(u16), V32(u32) }
12+
struct S(E, u16, u16);
13+
const C: S = S(V16(0xDEAD), 0x600D, 0xBAD);
14+
15+
fn main() {
16+
let S(_, n, _) = C;
17+
assert n != 0xBAD;
18+
assert n == 0x600D;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 E { V0, V16(u16) }
12+
struct S(E, u16, u16);
13+
const C: S = S(V0, 0x600D, 0xBAD);
14+
15+
fn main() {
16+
let S(_, n, _) = C;
17+
assert n != 0xBAD;
18+
assert n == 0x600D;
19+
}

0 commit comments

Comments
 (0)