diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index b1b073c3ecb..3792887709f 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -29,15 +29,14 @@ pub struct CheckCrateVisitor { impl Visitor for CheckCrateVisitor { fn visit_item(&mut self, i: &Item, env: bool) { - check_item(self, self.sess, self.def_map, self.method_map, - self.tcx, i, env) + check_item(self, self.sess, self.def_map, i, env); } fn visit_pat(&mut self, p: &Pat, env: bool) { check_pat(self, p, env); } fn visit_expr(&mut self, ex: &Expr, env: bool) { check_expr(self, self.sess, self.def_map, self.method_map, - self.tcx, ex, env, false); + self.tcx, ex, env); } } @@ -59,13 +58,11 @@ pub fn check_crate(sess: Session, pub fn check_item(v: &mut CheckCrateVisitor, sess: Session, def_map: resolve::DefMap, - method_map: typeck::method_map, - tcx: ty::ctxt, it: &Item, _is_const: bool) { match it.node { - ItemStatic(_, mut_, ex) => { - check_expr(v, sess, def_map, method_map, tcx, ex, true, mut_ == MutMutable); + ItemStatic(_, _, ex) => { + v.visit_expr(ex, true); check_item_recursion(sess, &v.tcx.map, def_map, it); } ItemEnum(ref enum_definition, _) => { @@ -108,8 +105,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor, method_map: typeck::MethodMap, tcx: ty::ctxt, e: &Expr, - is_const: bool, - is_static_mut: bool) { + is_const: bool) { if is_const { match e.node { ExprUnary(UnDeref, _) => { } @@ -177,6 +173,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor, } } } + ExprVstore(_, ExprVstoreMutSlice) | ExprVstore(_, ExprVstoreSlice) | ExprVec(_, MutImmutable) | ExprAddrOf(MutImmutable, _) | @@ -191,11 +188,6 @@ pub fn check_expr(v: &mut CheckCrateVisitor, e.span, "references in constants may only refer to \ immutable values"); - } - ExprVstore(_, ExprVstoreMutSlice) => { - if !is_static_mut { - sess.span_err(e.span, "mutable slice is not allowed in immutable constants") - } }, ExprVstore(_, ExprVstoreUniq) => { sess.span_err(e.span, "cannot allocate vectors in constant expressions") diff --git a/src/librustc/middle/check_static.rs b/src/librustc/middle/check_static.rs index ebf1904d3cb..62f9f512b5d 100644 --- a/src/librustc/middle/check_static.rs +++ b/src/librustc/middle/check_static.rs @@ -108,6 +108,10 @@ impl Visitor for CheckStaticVisitor { ast::ExprVstore(_, ast::ExprVstoreSlice) => { visit::walk_expr(self, e, is_const); } + ast::ExprVstore(_, ast::ExprVstoreMutSlice) => { + self.tcx.sess.span_err(e.span, + "static items are not allowed to have mutable slices"); + }, ast::ExprUnary(ast::UnBox, _) => { self.tcx.sess.span_err(e.span, "static items are not allowed to have managed pointers"); diff --git a/src/librustc/middle/trans/consts.rs b/src/librustc/middle/trans/consts.rs index d85da2ae3ef..5b011939c61 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -574,7 +574,7 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr, (v, inlineable) } ast::ExprVstore(sub, store @ ast::ExprVstoreSlice) | - ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => { + ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => { match sub.node { ast::ExprLit(ref lit) => { match lit.node { diff --git a/src/test/compile-fail/issue-11411.rs b/src/test/compile-fail/check-static-immutable-mut-slices.rs similarity index 71% rename from src/test/compile-fail/issue-11411.rs rename to src/test/compile-fail/check-static-immutable-mut-slices.rs index 4ccee2b40c1..73ce488cbc6 100644 --- a/src/test/compile-fail/issue-11411.rs +++ b/src/test/compile-fail/check-static-immutable-mut-slices.rs @@ -8,6 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -static TEST: &'static mut [int] = &mut []; //~ ERROR mutable slice is not allowed +// Checks that immutable static items can't have mutable slices -fn main() { } +static TEST: &'static mut [int] = &mut []; +//~^ ERROR static items are not allowed to have mutable slices + +pub fn main() { } diff --git a/src/test/run-pass/issue-11411.rs b/src/test/run-pass/check-static-mut-slices.rs similarity index 86% rename from src/test/run-pass/issue-11411.rs rename to src/test/run-pass/check-static-mut-slices.rs index e0da80702d7..af25c43005d 100644 --- a/src/test/run-pass/issue-11411.rs +++ b/src/test/run-pass/check-static-mut-slices.rs @@ -8,11 +8,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// Checks that mutable static items can have mutable slices static mut TEST: &'static mut [int] = &mut [1]; pub fn main() { unsafe { TEST[0] += 1; + assert_eq!(TEST[0], 2); } }