From abfc6db4c2b9c9c686a5c09d59e4c534e5f4f2ec Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Thu, 6 Mar 2014 22:48:52 +0100 Subject: 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 --- src/librustc/middle/check_const.rs | 20 ++++++-------------- src/librustc/middle/check_static.rs | 4 ++++ src/librustc/middle/trans/consts.rs | 2 +- .../check-static-immutable-mut-slices.rs | 16 ++++++++++++++++ src/test/compile-fail/issue-11411.rs | 13 ------------- src/test/run-pass/check-static-mut-slices.rs | 20 ++++++++++++++++++++ src/test/run-pass/issue-11411.rs | 18 ------------------ 7 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 src/test/compile-fail/check-static-immutable-mut-slices.rs delete mode 100644 src/test/compile-fail/issue-11411.rs create mode 100644 src/test/run-pass/check-static-mut-slices.rs delete mode 100644 src/test/run-pass/issue-11411.rs 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/check-static-immutable-mut-slices.rs b/src/test/compile-fail/check-static-immutable-mut-slices.rs new file mode 100644 index 00000000000..73ce488cbc6 --- /dev/null +++ b/src/test/compile-fail/check-static-immutable-mut-slices.rs @@ -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 or the MIT license +// , 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 index 4ccee2b40c1..00000000000 --- a/src/test/compile-fail/issue-11411.rs +++ /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 or the MIT license -// , 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 index 00000000000..af25c43005d --- /dev/null +++ b/src/test/run-pass/check-static-mut-slices.rs @@ -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 or the MIT license +// , 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 index e0da80702d7..00000000000 --- a/src/test/run-pass/issue-11411.rs +++ /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 or the MIT license -// , 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; - } -} -- cgit 1.4.1-3-g733a5