Skip to content

Commit abfc6db

Browse files
committed
rustc: Move mut slice check to check_static
This is a follow-up patch that moves the mut slice check to the recently added `check_static` Closes #11411
1 parent 8b8d41d commit abfc6db

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

Diff for: src/librustc/middle/check_const.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ pub struct CheckCrateVisitor {
2929

3030
impl Visitor<bool> for CheckCrateVisitor {
3131
fn visit_item(&mut self, i: &Item, env: bool) {
32-
check_item(self, self.sess, self.def_map, self.method_map,
33-
self.tcx, i, env)
32+
check_item(self, self.sess, self.def_map, i, env);
3433
}
3534
fn visit_pat(&mut self, p: &Pat, env: bool) {
3635
check_pat(self, p, env);
3736
}
3837
fn visit_expr(&mut self, ex: &Expr, env: bool) {
3938
check_expr(self, self.sess, self.def_map, self.method_map,
40-
self.tcx, ex, env, false);
39+
self.tcx, ex, env);
4140
}
4241
}
4342

@@ -59,13 +58,11 @@ pub fn check_crate(sess: Session,
5958
pub fn check_item(v: &mut CheckCrateVisitor,
6059
sess: Session,
6160
def_map: resolve::DefMap,
62-
method_map: typeck::method_map,
63-
tcx: ty::ctxt,
6461
it: &Item,
6562
_is_const: bool) {
6663
match it.node {
67-
ItemStatic(_, mut_, ex) => {
68-
check_expr(v, sess, def_map, method_map, tcx, ex, true, mut_ == MutMutable);
64+
ItemStatic(_, _, ex) => {
65+
v.visit_expr(ex, true);
6966
check_item_recursion(sess, &v.tcx.map, def_map, it);
7067
}
7168
ItemEnum(ref enum_definition, _) => {
@@ -108,8 +105,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
108105
method_map: typeck::MethodMap,
109106
tcx: ty::ctxt,
110107
e: &Expr,
111-
is_const: bool,
112-
is_static_mut: bool) {
108+
is_const: bool) {
113109
if is_const {
114110
match e.node {
115111
ExprUnary(UnDeref, _) => { }
@@ -177,6 +173,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
177173
}
178174
}
179175
}
176+
ExprVstore(_, ExprVstoreMutSlice) |
180177
ExprVstore(_, ExprVstoreSlice) |
181178
ExprVec(_, MutImmutable) |
182179
ExprAddrOf(MutImmutable, _) |
@@ -191,11 +188,6 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
191188
e.span,
192189
"references in constants may only refer to \
193190
immutable values");
194-
}
195-
ExprVstore(_, ExprVstoreMutSlice) => {
196-
if !is_static_mut {
197-
sess.span_err(e.span, "mutable slice is not allowed in immutable constants")
198-
}
199191
},
200192
ExprVstore(_, ExprVstoreUniq) => {
201193
sess.span_err(e.span, "cannot allocate vectors in constant expressions")

Diff for: src/librustc/middle/check_static.rs

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ impl Visitor<bool> for CheckStaticVisitor {
108108
ast::ExprVstore(_, ast::ExprVstoreSlice) => {
109109
visit::walk_expr(self, e, is_const);
110110
}
111+
ast::ExprVstore(_, ast::ExprVstoreMutSlice) => {
112+
self.tcx.sess.span_err(e.span,
113+
"static items are not allowed to have mutable slices");
114+
},
111115
ast::ExprUnary(ast::UnBox, _) => {
112116
self.tcx.sess.span_err(e.span,
113117
"static items are not allowed to have managed pointers");

Diff for: src/librustc/middle/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr,
574574
(v, inlineable)
575575
}
576576
ast::ExprVstore(sub, store @ ast::ExprVstoreSlice) |
577-
ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => {
577+
ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => {
578578
match sub.node {
579579
ast::ExprLit(ref lit) => {
580580
match lit.node {

Diff for: src/test/compile-fail/issue-11411.rs renamed to src/test/compile-fail/check-static-immutable-mut-slices.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
static TEST: &'static mut [int] = &mut []; //~ ERROR mutable slice is not allowed
11+
// Checks that immutable static items can't have mutable slices
1212

13-
fn main() { }
13+
static TEST: &'static mut [int] = &mut [];
14+
//~^ ERROR static items are not allowed to have mutable slices
15+
16+
pub fn main() { }

Diff for: src/test/run-pass/issue-11411.rs renamed to src/test/run-pass/check-static-mut-slices.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Checks that mutable static items can have mutable slices
1112

1213
static mut TEST: &'static mut [int] = &mut [1];
1314

1415
pub fn main() {
1516
unsafe {
1617
TEST[0] += 1;
18+
assert_eq!(TEST[0], 2);
1719
}
1820
}

0 commit comments

Comments
 (0)