Skip to content

Commit

Permalink
Const slices now work. Something odd about non-const cases though, see
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Aug 7, 2012
1 parent 4254084 commit 32e4fd6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/rustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ fn const_lit(cx: @crate_ctxt, e: @ast::expr, lit: ast::lit)
// duplicate constants. I think. Maybe LLVM has a magical mode that does so
// later on?

fn const_vec_and_sz(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr])
-> (ValueRef, ValueRef) {
fn const_vec(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr])
-> (ValueRef, ValueRef, TypeRef) {
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 = shape::llsize_of(cx, llunitty);
let sz = llvm::LLVMConstMul(C_uint(cx, es.len()), unit_sz);
return (v, sz);
return (v, sz, llunitty);
}

fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
Expand Down Expand Up @@ -157,7 +157,7 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
C_struct(fs.map(|f| const_expr(cx, f.node.expr)))
}
ast::expr_vec(es, m_imm) => {
let (v, _) = const_vec_and_sz(cx, e, es);
let (v, _, _) = const_vec(cx, e, es);
v
}
ast::expr_vstore(e, ast::vstore_fixed(_)) => {
Expand All @@ -173,15 +173,16 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
}
}
ast::expr_vec(es, m_imm) => {
let (cv, sz) = const_vec_and_sz(cx, e, es);
let subty = ty::expr_ty(cx.tcx, sub),
llty = type_of::type_of(cx, subty);
let (cv, sz, llunitty) = const_vec(cx, e, es);
let llty = val_ty(cv);
let gv = do str::as_c_str("const") |name| {
llvm::LLVMAddGlobal(cx.llmod, llty, name)
};
llvm::LLVMSetInitializer(gv, cv);
llvm::LLVMSetGlobalConstant(gv, True);
C_struct(~[gv, sz])
let p = llvm::LLVMConstPointerCast(gv, T_ptr(llunitty));

C_struct(~[p, sz])
}
_ => cx.sess.span_bug(e.span,
~"bad const-slice expr")
Expand Down
3 changes: 1 addition & 2 deletions src/rustc/middle/typeck/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2315,8 +2315,7 @@ fn ast_expr_vstore_to_vstore(fcx: @fn_ctxt, e: @ast::expr, n: uint,
ast::vstore_box => ty::vstore_box,
ast::vstore_slice(a_r) => match fcx.block_region() {
result::ok(b_r) => {
let rscope = in_anon_rscope(fcx, b_r);
let r = astconv::ast_region_to_region(fcx, rscope, e.span, a_r);
let r = fcx.infcx.next_region_var_with_scope_lb(e.id);
ty::vstore_slice(r)
}
result::err(msg) => {
Expand Down
3 changes: 3 additions & 0 deletions src/test/run-pass/const-vecs-and-slices.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const x : [int]/4 = [1,2,3,4];
const y : &[int] = &[1,2,3,4];

fn main() {
io::println(fmt!("%?", x[1]));
io::println(fmt!("%?", y[1]));
assert x[1] == 2;
assert x[3] == 4;
assert x[3] == y[3];
}
9 changes: 7 additions & 2 deletions src/test/run-pass/estr-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ fn main() {

let a = &"aaaa";
let b = &"bbbb";
let c = &"cccc";
let cc = &"ccccc";

// let c = &"cccc";
// let cc = &"ccccc";

log(debug, a);

Expand All @@ -29,6 +30,9 @@ fn main() {

log(debug, b);

// FIXME #3138: So then, why don't these ones work?

/*
assert a < c;
assert a <= c;
assert a != c;
Expand All @@ -44,4 +48,5 @@ fn main() {
assert cc > c;
log(debug, cc);
*/
}

0 comments on commit 32e4fd6

Please sign in to comment.