Skip to content

Allow vector repeat exprs in statics. #8483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub fn check_expr(sess: Session,
expr_field(*) |
expr_index(*) |
expr_tup(*) |
expr_repeat(*) |
expr_struct(*) => { }
expr_addr_of(*) => {
sess.span_err(
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ pub fn classify(e: &expr,
lookup_constness(tcx, e)
}

ast::expr_repeat(*) => general_const,

_ => non_const
};
tcx.ccache.insert(did, cn);
Expand Down
18 changes: 18 additions & 0 deletions src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use middle::trans::type_::Type;

use std::c_str::ToCStr;
use std::libc::c_uint;
use std::vec;
use syntax::{ast, ast_util, ast_map};

pub fn const_lit(cx: &mut CrateContext, e: &ast::expr, lit: ast::lit)
Expand Down Expand Up @@ -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")
}
}
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) => {
assert_eq!(pth.types.len(), 0);
let tcx = cx.tcx;
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/static-vec-repeat-not-constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn foo() -> int { 23 }

static a: [int, ..2] = [foo(), ..2]; //~ ERROR: function calls in constants are limited to struct and enum constructors

fn main() {}
16 changes: 16 additions & 0 deletions src/test/run-pass/repeat-expr-in-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

static FOO: [int, ..4] = [32, ..4];
static BAR: [int, ..4] = [32, 32, 32, 32];

pub fn main() {
assert_eq!(FOO, BAR);
}