Merge pull request #4358 from jld/constenum-unit

Fix consts of nullary univariant enum type.
This commit is contained in:
Tim Chevalier 2013-01-07 13:40:05 -08:00
commit 78aad60062
2 changed files with 26 additions and 2 deletions

View File

@ -398,7 +398,6 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
// forbids paths that don't map to C-like enum variants.
let ety = ty::expr_ty(cx.tcx, e);
let llty = type_of::type_of(cx, ety);
let llstructtys = lib::llvm::struct_element_types(llty);
// Can't use `discrims` from the crate context here because
// those discriminants have an extra level of indirection,
@ -422,8 +421,14 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
lldiscrim = found_lldiscrim;
}
}
let fields = if ty::enum_is_univariant(cx.tcx, enum_did) {
~[lldiscrim]
} else {
let llstructtys = lib::llvm::struct_element_types(llty);
~[lldiscrim, C_null(llstructtys[1])]
};
C_named_struct(llty, ~[ lldiscrim, C_null(llstructtys[1]) ])
C_named_struct(llty, fields)
}
Some(ast::def_struct(_)) => {
let ety = ty::expr_ty(cx.tcx, e);

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 Foo {
Bar = 0xDEADBEE
}
const X: Foo = Bar;
fn main() {
assert((X as uint) == 0xDEADBEE);
}