Skip to content

Commit 488ece0

Browse files
committed
Implement &-expressions in consts. Part of #2317.
1 parent e02b1b1 commit 488ece0

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

src/rustc/middle/borrowck/preserve.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ impl private_methods for &preserve_ctxt {
7474
// when we borrow an rvalue, we can keep it rooted but only
7575
// up to the root_ub point
7676

77+
// When we're in a 'const &x = ...' context, self.root_ub is
78+
// zero and the rvalue is static, not bound to a scope.
79+
let scope_region = if self.root_ub == 0 {
80+
ty::re_static
81+
} else {
82+
ty::re_scope(self.root_ub)
83+
};
84+
7785
// FIXME(#2977)--need to update trans!
78-
self.compare_scope(cmt, ty::re_scope(self.root_ub))
86+
self.compare_scope(cmt, scope_region)
7987
}
8088
cat_stack_upvar(cmt) {
8189
self.preserve(cmt)

src/rustc/middle/check_const.rs

+7
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,15 @@ fn check_expr(sess: session, def_map: resolve3::DefMap,
101101
}
102102
}
103103
}
104+
expr_addr_of(m_imm, _) |
104105
expr_tup(*) |
105106
expr_rec(*) { }
107+
expr_addr_of(*) {
108+
sess.span_err(
109+
e.span,
110+
~"borrowed pointers in constants may only refer to \
111+
immutable values");
112+
}
106113
_ {
107114
sess.span_err(e.span,
108115
~"constant contains unimplemented expression type");

src/rustc/middle/trans/consts.rs

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ fn const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
130130
}
131131
}
132132
}
133+
ast::expr_addr_of(ast::m_imm, sub) {
134+
let cv = const_expr(cx, sub);
135+
let subty = ty::expr_ty(cx.tcx, sub),
136+
llty = type_of::type_of(cx, subty);
137+
let gv = do str::as_c_str("const") |name| {
138+
llvm::LLVMAddGlobal(cx.llmod, llty, name)
139+
};
140+
llvm::LLVMSetInitializer(gv, cv);
141+
llvm::LLVMSetGlobalConstant(gv, True);
142+
gv
143+
}
133144
ast::expr_tup(es) {
134145
C_struct(es.map(|e| const_expr(cx, e)))
135146
}

src/rustc/middle/typeck/rscope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ trait region_scope {
88
enum empty_rscope { empty_rscope }
99
impl of region_scope for empty_rscope {
1010
fn anon_region() -> result<ty::region, ~str> {
11-
result::err(~"region types are not allowed here")
11+
result::ok(ty::re_static)
1212
}
1313
fn named_region(id: ast::ident) -> result<ty::region, ~str> {
1414
if *id == ~"static" { result::ok(ty::re_static) }

src/test/run-pass/const-rec-and-tup.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const x : (int,int) = (0xfeedf00dd,0xca11ab1e);
2-
const y : { x: (int, int),
1+
const x : (i32,i32) = (0xfeedf00dd,0xca11ab1e);
2+
const y : { x: (i64, i64),
33
y: { a: float,
44
b: float } } = { x: (0xf0f0f0f0_f0f0f0f0,
55
0xabababab_abababab),
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
const x: &int = &10;
3+
4+
const y: &{a: int, b: &int} = &{a: 15, b: x};
5+
6+
fn main() {
7+
io::println(fmt!("x = %?", x));
8+
io::println(fmt!("y = {a: %?, b: %?}", y.a, *(y.b)));
9+
}

0 commit comments

Comments
 (0)