Skip to content

Commit 7f58552

Browse files
committed
auto merge of #8483 : luqmana/rust/rexprs, r=catamorphism
Fixes #8152.
2 parents 7cda0d4 + a2ed09a commit 7f58552

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-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;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() -> int { 23 }
12+
13+
static a: [int, ..2] = [foo(), ..2]; //~ ERROR: function calls in constants are limited to struct and enum constructors
14+
15+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
static FOO: [int, ..4] = [32, ..4];
12+
static BAR: [int, ..4] = [32, 32, 32, 32];
13+
14+
pub fn main() {
15+
assert_eq!(FOO, BAR);
16+
}

0 commit comments

Comments
 (0)