提交 8b8d41d2 编写于 作者: X xales 提交者: Flavio Percoco

Allow mutable slices in statics.

Fixes #11411
上级 1eb3f63d
...@@ -520,6 +520,9 @@ fn check_for_aliasability_violation(this: &CheckLoanCtxt, ...@@ -520,6 +520,9 @@ fn check_for_aliasability_violation(this: &CheckLoanCtxt,
None => { None => {
return true; return true;
} }
Some(mc::AliasableStaticMut) => {
return true;
}
Some(cause) => { Some(cause) => {
this.bccx.report_aliasability_violation( this.bccx.report_aliasability_violation(
expr.span, expr.span,
......
...@@ -29,14 +29,15 @@ pub struct CheckCrateVisitor { ...@@ -29,14 +29,15 @@ pub struct CheckCrateVisitor {
impl Visitor<bool> for CheckCrateVisitor { impl Visitor<bool> for CheckCrateVisitor {
fn visit_item(&mut self, i: &Item, env: bool) { 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) { fn visit_pat(&mut self, p: &Pat, env: bool) {
check_pat(self, p, env); check_pat(self, p, env);
} }
fn visit_expr(&mut self, ex: &Expr, env: bool) { fn visit_expr(&mut self, ex: &Expr, env: bool) {
check_expr(self, self.sess, self.def_map, self.method_map, check_expr(self, self.sess, self.def_map, self.method_map,
self.tcx, ex, env); self.tcx, ex, env, false);
} }
} }
...@@ -58,11 +59,13 @@ pub fn check_crate(sess: Session, ...@@ -58,11 +59,13 @@ pub fn check_crate(sess: Session,
pub fn check_item(v: &mut CheckCrateVisitor, pub fn check_item(v: &mut CheckCrateVisitor,
sess: Session, sess: Session,
def_map: resolve::DefMap, def_map: resolve::DefMap,
method_map: typeck::method_map,
tcx: ty::ctxt,
it: &Item, it: &Item,
_is_const: bool) { _is_const: bool) {
match it.node { match it.node {
ItemStatic(_, _, ex) => { ItemStatic(_, mut_, ex) => {
v.visit_expr(ex, true); check_expr(v, sess, def_map, method_map, tcx, ex, true, mut_ == MutMutable);
check_item_recursion(sess, &v.tcx.map, def_map, it); check_item_recursion(sess, &v.tcx.map, def_map, it);
} }
ItemEnum(ref enum_definition, _) => { ItemEnum(ref enum_definition, _) => {
...@@ -105,7 +108,8 @@ pub fn check_expr(v: &mut CheckCrateVisitor, ...@@ -105,7 +108,8 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
method_map: typeck::MethodMap, method_map: typeck::MethodMap,
tcx: ty::ctxt, tcx: ty::ctxt,
e: &Expr, e: &Expr,
is_const: bool) { is_const: bool,
is_static_mut: bool) {
if is_const { if is_const {
match e.node { match e.node {
ExprUnary(UnDeref, _) => { } ExprUnary(UnDeref, _) => { }
...@@ -187,6 +191,11 @@ pub fn check_expr(v: &mut CheckCrateVisitor, ...@@ -187,6 +191,11 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
e.span, e.span,
"references in constants may only refer to \ "references in constants may only refer to \
immutable values"); immutable values");
}
ExprVstore(_, ExprVstoreMutSlice) => {
if !is_static_mut {
sess.span_err(e.span, "mutable slice is not allowed in immutable constants")
}
}, },
ExprVstore(_, ExprVstoreUniq) => { ExprVstore(_, ExprVstoreUniq) => {
sess.span_err(e.span, "cannot allocate vectors in constant expressions") sess.span_err(e.span, "cannot allocate vectors in constant expressions")
......
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
use back::abi; 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, use lib::llvm::{IntEQ, IntNE, IntUGT, IntUGE, IntULT, IntULE, IntSGT, IntSGE, IntSLT, IntSLE,
RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE}; RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE};
...@@ -572,7 +573,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr, ...@@ -572,7 +573,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr,
is_local); is_local);
(v, inlineable) (v, inlineable)
} }
ast::ExprVstore(sub, ast::ExprVstoreSlice) => { ast::ExprVstore(sub, store @ ast::ExprVstoreSlice) |
ast::ExprVstore(sub, store @ ast::ExprVstoreMutSlice) => {
match sub.node { match sub.node {
ast::ExprLit(ref lit) => { ast::ExprLit(ref lit) => {
match lit.node { match lit.node {
...@@ -590,7 +592,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr, ...@@ -590,7 +592,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: &ast::Expr,
llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name) llvm::LLVMAddGlobal(cx.llmod, llty.to_ref(), name)
}); });
llvm::LLVMSetInitializer(gv, cv); llvm::LLVMSetInitializer(gv, cv);
llvm::LLVMSetGlobalConstant(gv, True); llvm::LLVMSetGlobalConstant(gv,
if store == ast::ExprVstoreMutSlice { False } else { True });
SetLinkage(gv, PrivateLinkage); SetLinkage(gv, PrivateLinkage);
let p = const_ptrcast(cx, gv, llunitty); let p = const_ptrcast(cx, gv, llunitty);
(C_struct([p, C_uint(cx, es.len())], false), false) (C_struct([p, C_uint(cx, es.len())], false), false)
......
// 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() { }
// 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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册