Skip to content

Commit 32e4fd6

Browse files
committed
Const slices now work. Something odd about non-const cases though, see #3138.
1 parent 4254084 commit 32e4fd6

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

src/rustc/middle/trans/consts.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ fn const_lit(cx: @crate_ctxt, e: @ast::expr, lit: ast::lit)
3232
// duplicate constants. I think. Maybe LLVM has a magical mode that does so
3333
// later on?
3434

35-
fn const_vec_and_sz(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr])
36-
-> (ValueRef, ValueRef) {
35+
fn const_vec(cx: @crate_ctxt, e: @ast::expr, es: &[@ast::expr])
36+
-> (ValueRef, ValueRef, TypeRef) {
3737
let vec_ty = ty::expr_ty(cx.tcx, e);
3838
let unit_ty = ty::sequence_element_type(cx.tcx, vec_ty);
3939
let llunitty = type_of::type_of(cx, unit_ty);
4040
let v = C_array(llunitty, es.map(|e| const_expr(cx, e)));
4141
let unit_sz = shape::llsize_of(cx, llunitty);
4242
let sz = llvm::LLVMConstMul(C_uint(cx, es.len()), unit_sz);
43-
return (v, sz);
43+
return (v, sz, llunitty);
4444
}
4545

4646
fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
@@ -157,7 +157,7 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
157157
C_struct(fs.map(|f| const_expr(cx, f.node.expr)))
158158
}
159159
ast::expr_vec(es, m_imm) => {
160-
let (v, _) = const_vec_and_sz(cx, e, es);
160+
let (v, _, _) = const_vec(cx, e, es);
161161
v
162162
}
163163
ast::expr_vstore(e, ast::vstore_fixed(_)) => {
@@ -173,15 +173,16 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
173173
}
174174
}
175175
ast::expr_vec(es, m_imm) => {
176-
let (cv, sz) = const_vec_and_sz(cx, e, es);
177-
let subty = ty::expr_ty(cx.tcx, sub),
178-
llty = type_of::type_of(cx, subty);
176+
let (cv, sz, llunitty) = const_vec(cx, e, es);
177+
let llty = val_ty(cv);
179178
let gv = do str::as_c_str("const") |name| {
180179
llvm::LLVMAddGlobal(cx.llmod, llty, name)
181180
};
182181
llvm::LLVMSetInitializer(gv, cv);
183182
llvm::LLVMSetGlobalConstant(gv, True);
184-
C_struct(~[gv, sz])
183+
let p = llvm::LLVMConstPointerCast(gv, T_ptr(llunitty));
184+
185+
C_struct(~[p, sz])
185186
}
186187
_ => cx.sess.span_bug(e.span,
187188
~"bad const-slice expr")

src/rustc/middle/typeck/check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,8 +2315,7 @@ fn ast_expr_vstore_to_vstore(fcx: @fn_ctxt, e: @ast::expr, n: uint,
23152315
ast::vstore_box => ty::vstore_box,
23162316
ast::vstore_slice(a_r) => match fcx.block_region() {
23172317
result::ok(b_r) => {
2318-
let rscope = in_anon_rscope(fcx, b_r);
2319-
let r = astconv::ast_region_to_region(fcx, rscope, e.span, a_r);
2318+
let r = fcx.infcx.next_region_var_with_scope_lb(e.id);
23202319
ty::vstore_slice(r)
23212320
}
23222321
result::err(msg) => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const x : [int]/4 = [1,2,3,4];
2+
const y : &[int] = &[1,2,3,4];
23

34
fn main() {
45
io::println(fmt!("%?", x[1]));
6+
io::println(fmt!("%?", y[1]));
57
assert x[1] == 2;
68
assert x[3] == 4;
9+
assert x[3] == y[3];
710
}

src/test/run-pass/estr-slice.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ fn main() {
1616

1717
let a = &"aaaa";
1818
let b = &"bbbb";
19-
let c = &"cccc";
20-
let cc = &"ccccc";
19+
20+
// let c = &"cccc";
21+
// let cc = &"ccccc";
2122

2223
log(debug, a);
2324

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

3031
log(debug, b);
3132

33+
// FIXME #3138: So then, why don't these ones work?
34+
35+
/*
3236
assert a < c;
3337
assert a <= c;
3438
assert a != c;
@@ -44,4 +48,5 @@ fn main() {
4448
assert cc > c;
4549
4650
log(debug, cc);
51+
*/
4752
}

0 commit comments

Comments
 (0)