diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index 841ffa231d1..e68e463ec34 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -73,9 +73,15 @@ pub fn const_vec(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr]) let vec_ty = ty::expr_ty(cx.tcx, e); let unit_ty = ty::sequence_element_type(cx.tcx, vec_ty); let llunitty = type_of::type_of(cx, unit_ty); - let v = C_array(llunitty, es.map(|e| const_expr(cx, *e))); let unit_sz = machine::llsize_of(cx, llunitty); let sz = llvm::LLVMConstMul(C_uint(cx, es.len()), unit_sz); + let vs = es.map(|e| const_expr(cx, *e)); + // If the vector contains enums, an LLVM array won't work. + let v = if vs.any(|vi| val_ty(*vi) != llunitty) { + C_struct(vs) + } else { + C_array(llunitty, vs) + }; return (v, sz, llunitty); } } diff --git a/src/test/run-pass/const-enum-vector.rs b/src/test/run-pass/const-enum-vector.rs new file mode 100644 index 00000000000..4925d6c0f07 --- /dev/null +++ b/src/test/run-pass/const-enum-vector.rs @@ -0,0 +1,23 @@ +// 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 E { V1(int), V0 } +const C: [E * 3] = [V0, V1(0xDEADBEE), V0]; + +fn main() { + match C[1] { + V1(n) => assert(n == 0xDEADBEE), + _ => die!() + } + match C[2] { + V0 => (), + _ => die!() + } +}