Skip to content

Commit 0a181a8

Browse files
committed
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
2 parents 79203b5 + abfc6db commit 0a181a8

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

src/librustc/middle/borrowck/check_loans.rs

+3
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ impl<'a> CheckLoanCtxt<'a> {
522522
None => {
523523
return true;
524524
}
525+
Some(mc::AliasableStaticMut) => {
526+
return true;
527+
}
525528
Some(cause) => {
526529
this.bccx.report_aliasability_violation(
527530
expr.span,

src/librustc/middle/check_const.rs

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
156156
}
157157
}
158158
}
159+
ExprVstore(_, ExprVstoreMutSlice) |
159160
ExprVstore(_, ExprVstoreSlice) |
160161
ExprVec(_, MutImmutable) |
161162
ExprAddrOf(MutImmutable, _) |

src/librustc/middle/check_static.rs

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ impl<'a> Visitor<bool> for CheckStaticVisitor<'a> {
107107
ast::ExprVstore(_, ast::ExprVstoreSlice) => {
108108
visit::walk_expr(self, e, is_const);
109109
}
110+
ast::ExprVstore(_, ast::ExprVstoreMutSlice) => {
111+
self.tcx.sess.span_err(e.span,
112+
"static items are not allowed to have mutable slices");
113+
},
110114
ast::ExprUnary(ast::UnBox, _) => {
111115
self.tcx.sess.span_err(e.span,
112116
"static items are not allowed to have managed pointers");

src/librustc/middle/trans/consts.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
use back::abi;
13-
use lib::llvm::{llvm, ConstFCmp, ConstICmp, SetLinkage, PrivateLinkage, ValueRef, Bool, True};
13+
use lib::llvm::{llvm, ConstFCmp, ConstICmp, SetLinkage, PrivateLinkage, ValueRef, Bool, True,
14+
False};
1415
use lib::llvm::{IntEQ, IntNE, IntUGT, IntUGE, IntULT, IntULE, IntSGT, IntSGE, IntSLT, IntSLE,
1516
RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE};
1617

@@ -574,7 +575,8 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
574575
is_local);
575576
(v, inlineable)
576577
}
577-
ast::ExprVstore(sub, ast::ExprVstoreSlice) => {
578+
ast::ExprVstore(sub, store @ ast::ExprVstoreSlice) |
579+
ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => {
578580
match sub.node {
579581
ast::ExprLit(ref lit) => {
580582
match lit.node {
@@ -592,7 +594,8 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
592594
llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name)
593595
});
594596
llvm::LLVMSetInitializer(gv, cv);
595-
llvm::LLVMSetGlobalConstant(gv, True);
597+
llvm::LLVMSetGlobalConstant(gv,
598+
if store == ast::ExprVstoreMutSlice { False } else { True });
596599
SetLinkage(gv, PrivateLinkage);
597600
let p = const_ptrcast(cx, gv, llunitty);
598601
(C_struct(cx, [p, C_uint(cx, es.len())], false), false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Checks that immutable static items can't have mutable slices
12+
13+
static TEST: &'static mut [int] = &mut [];
14+
//~^ ERROR static items are not allowed to have mutable slices
15+
16+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Checks that mutable static items can have mutable slices
12+
13+
static mut TEST: &'static mut [int] = &mut [1];
14+
15+
pub fn main() {
16+
unsafe {
17+
TEST[0] += 1;
18+
assert_eq!(TEST[0], 2);
19+
}
20+
}

0 commit comments

Comments
 (0)