]> git.lizzy.rs Git - rust.git/commitdiff
Immutable static items should be `Freeze` Fixes #12432
authorFlavio Percoco <flaper87@gmail.com>
Fri, 21 Feb 2014 19:31:50 +0000 (20:31 +0100)
committerFlavio Percoco <flaper87@gmail.com>
Thu, 27 Feb 2014 17:09:35 +0000 (18:09 +0100)
src/librustc/middle/check_static.rs
src/test/compile-fail/check-static-values-constraints.rs
src/test/compile-fail/static-mut-not-constant.rs
src/test/run-pass/const-fn-val.rs
src/test/run-pass/const-vec-of-fns.rs
src/test/run-pass/core-run-destroy.rs

index 86078c4023a137f4a8ffdb98cc98db96460ab2fa..ebf1904d3cb2a5efb8854e2b1ddc98e0d4249aaa 100644 (file)
@@ -118,8 +118,24 @@ fn visit_expr(&mut self, e: &ast::Expr, is_const: bool) {
                 self.tcx.sess.span_err(e.span,
                                    "static items are not allowed to have owned pointers");
             }
+            ast::ExprProc(..) => {
+                self.report_error(e.span,
+                                  Some(~"immutable static items must be `Freeze`"));
+                return;
+            }
+            ast::ExprAddrOf(mutability, _) => {
+                match mutability {
+                    ast::MutMutable => {
+                        self.report_error(e.span,
+                                  Some(~"immutable static items must be `Freeze`"));
+                        return;
+                    }
+                    _ => {}
+                }
+            }
             _ => {
                 let node_ty = ty::node_id_to_type(self.tcx, e.id);
+
                 match ty::get(node_ty).sty {
                     ty::ty_struct(did, _) |
                     ty::ty_enum(did, _) => {
@@ -128,6 +144,11 @@ fn visit_expr(&mut self, e: &ast::Expr, is_const: bool) {
                                      Some(~"static items are not allowed to have destructors"));
                             return;
                         }
+                        if Some(did) == self.tcx.lang_items.no_freeze_bound() {
+                            self.report_error(e.span,
+                                              Some(~"immutable static items must be `Freeze`"));
+                            return;
+                        }
                     }
                     _ => {}
                 }
index 1248f56d4b88fc76fb4d4842e645abadaba63f02..852b06d00a64e765a242fa99d6bb6f0c6ab31647 100644 (file)
@@ -11,6 +11,8 @@
 
 // Verifies all possible restrictions for static items values.
 
+use std::kinds::marker;
+
 struct WithDtor;
 
 impl Drop for WithDtor {
@@ -122,6 +124,30 @@ fn drop(&mut self) {}
 static STATIC19: ~int = box 3;
 //~^ ERROR static items are not allowed to have owned pointers
 
+
+struct StructNoFreeze<'a> {
+    nf: &'a int
+}
+
+enum EnumNoFreeze<'a> {
+    FreezableVariant,
+    NonFreezableVariant(StructNoFreeze<'a>)
+}
+
+static STATIC20: StructNoFreeze<'static> = StructNoFreeze{nf: &'static mut 4};
+//~^ ERROR immutable static items must be `Freeze`
+
+static STATIC21: EnumNoFreeze<'static> = FreezableVariant;
+static STATIC22: EnumNoFreeze<'static> = NonFreezableVariant(StructNoFreeze{nf: &'static mut 4});
+//~^ ERROR immutable static items must be `Freeze`
+
+struct NFMarker {
+    nf: marker::NoFreeze
+}
+
+static STATIC23: NFMarker = NFMarker{nf: marker::NoFreeze};
+//~^ ERROR immutable static items must be `Freeze`
+
 pub fn main() {
     let y = { static x: ~int = ~3; x };
     //~^ ERROR static items are not allowed to have owned pointers
index 69db9fa1b35908d5b29fc8658ffa2fa6a3400a72..90dabb7e3a271d8dc18cc90e7e265eb372b5fe74 100644 (file)
@@ -8,6 +8,6 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-static mut a: ~int = ~3; //~ ERROR: cannot do allocations in constant expressions
+static mut a: ~int = ~3; //~ ERROR: mutable static items are not allowed to have owned pointers
 
 fn main() {}
index 0ad8dfc277cbbd49da6b9e4c41ae7c665a35f70d..18638ab68776ee545f40177a2dd59988d760ad79 100644 (file)
@@ -14,8 +14,8 @@ fn foo() -> int {
 
 struct Bar<'a> { f: 'a || -> int }
 
-static b : Bar<'static> = Bar { f: foo };
+static mut b : Bar<'static> = Bar { f: foo };
 
 pub fn main() {
-    assert_eq!((b.f)(), 0xca7f000d);
+    unsafe { assert_eq!((b.f)(), 0xca7f000d); }
 }
index 6d193ec400ba73925862bc3867481e080662d8fc..9a93fcb5f89121d4c4d832add8af3e1169f8b7bc 100644 (file)
 fn f() { }
 static bare_fns: &'static [fn()] = &[f, f];
 struct S<'a>('a ||);
