]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Move mut slice check to `check_static`
authorFlavio Percoco <flaper87@gmail.com>
Thu, 6 Mar 2014 21:48:52 +0000 (22:48 +0100)
committerFlavio Percoco <flaper87@gmail.com>
Thu, 6 Mar 2014 21:50:39 +0000 (22:50 +0100)
This is a follow-up patch that moves the mut slice check to the recently
added `check_static`

Closes #11411

src/librustc/middle/check_const.rs
src/librustc/middle/check_static.rs
src/librustc/middle/trans/consts.rs
src/test/compile-fail/check-static-immutable-mut-slices.rs [new file with mode: 0644]
src/test/compile-fail/issue-11411.rs [deleted file]
src/test/run-pass/check-static-mut-slices.rs [new file with mode: 0644]
src/test/run-pass/issue-11411.rs [deleted file]

index b1b073c3ecb8b0fd978e93a3266d845167f1e310..3792887709f74cba4300b5a5e4ce69b6bdc6ef38 100644 (file)
@@ -29,15 +29,14 @@ pub struct CheckCrateVisitor {
 
 impl Visitor<bool> 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")
index ebf1904d3cb2a5efb8854e2b1ddc98e0d4249aaa..62f9f512b5dd6d7e0507b43591d94e14fa2e895e 100644 (file)
@@ -108,6 +108,10 @@ fn visit_expr(&mut self, e: &ast::Expr, is_const: bool) {
             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");
index d85da2ae3ef8178e73a74bd7a5c045bc64f82e90..5b011939c610cc106bc2b26b05ecaa1c7254a112 100644 (file)
@@ -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/check-static-immutable-mut-slices.rs b/src/test/compile-fail/check-static-immutable-mut-slices.rs
new file mode 100644 (file)
index 0000000..73ce488
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Checks that immutable static items can't have mutable slices
+
+static TEST: &'static mut [int] = &mut [];
+//~^ ERROR static items are not allowed to have mutable slices
+
+pub fn main() { }
diff --git a/src/test/compile-fail/issue-11411.rs b/src/test/compile-fail/issue-11411.rs
deleted file mode 100644 (file)
index 4ccee2b..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// 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
-
-fn main() { }
diff --git a/src/test/run-pass/check-static-mut-slices.rs b/src/test/run-pass/check-static-mut-slices.rs
new file mode 100644 (file)
index 0000000..af25c43
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// 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);
+    }
+}
diff --git a/src/test/run-pass/issue-11411.rs b/src/test/run-pass/issue-11411.rs
deleted file mode 100644 (file)
index e0da807..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-
-static mut TEST: &'static mut [int] = &mut [1];
-
-pub fn main() {
-    unsafe {
-        TEST[0] += 1;
-    }
-}