]> git.lizzy.rs Git - rust.git/commitdiff
Forbid moves out of static items Closes #10577
authorFlavio Percoco <flaper87@gmail.com>
Tue, 18 Feb 2014 14:19:44 +0000 (15:19 +0100)
committerFlavio Percoco <flaper87@gmail.com>
Thu, 27 Feb 2014 08:24:17 +0000 (09:24 +0100)
src/librustc/middle/borrowck/gather_loans/gather_moves.rs
src/test/compile-fail/borrowck-move-out-of-static-item.rs [new file with mode: 0644]
src/test/compile-fail/static-items-cant-move.rs
src/test/compile-fail/std-uncopyable-atomics.rs
src/test/run-pass/issue-6919.rs

index 49b12a6db1fb5afe8d660f1e3451308094122bc1..7ec9f1f8f47953f986362921c711cc9177fdb6ac 100644 (file)
@@ -104,7 +104,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_upvar(..) | mc::cat_static_item |
         mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
             bccx.span_err(
                 cmt0.span,
@@ -120,19 +120,6 @@ fn check_is_legal_to_move_from(bccx: &BorrowckCtxt,
             true
         }
 
-        // It seems strange to allow a move out of a static item,
-        // but what happens in practice is that you have a
-        // reference to a constant with a type that should be
-        // moved, like `None::<~int>`.  The type of this constant
-        // is technically `Option<~int>`, which moves, but we know
-        // that the content of static items will never actually
-        // contain allocated pointers, so we can just memcpy it.
-        // Since static items can never have allocated memory,
-        // this is ok. For now anyhow.
-        mc::cat_static_item => {
-            true
-        }
-
         mc::cat_rvalue(..) |
         mc::cat_local(..) |
         mc::cat_arg(..) => {
diff --git a/src/test/compile-fail/borrowck-move-out-of-static-item.rs b/src/test/compile-fail/borrowck-move-out-of-static-item.rs
new file mode 100644 (file)
index 0000000..a7e5573
--- /dev/null
@@ -0,0 +1,29 @@
+// 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.
+
+// Ensure that moves out of static items is forbidden
+
+use std::kinds::marker;
+
+struct Foo {
+    foo: int,
+    nopod: marker::NoPod
+}
+
+static BAR: Foo = Foo{foo: 5, nopod: marker::NoPod};
+
+
+fn test(f: Foo) {
+    let _f = Foo{foo: 4, ..f};
+}
+
+fn main() {
+    test(BAR); //~ ERROR cannot move out of static item
+}
index 1bd78d2b1a238ebca5c5b64ef3d122205c257604..f089904dd91497552f99552e89fca3d5b8f64d66 100644 (file)
@@ -25,5 +25,5 @@ fn test(f: Foo) {
 }
 
 fn main() {
-    test(BAR);
+    test(BAR); //~ ERROR cannot move out of static item
 }
index 57c66974fcd01f8f1db9bd48c604d6db283c971b..5c0e7e90aa932eedf90aadff44b7cfe29203cb00 100644 (file)
 use std::ptr;
 
 fn main() {
-    let x = INIT_ATOMIC_FLAG;
+    let x = INIT_ATOMIC_FLAG; //~ ERROR cannot move out of static item
     let x = *&x; //~ ERROR: cannot move out of dereference
-    let x = INIT_ATOMIC_BOOL;
+    let x = INIT_ATOMIC_BOOL; //~ ERROR cannot move out of static item
     let x = *&x; //~ ERROR: cannot move out of dereference
-    let x = INIT_ATOMIC_INT;
+    let x = INIT_ATOMIC_INT; //~ ERROR cannot move out of static item
     let x = *&x; //~ ERROR: cannot move out of dereference
-    let x = INIT_ATOMIC_UINT;
+    let x = INIT_ATOMIC_UINT; //~ ERROR cannot move out of static item
     let x = *&x; //~ ERROR: cannot move out of dereference
     let x: AtomicPtr<uint> = AtomicPtr::new(ptr::mut_null());
     let x = *&x; //~ ERROR: cannot move out of dereference
index 6d84268fb418d2e4f3702dbbf0bfe69373da67ff..5afffb3d029f71cb04f94a66fc1a0ba264b0a7f7 100644 (file)
@@ -15,6 +15,6 @@
 extern crate issue6919_3;
 
 pub fn main() {
-    issue6919_3::D.k;
+    let _ = issue6919_3::D.k;
 }