Skip to content

Commit ccc85b1

Browse files
committed
librustc: Allow vector repeat exprs in statics.
1 parent 3504027 commit ccc85b1

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/librustc/middle/check_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub fn check_expr(sess: Session,
160160
expr_field(*) |
161161
expr_index(*) |
162162
expr_tup(*) |
163+
expr_repeat(*) |
163164
expr_struct(*) => { }
164165
expr_addr_of(*) => {
165166
sess.span_err(

src/librustc/middle/const_eval.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub fn classify(e: &expr,
153153
lookup_constness(tcx, e)
154154
}
155155

156+
ast::expr_repeat(*) => general_const,
157+
156158
_ => non_const
157159
};
158160
tcx.ccache.insert(did, cn);

src/librustc/middle/trans/consts.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use middle::trans::type_::Type;
3232

3333
use std::c_str::ToCStr;
3434
use std::libc::c_uint;
35+
use std::vec;
3536
use syntax::{ast, ast_util, ast_map};
3637

3738
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 {
540541
_ => cx.sess.span_bug(e.span, "bad const-slice expr")
541542
}
542543
}
544+
ast::expr_repeat(elem, count, _) => {
545+
let vec_ty = ty::expr_ty(cx.tcx, e);
546+
let unit_ty = ty::sequence_element_type(cx.tcx, vec_ty);
547+
let llunitty = type_of::type_of(cx, unit_ty);
548+
let n = match const_eval::eval_const_expr(cx.tcx, count) {
549+
const_eval::const_int(i) => i as uint,
550+
const_eval::const_uint(i) => i as uint,
551+
_ => cx.sess.span_bug(count.span, "count must be integral const expression.")
552+
};
553+
let vs = vec::from_elem(n, const_expr(cx, elem));
554+
let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
555+
C_struct(vs)
556+
} else {
557+
C_array(llunitty, vs)
558+
};
559+
v
560+
}
543561
ast::expr_path(ref pth) => {
544562
assert_eq!(pth.types.len(), 0);
545563
let tcx = cx.tcx;

0 commit comments

Comments
 (0)