Const slices now work. Something odd about non-const cases though, see #3138.

This commit is contained in:
Graydon Hoare 2012-08-07 15:04:40 -07:00
parent 42540841f3
commit 32e4fd62e9
4 changed files with 20 additions and 12 deletions

View File

@ -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 {
@ -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(_)) => {
@ -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")

View File

@ -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) => {

View File

@ -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];
}

View File

@ -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);
@ -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;
@ -44,4 +48,5 @@ fn main() {
assert cc > c;
log(debug, cc);
*/
}