diff --git a/src/librustc/middle/borrowck/gather_loans/gather_moves.rs b/src/librustc/middle/borrowck/gather_loans/gather_moves.rs index d07d6a066b0ea..f447d3f5d5ef8 100644 --- a/src/librustc/middle/borrowck/gather_loans/gather_moves.rs +++ b/src/librustc/middle/borrowck/gather_loans/gather_moves.rs @@ -117,7 +117,7 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt, mc::cat_deref(_, _, mc::BorrowedPtr(..)) | mc::cat_deref(_, _, mc::GcPtr) | mc::cat_deref(_, _, mc::UnsafePtr(..)) | - mc::cat_upvar(..) | mc::cat_static_item | + mc::cat_upvar(..) | mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => { bccx.span_err( cmt0.span, @@ -126,6 +126,19 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt, false } + mc::cat_static_item => { + match cmt.mutbl { + // "Moves" out of static items end-up calling memcpy, so they are allowed. + mc::McImmutable => { + true + } + _ => { + bccx.span_err( cmt0.span, "cannot move out of mutable static items"); + false + } + } + } + // Can move out of captured upvars only if the destination closure // type is 'once'. 1-shot stack closures emit the copied_upvar form // (see mem_categorization.rs). diff --git a/src/test/compile-fail/static-items-cant-move.rs b/src/test/compile-fail/cant-move-out-of-mutable-static.rs similarity index 72% rename from src/test/compile-fail/static-items-cant-move.rs rename to src/test/compile-fail/cant-move-out-of-mutable-static.rs index 28e73f74ff3fc..e6e0b2247762a 100644 --- a/src/test/compile-fail/static-items-cant-move.rs +++ b/src/test/compile-fail/cant-move-out-of-mutable-static.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Verifies that static items can't be moved +// Ensure that moves out of mutable static items is disallowed use std::kinds::marker; @@ -17,13 +17,11 @@ struct Foo { nocopy: marker::NoCopy } -static BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy}; - - -fn test(f: Foo) { - let _f = Foo{foo: 4, ..f}; -} +static mut BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy}; fn main() { - test(BAR); //~ ERROR cannot move out of static item + unsafe { + let _f = BAR; //~ ERROR: cannot move out of mutable static item + } } + diff --git a/src/test/compile-fail/std-uncopyable-atomics.rs b/src/test/compile-fail/std-uncopyable-atomics.rs index cd853a2cf4d78..0b7ddc2b3ff87 100644 --- a/src/test/compile-fail/std-uncopyable-atomics.rs +++ b/src/test/compile-fail/std-uncopyable-atomics.rs @@ -16,11 +16,11 @@ use std::sync::atomics::*; use std::ptr; fn main() { - let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item + let x = INIT_ATOMIC_BOOL; let x = *&x; //~ ERROR: cannot move out of dereference - let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item + let x = INIT_ATOMIC_INT; let x = *&x; //~ ERROR: cannot move out of dereference - let x = INIT_ATOMIC_UINT; //~ ERROR cannot move out of static item + let x = INIT_ATOMIC_UINT; let x = *&x; //~ ERROR: cannot move out of dereference let x: AtomicPtr = AtomicPtr::new(ptr::mut_null()); let x = *&x; //~ ERROR: cannot move out of dereference diff --git a/src/test/run-pass/borrowck-ensure-static-items-moves-copy.rs b/src/test/run-pass/borrowck-ensure-static-items-moves-copy.rs new file mode 100644 index 0000000000000..5ac6c5d564f89 --- /dev/null +++ b/src/test/run-pass/borrowck-ensure-static-items-moves-copy.rs @@ -0,0 +1,37 @@ +// 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. + +// Ensure that moves out of statics don't null the static + +use std::kinds::marker; + +struct Foo { + foo: int, + nocopy: marker::NoCopy +} + +impl Eq for Foo { + fn eq(&self, other: &Foo) -> bool { + self.foo == other.foo + } +} + +impl std::fmt::Show for Foo { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f.buf, "Foo({})", self.foo) + } +} + +static BAR: Foo = Foo{foo: 5, nocopy: marker::NoCopy}; + +fn main() { + let x = BAR; + assert_eq!(x, BAR); +} diff --git a/src/test/compile-fail/borrowck-move-out-of-static-item.rs b/src/test/run-pass/borrowck-move-out-of-static-item.rs similarity index 85% rename from src/test/compile-fail/borrowck-move-out-of-static-item.rs rename to src/test/run-pass/borrowck-move-out-of-static-item.rs index ea36c76ea4058..62bbdfb06b1f3 100644 --- a/src/test/compile-fail/borrowck-move-out-of-static-item.rs +++ b/src/test/run-pass/borrowck-move-out-of-static-item.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Ensure that moves out of static items is forbidden +// Ensure that moves out of immutable static items is allowed use std::kinds::marker; @@ -25,5 +25,5 @@ fn test(f: Foo) { } fn main() { - test(BAR); //~ ERROR cannot move out of static item + test(BAR); }