-static closures: &'static [S<'static>] = &[S(f), S(f)];
+static mut closures: &'static [S<'static>] = &[S(f), S(f)];
 
 pub fn main() {
-    for &bare_fn in bare_fns.iter() { bare_fn() }
-    for closure in closures.iter() {
-        let S(ref closure) = *closure;
-        (*closure)()
+    unsafe {
+        for &bare_fn in bare_fns.iter() { bare_fn() }
+        for closure in closures.iter() {
+            let S(ref closure) = *closure;
+            (*closure)()
+        }
     }
 }
index 60f98b1075a47d6e9a528e3df94660a33e1f5ec5..db7b2803c71163913f6d7a7fbceae0ab6a4765dc 100644 (file)
 #[test]
 fn test_destroy_once() {
     #[cfg(not(target_os="android"))]
-    static PROG: &'static str = "echo";
+    static mut PROG: &'static str = "echo";
+
     #[cfg(target_os="android")]
-    static PROG: &'static str = "ls"; // android don't have echo binary
+    static mut PROG: &'static str = "ls"; // android don't have echo binary
 
-    let mut p = Process::new(PROG, []).unwrap();
+    let mut p = unsafe {Process::new(PROG, []).unwrap()};
     p.signal_exit().unwrap(); // this shouldn't crash (and nor should the destructor)
 }
 
 #[test]
 fn test_destroy_twice() {
     #[cfg(not(target_os="android"))]
-    static PROG: &'static str = "echo";
+    static mut PROG: &'static str = "echo";
     #[cfg(target_os="android")]
-    static PROG: &'static str = "ls"; // android don't have echo binary
+    static mut PROG: &'static str = "ls"; // android don't have echo binary
 
-    let mut p = match Process::new(PROG, []) {
+    let mut p = match unsafe{Process::new(PROG, [])} {
         Ok(p) => p,
         Err(e) => fail!("wut: {}", e),
     };
@@ -49,13 +50,13 @@ fn test_destroy_twice() {
 fn test_destroy_actually_kills(force: bool) {
 
     #[cfg(unix,not(target_os="android"))]
-    static BLOCK_COMMAND: &'static str = "cat";
+    static mut BLOCK_COMMAND: &'static str = "cat";
 
     #[cfg(unix,target_os="android")]
-    static BLOCK_COMMAND: &'static str = "/system/bin/cat";
+    static mut BLOCK_COMMAND: &'static str = "/system/bin/cat";
 
     #[cfg(windows)]
-    static BLOCK_COMMAND: &'static str = "cmd";
+    static mut BLOCK_COMMAND: &'static str = "cmd";
 
     #[cfg(unix,not(target_os="android"))]
     fn process_exists(pid: libc::pid_t) -> bool {
@@ -91,7 +92,7 @@ fn process_exists(pid: libc::pid_t) -> bool {
     }
 
     // this process will stay alive indefinitely trying to read from stdin
-    let mut p = Process::new(BLOCK_COMMAND, []).unwrap();
+    let mut p = unsafe {Process::new(BLOCK_COMMAND, []).unwrap()};
 
     assert!(process_exists(p.id()));