about summary refs log tree commit diff
path: root/src/test/compile-fail
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-12-05 17:01:33 -0800
committerNiko Matsakis <niko@alum.mit.edu>2014-12-08 13:47:44 -0500
commit096a28607fb80c91e6e2ca64d9ef44c4e550e96c (patch)
tree82c4ee8f20df133305959d507ec76adb4db5e324 /src/test/compile-fail
parentc7a9b49d1b5d4e520f25355f26a93dfac4ffa146 (diff)
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.

A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.

For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.

This breaks code like:

    #[deriving(Show)]
    struct Point2D {
        x: int,
        y: int,
    }

    fn main() {
        let mypoint = Point2D {
            x: 1,
            y: 1,
        };
        let otherpoint = mypoint;
        println!("{}{}", mypoint, otherpoint);
    }

Change this code to:

    #[deriving(Show)]
    struct Point2D {
        x: int,
        y: int,
    }

    impl Copy for Point2D {}

    fn main() {
        let mypoint = Point2D {
            x: 1,
            y: 1,
        };
        let otherpoint = mypoint;
        println!("{}{}", mypoint, otherpoint);
    }

This is the backwards-incompatible part of #13231.

Part of RFC #3.

[breaking-change]
Diffstat (limited to 'src/test/compile-fail')
-rw-r--r--src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs4
-rw-r--r--src/test/compile-fail/borrowck-borrow-from-stack-variable.rs4
-rw-r--r--src/test/compile-fail/borrowck-loan-local-as-both-mut-and-imm.rs35
-rw-r--r--src/test/compile-fail/borrowck-use-mut-borrow.rs3
-rw-r--r--src/test/compile-fail/dst-index.rs7
-rw-r--r--src/test/compile-fail/dst-rvalue.rs2
-rw-r--r--src/test/compile-fail/issue-17651.rs3
-rw-r--r--src/test/compile-fail/kindck-copy.rs3
-rw-r--r--src/test/compile-fail/lint-dead-code-1.rs1
-rw-r--r--src/test/compile-fail/lint-missing-doc.rs1
-rw-r--r--src/test/compile-fail/opt-in-copy.rs33
-rw-r--r--src/test/compile-fail/stage0-clone-contravariant-lifetime.rs43
-rw-r--r--src/test/compile-fail/stage0-cmp.rs39
13 files changed, 59 insertions, 119 deletions
diff --git a/src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs b/src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs
index c071691c947..d5998c8ca99 100644
--- a/src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs
+++ b/src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs
@@ -14,11 +14,15 @@ struct Foo {
   bar2: Bar
 }
 
+impl Copy for Foo {}
+
 struct Bar {
   int1: int,
   int2: int,
 }
 
