diff --git a/src/librustc/middle/borrowck/check_loans.rs b/src/librustc/middle/borrowck/check_loans.rs index 3a500a82664dd..c30ac8c56ed13 100644 --- a/src/librustc/middle/borrowck/check_loans.rs +++ b/src/librustc/middle/borrowck/check_loans.rs @@ -520,6 +520,9 @@ impl<'a> CheckLoanCtxt<'a> { None => { return true; } + Some(mc::AliasableStaticMut) => { + return true; + } Some(cause) => { this.bccx.report_aliasability_violation( expr.span, diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index 3b8e708676235..3792887709f74 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -173,6 +173,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor, } } } + ExprVstore(_, ExprVstoreMutSlice) | ExprVstore(_, ExprVstoreSlice) | ExprVec(_, MutImmutable) | ExprAddrOf(MutImmutable, _) | diff --git a/src/librustc/middle/check_static.rs b/src/librustc/middle/check_static.rs index ebf1904d3cb2a..62f9f512b5dd6 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 55d44e00bae46..5b011939c610c 100644 --- a/src/librustc/middle/trans/consts.rs +++ b/src/librustc/middle/trans/consts.rs @@ -10,7 +10,8 @@ use back::abi; -use lib::llvm::{llvm, ConstFCmp, ConstICmp, SetLinkage, PrivateLinkage, ValueRef, Bool, True}; +use lib::llvm::{llvm, ConstFCmp, ConstICmp, SetLinkage, PrivateLinkage, ValueRef, Bool, True, + False}; use lib::llvm::{IntEQ, IntNE, IntUGT, IntUGE, IntULT, IntULE, IntSGT, IntSGE, IntSLT, IntSLE, RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE}; @@ -572,7 +573,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr, is_local); (v, inlineable) } - ast::ExprVstore(sub, ast::ExprVstoreSlice) => { + ast::ExprVstore(sub, store @ ast::ExprVstoreSlice) | + ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => { match sub.node { ast::ExprLit(ref lit) => { match lit.node { @@ -590,7 +592,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr, llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name) }); llvm::LLVMSetInitializer(gv, cv); - llvm::LLVMSetGlobalConstant(gv, True); + llvm::LLVMSetGlobalConstant(gv, + if store == ast::ExprVstoreMutSlice { False } else { True }); SetLinkage(gv, PrivateLinkage); let p = const_ptrcast(cx, gv, llunitty); (C_struct([p, C_uint(cx, es.len())], false), false) 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 0000000000000..73ce488cbc669 --- /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/run-pass/check-static-mut-slices.rs b/src/test/run-pass/check-static-mut-slices.rs new file mode 100644 index 0000000000000..af25c43005dd4 --- /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); + } +}