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
This commit is contained in:
Flavio Percoco 2014-03-06 22:48:52 +01:00
parent 8b8d41d28a
commit abfc6db4c2
5 changed files with 18 additions and 17 deletions

View 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")

View file

@ -108,6 +108,10 @@ impl Visitor<bool> 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");

View 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 {

View file

@ -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() { }

View file

@ -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);
}
}