+impl Copy for Bar {}
+
 fn make_foo() -> Box<Foo> { panic!() }
 
 fn borrow_same_field_twice_mut_mut() {
diff --git a/src/test/compile-fail/borrowck-borrow-from-stack-variable.rs b/src/test/compile-fail/borrowck-borrow-from-stack-variable.rs
index 3a85b45ad12..d252d442297 100644
--- a/src/test/compile-fail/borrowck-borrow-from-stack-variable.rs
+++ b/src/test/compile-fail/borrowck-borrow-from-stack-variable.rs
@@ -13,11 +13,15 @@ struct Foo {
   bar2: Bar
 }
 
+impl Copy for Foo {}
+
 struct Bar {
   int1: int,
   int2: int,
 }
 
+impl Copy for Bar {}
+
 fn make_foo() -> Foo { panic!() }
 
 fn borrow_same_field_twice_mut_mut() {
diff --git a/src/test/compile-fail/borrowck-loan-local-as-both-mut-and-imm.rs b/src/test/compile-fail/borrowck-loan-local-as-both-mut-and-imm.rs
deleted file mode 100644
index 2063d7388a9..00000000000
--- a/src/test/compile-fail/borrowck-loan-local-as-both-mut-and-imm.rs
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2012 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.
-
-enum Either<T, U> { Left(T), Right(U) }
-
-    fn f(x: &mut Either<int,f64>, y: &Either<int,f64>) -> int {
-        match *y {
-            Either::Left(ref z) => {
-                *x = Either::Right(1.0);
-                *z
-            }
-            _ => panic!()
-        }
-    }
-
-    fn g() {
-        let mut x: Either<int,f64> = Either::Left(3);
-        println!("{}", f(&mut x, &x)); //~ ERROR cannot borrow
-    }
-
-    fn h() {
-        let mut x: Either<int,f64> = Either::Left(3);
-        let y: &Either<int, f64> = &x;
-        let z: &mut Either<int, f64> = &mut x; //~ ERROR cannot borrow
-        *z = *y;
-    }
-
-    fn main() {}
diff --git a/src/test/compile-fail/borrowck-use-mut-borrow.rs b/src/test/compile-fail/borrowck-use-mut-borrow.rs
index 7414bb930d4..0d27473cb2d 100644
--- a/src/test/compile-fail/borrowck-use-mut-borrow.rs
+++ b/src/test/compile-fail/borrowck-use-mut-borrow.rs
@@ -9,6 +9,9 @@
 // except according to those terms.
 
 struct A { a: int, b: int }
+
+impl Copy for A {}
+
 struct B { a: int, b: Box<int> }
 
 fn var_copy_after_var_borrow() {
diff --git a/src/test/compile-fail/dst-index.rs b/src/test/compile-fail/dst-index.rs
index f6511d68662..af97c864dc8 100644
--- a/src/test/compile-fail/dst-index.rs
+++ b/src/test/compile-fail/dst-index.rs
@@ -16,6 +16,8 @@ use std::fmt::Show;
 
 struct S;
 
+impl Copy for S {}
+
 impl Index<uint, str> for S {
     fn index<'a>(&'a self, _: &uint) -> &'a str {
         "hello"
@@ -24,6 +26,8 @@ impl Index<uint, str> for S {
 
 struct T;
 
+impl Copy for T {}
+
 impl Index<uint, Show + 'static> for T {
     fn index<'a>(&'a self, idx: &uint) -> &'a (Show + 'static) {
         static x: uint = 42;
@@ -33,7 +37,8 @@ impl Index<uint, Show + 'static> for T {
 
 fn main() {
     S[0];
-    //~^ ERROR E0161
+    //~^ ERROR cannot move out of dereference
+    //~^^ ERROR E0161
     T[0];
     //~^ ERROR cannot move out of dereference
     //~^^ ERROR E0161
diff --git a/src/test/compile-fail/dst-rvalue.rs b/src/test/compile-fail/dst-rvalue.rs
index 52b7ea9efa5..4c1dafd8c1a 100644
--- a/src/test/compile-fail/dst-rvalue.rs
+++ b/src/test/compile-fail/dst-rvalue.rs
@@ -13,8 +13,10 @@
 pub fn main() {
     let _x: Box<str> = box *"hello world";
     //~^ ERROR E0161
+    //~^^ ERROR cannot move out of dereference
 
     let array: &[int] = &[1, 2, 3];
     let _x: Box<[int]> = box *array;
     //~^ ERROR E0161
+    //~^^ ERROR cannot move out of dereference
 }
diff --git a/src/test/compile-fail/issue-17651.rs b/src/test/compile-fail/issue-17651.rs
index ef8174a26aa..ab396edddf4 100644
--- a/src/test/compile-fail/issue-17651.rs
+++ b/src/test/compile-fail/issue-17651.rs
@@ -13,5 +13,6 @@
 
 fn main() {
     (|| box *[0u].as_slice())();
-    //~^ ERROR cannot move a value of type [uint]
+    //~^ ERROR cannot move out of dereference
+    //~^^ ERROR cannot move a value of type [uint]
 }
diff --git a/src/test/compile-fail/kindck-copy.rs b/src/test/compile-fail/kindck-copy.rs
index f0c4a4243ac..8868c7f8256 100644
--- a/src/test/compile-fail/kindck-copy.rs
+++ b/src/test/compile-fail/kindck-copy.rs
@@ -14,6 +14,7 @@
 use std::rc::Rc;
 
 fn assert_copy<T:Copy>() { }
+
 trait Dummy { }
 
 struct MyStruct {
@@ -21,6 +22,8 @@ struct MyStruct {
     y: int,
 }
 
+impl Copy for MyStruct {}
+
 struct MyNoncopyStruct {
     x: Box<char>,
 }
diff --git a/src/test/compile-fail/lint-dead-code-1.rs b/src/test/compile-fail/lint-dead-code-1.rs
index 1a4a87e608b..9e5f15c2721 100644
--- a/src/test/compile-fail/lint-dead-code-1.rs
+++ b/src/test/compile-fail/lint-dead-code-1.rs
@@ -12,6 +12,7 @@
 #![allow(unused_variables)]
 #![allow(non_camel_case_types)]
 #![allow(non_upper_case_globals)]
+#![allow(missing_copy_implementations)]
 #![deny(dead_code)]
 
 #![crate_type="lib"]
diff --git a/src/test/compile-fail/lint-missing-doc.rs b/src/test/compile-fail/lint-missing-doc.rs
index 8d4ecde692d..b73c3fa2610 100644
--- a/src/test/compile-fail/lint-missing-doc.rs
+++ b/src/test/compile-fail/lint-missing-doc.rs
@@ -13,6 +13,7 @@
 #![feature(globs)]
 #![deny(missing_docs)]
 #![allow(dead_code)]
+#![allow(missing_copy_implementations)]
 
 //! Some garbage docs for the crate here
 #![doc="More garbage"]
diff --git a/src/test/compile-fail/opt-in-copy.rs b/src/test/compile-fail/opt-in-copy.rs
new file mode 100644
index 00000000000..56f71c844ac
--- /dev/null
+++ b/src/test/compile-fail/opt-in-copy.rs
@@ -0,0 +1,33 @@
+// 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.
+
+struct CantCopyThis;
+
+struct IWantToCopyThis {
+    but_i_cant: CantCopyThis,
+}
+
+impl Copy for IWantToCopyThis {}
+//~^ ERROR the trait `Copy` may not be implemented for this type
+
+enum CantCopyThisEither {
+    A,
+    B,
+}
+
+enum IWantToCopyThisToo {
+    ButICant(CantCopyThisEither),
+}
+
+impl Copy for IWantToCopyThisToo {}
+//~^ ERROR the trait `Copy` may not be implemented for this type
+
+fn main() {}
+
diff --git a/src/test/compile-fail/stage0-clone-contravariant-lifetime.rs b/src/test/compile-fail/stage0-clone-contravariant-lifetime.rs
deleted file mode 100644
index 1d1b244ab5a..00000000000
--- a/src/test/compile-fail/stage0-clone-contravariant-lifetime.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-// 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.
-
-// A zero-dependency test that covers some basic traits, default
-// methods, etc.  When mucking about with basic type system stuff I
-// often encounter problems in the iterator trait, so it's useful to
-// have hanging around. -nmatsakis
-
-// error-pattern: requires `start` lang_item
-
-#![no_std]
-#![feature(lang_items)]
-
-#[lang = "sized"]
-pub trait Sized for Sized? {
-    // Empty.
-}
-
-pub mod std {
-    pub mod clone {
-        pub trait Clone {
-            fn clone(&self) -> Self;
-        }
-    }
-}
-
-pub struct ContravariantLifetime<'a>;
-
-impl <'a> ::std::clone::Clone for ContravariantLifetime<'a> {
-    #[inline]
-    fn clone(&self) -> ContravariantLifetime<'a> {
-        match *self { ContravariantLifetime => ContravariantLifetime, }
-    }
-}
-
-fn main() { }
diff --git a/src/test/compile-fail/stage0-cmp.rs b/src/test/compile-fail/stage0-cmp.rs
deleted file mode 100644
index f68eb6400fa..00000000000
--- a/src/test/compile-fail/stage0-cmp.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-// 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.
-
-
-// A zero-dependency test that covers some basic traits, default
-// methods, etc.  When mucking about with basic type system stuff I
-// often encounter problems in the iterator trait, so it's useful to
-// have hanging around. -nmatsakis
-
-// error-pattern: requires `start` lang_item
-
-#![no_std]
-#![feature(lang_items)]
-
-#[lang = "sized"]
-pub trait Sized for Sized? {
-    // Empty.
-}
-
-#[unstable = "Definition may change slightly after trait reform"]
-pub trait PartialEq for Sized? {
-    /// This method tests for `self` and `other` values to be equal, and is used by `==`.
-    fn eq(&self, other: &Self) -> bool;
-}
-
-#[unstable = "Trait is unstable."]
-impl<'a, Sized? T: PartialEq> PartialEq for &'a T {
-    #[inline]
-    fn eq(&self, other: & &'a T) -> bool { PartialEq::eq(*self, *other) }
-}
-
-fn main() { }