auto merge of #12742 : FlaPer87/rust/issue-11411-static-mut-slice, r=nikomatsakis

This PR enables the use of mutable slices in *mutable* static items. The work was started by @xales and I added a follow-up commit that moves the *immutable* restriction to the recently added `check_static`

Closes #11411
This commit is contained in:
bors 2014-03-17 09:57:06 -07:00
commit 0a181a8917
6 changed files with 50 additions and 3 deletions

View file

@ -522,6 +522,9 @@ impl<'a> CheckLoanCtxt<'a> {
None => {
return true;
}
Some(mc::AliasableStaticMut) => {
return true;
}
Some(cause) => {
this.bccx.report_aliasability_violation(
expr.span,

View file

@ -156,6 +156,7 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
}
}
}
ExprVstore(_, ExprVstoreMutSlice) |
ExprVstore(_, ExprVstoreSlice) |
ExprVec(_, MutImmutable) |
ExprAddrOf(MutImmutable, _) |

View file

@ -107,6 +107,10 @@ impl<'a> Visitor<bool> for CheckStaticVisitor<'a> {
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

@ -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};
@ -574,7 +575,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 {
@ -592,7 +594,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(cx, [p, C_uint(cx, es.len())], false), false)

View file

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

View file

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