librustc: Allow vector repeat exprs in statics.

This commit is contained in:
Luqman Aden 2013-08-12 21:10:29 -04:00
parent 35040275b3
commit ccc85b1ac7
3 changed files with 21 additions and 0 deletions

View File

@ -160,6 +160,7 @@ pub fn check_expr(sess: Session,
expr_field(*) | expr_field(*) |
expr_index(*) | expr_index(*) |
expr_tup(*) | expr_tup(*) |
expr_repeat(*) |
expr_struct(*) => { } expr_struct(*) => { }
expr_addr_of(*) => { expr_addr_of(*) => {
sess.span_err( sess.span_err(

View File

@ -153,6 +153,8 @@ pub fn classify(e: &expr,
lookup_constness(tcx, e) lookup_constness(tcx, e)
} }
ast::expr_repeat(*) => general_const,
_ => non_const _ => non_const
}; };
tcx.ccache.insert(did, cn); tcx.ccache.insert(did, cn);

View File

@ -32,6 +32,7 @@ use middle::trans::type_::Type;
use std::c_str::ToCStr; use std::c_str::ToCStr;
use std::libc::c_uint; use std::libc::c_uint;
use std::vec;
use syntax::{ast, ast_util, ast_map}; use syntax::{ast, ast_util, ast_map};
pub fn const_lit(cx: &mut CrateContext, e: &ast::expr, lit: ast::lit) pub fn const_lit(cx: &mut CrateContext, e: &ast::expr, lit: ast::lit)
@ -540,6 +541,23 @@ fn const_expr_unadjusted(cx: @mut CrateContext, e: &ast::expr) -> ValueRef {
_ => cx.sess.span_bug(e.span, "bad const-slice expr") _ => cx.sess.span_bug(e.span, "bad const-slice expr")
} }
} }
ast::expr_repeat(elem, count, _) => {
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 n = match const_eval::eval_const_expr(cx.tcx, count) {
const_eval::const_int(i) => i as uint,
const_eval::const_uint(i) => i as uint,
_ => cx.sess.span_bug(count.span, "count must be integral const expression.")
};
let vs = vec::from_elem(n, const_expr(cx, elem));
let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
C_struct(vs)
} else {
C_array(llunitty, vs)
};
v
}
ast::expr_path(ref pth) => { ast::expr_path(ref pth) => {
assert_eq!(pth.types.len(), 0); assert_eq!(pth.types.len(), 0);
let tcx = cx.tcx; let tcx = cx.tcx;