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]
This commit is contained in:
Niko Matsakis 2014-12-05 17:01:33 -08:00
parent c7a9b49d1b
commit 096a28607f
277 changed files with 2182 additions and 513 deletions

View file

@ -35,6 +35,8 @@ pub struct ChaChaRng {
index: uint, // Index into state
}
impl Copy for ChaChaRng {}
static EMPTY: ChaChaRng = ChaChaRng {
buffer: [0, ..STATE_WORDS],
state: [0, ..STATE_WORDS],

View file

@ -10,6 +10,7 @@
//! The exponential distribution.
use core::kinds::Copy;
use core::num::Float;
use {Rng, Rand};
@ -31,6 +32,8 @@ use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
/// College, Oxford
pub struct Exp1(pub f64);
impl Copy for Exp1 {}
// This could be done via `-rng.gen::<f64>().ln()` but that is slower.
impl Rand for Exp1 {
#[inline]
@ -71,6 +74,8 @@ pub struct Exp {
lambda_inverse: f64
}
impl Copy for Exp {}
impl Exp {
/// Construct a new `Exp` with the given shape parameter
/// `lambda`. Panics if `lambda <= 0`.

View file

@ -10,6 +10,7 @@
//! The normal and derived distributions.
use core::kinds::Copy;
use core::num::Float;
use {Rng, Rand, Open01};
@ -30,6 +31,8 @@ use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
/// College, Oxford
pub struct StandardNormal(pub f64);
impl Copy for StandardNormal {}
impl Rand for StandardNormal {
fn rand<R:Rng>(rng: &mut R) -> StandardNormal {
#[inline]
@ -88,6 +91,8 @@ pub struct Normal {
std_dev: f64,
}
impl Copy for Normal {}
impl Normal {
/// Construct a new `Normal` distribution with the given mean and
/// standard deviation.
@ -134,6 +139,8 @@ pub struct LogNormal {
norm: Normal
}
impl Copy for LogNormal {}
impl LogNormal {
/// Construct a new `LogNormal` distribution with the given mean
/// and standard deviation.

View file

@ -37,6 +37,9 @@ pub struct IsaacRng {
b: u32,
c: u32
}
impl Copy for IsaacRng {}
static EMPTY: IsaacRng = IsaacRng {
cnt: 0,
rsl: [0, ..RAND_SIZE_UINT],
@ -271,6 +274,8 @@ pub struct Isaac64Rng {
c: u64,
}
impl Copy for Isaac64Rng {}
static EMPTY_64: Isaac64Rng = Isaac64Rng {
cnt: 0,
rsl: [0, .. RAND_SIZE_64],

View file

@ -377,6 +377,7 @@ pub trait SeedableRng<Seed>: Rng {
/// [1]: Marsaglia, George (July 2003). ["Xorshift
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
/// Statistical Software*. Vol. 8 (Issue 14).
#[allow(missing_copy_implementations)]
pub struct XorShiftRng {
x: u32,
y: u32,
@ -384,6 +385,17 @@ pub struct XorShiftRng {
w: u32,
}
impl Clone for XorShiftRng {
fn clone(&self) -> XorShiftRng {
XorShiftRng {
x: self.x,
y: self.y,
z: self.z,
w: self.w,
}
}
}
impl XorShiftRng {
/// Creates a new XorShiftRng instance which is not seeded.
///

View file

@ -135,6 +135,8 @@ pub trait Reseeder<R> {
/// replacing the RNG with the result of a `Default::default` call.
pub struct ReseedWithDefault;
impl Copy for ReseedWithDefault {}
impl<R: Rng + Default> Reseeder<R> for ReseedWithDefault {
fn reseed(&mut self, rng: &mut R) {
*rng = Default::default();