From 6043a72774b1541aa165453fb2fdb96b58faaf86 Mon Sep 17 00:00:00 2001 From: Jed Davis Date: Sun, 30 Dec 2012 20:23:29 -0800 Subject: [PATCH 1/2] Make consts of degenerate nullary enums not ICE. This makes the const construction code match the logic in type_of. --- src/librustc/middle/trans/consts.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index 94da557d39b2a..2d0ac47fd777b 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -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); From 3e7ab3a443a3c49d457226e7842a7256fe07933b Mon Sep 17 00:00:00 2001 From: Jed Davis Date: Fri, 4 Jan 2013 18:43:36 -0800 Subject: [PATCH 2/2] Add a test for nullary univariant enums. --- .../run-pass/const-nullary-univariant-enum.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/run-pass/const-nullary-univariant-enum.rs diff --git a/src/test/run-pass/const-nullary-univariant-enum.rs b/src/test/run-pass/const-nullary-univariant-enum.rs new file mode 100644 index 0000000000000..e1db50d566b43 --- /dev/null +++ b/src/test/run-pass/const-nullary-univariant-enum.rs @@ -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 or the MIT license +// , 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); +}