Skip to content

Allow mutable slices in statics. #11797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 14 additions & 5 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ struct CheckCrateVisitor {

impl Visitor<bool> for CheckCrateVisitor {
fn visit_item(&mut self, i: &Item, env: bool) {
check_item(self, self.sess, self.def_map, i, env);
check_item(self, self.sess, self.def_map, self.method_map,
self.tcx, 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);
self.tcx, ex, env, false);
}
}

Expand All @@ -58,11 +59,13 @@ 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(_, _, ex) => {
v.visit_expr(ex, true);
ItemStatic(_, mut_, ex) => {
check_expr(v, sess, def_map, method_map, tcx, ex, true, mut_ == MutMutable);
check_item_recursion(sess, &v.tcx.map, def_map, it);
}
ItemEnum(ref enum_definition, _) => {
Expand Down Expand Up @@ -105,7 +108,8 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
method_map: typeck::method_map,
tcx: ty::ctxt,
e: &Expr,
is_const: bool) {
is_const: bool,
is_static_mut: bool) {
if is_const {
match e.node {
ExprUnary(_, UnDeref, _) => { }
Expand Down Expand Up @@ -187,6 +191,11 @@ 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")
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -567,7 +568,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr,
let (v, _, inlineable) = const_vec(cx, e, *es, 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 {
Expand All @@ -582,7 +584,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)
Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/issue-11411.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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() { }
18 changes: 18 additions & 0 deletions src/test/run-pass/issue-11411.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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;
}
}