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

@ -25,6 +25,8 @@ mod src {
pub struct A;
impl Copy for A {}
pub fn make() -> B { A }
impl A {

View file

@ -22,6 +22,10 @@ mod private {
p: i32,
}
pub const THREE: P = P { p: 3 };
impl Copy for P {}
}
pub static A: S = S { p: private::THREE };
impl Copy for S {}

View file

@ -22,3 +22,8 @@ extern fn stack_exhausted() {}
#[lang = "eh_personality"]
extern fn eh_personality() {}
#[lang="copy"]
pub trait Copy {}

View file

@ -16,6 +16,8 @@ pub fn get_count() -> u64 { unsafe { COUNT } }
pub struct Foo;
impl Copy for Foo {}
impl Foo {
pub fn foo(self, x: &Foo) {
unsafe { COUNT *= 2; }

View file

@ -16,6 +16,8 @@ pub fn get_count() -> u64 { unsafe { COUNT } }
pub struct Foo;
impl Copy for Foo {}
impl Foo {
pub fn run_trait(self) {
unsafe { COUNT *= 17; }

View file

@ -14,20 +14,31 @@
pub struct Struct;
impl Copy for Struct {}
pub enum Unit {
UnitVariant,
Argument(Struct)
}
impl Copy for Unit {}
pub struct TupleStruct(pub uint, pub &'static str);
impl Copy for TupleStruct {}
// used by the cfail test
pub struct StructWithFields {
foo: int,
}
impl Copy for StructWithFields {}
pub enum EnumWithVariants {
EnumVariant,
EnumVariantArg(int)
}
impl Copy for EnumWithVariants {}

View file

@ -21,6 +21,8 @@ struct Vec2 {
y: f32,
}
impl Copy for Vec2 {}
fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }

View file

@ -53,7 +53,14 @@ fn print_complements() {
}
}
enum Color { Red, Yellow, Blue }
enum Color {
Red,
Yellow,
Blue,
}
impl Copy for Color {}
impl fmt::Show for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
@ -70,6 +77,8 @@ struct CreatureInfo {
color: Color
}
impl Copy for CreatureInfo {}
fn show_color_list(set: Vec<Color>) -> String {
let mut out = String::new();
for col in set.iter() {

View file

@ -67,6 +67,8 @@ struct P {
p: [i32, .. 16],
}
impl Copy for P {}
struct Perm {
cnt: [i32, .. 16],
fact: [u32, .. 16],
@ -75,6 +77,8 @@ struct Perm {
perm: P,
}
impl Copy for Perm {}
impl Perm {
fn new(n: u32) -> Perm {
let mut fact = [1, .. 16];

View file

@ -109,6 +109,8 @@ struct AminoAcid {
p: f32,
}
impl Copy for AminoAcid {}
struct RepeatFasta<'a, W:'a> {
alu: &'static str,
out: &'a mut W

View file

@ -62,6 +62,8 @@ static OCCURRENCES: [&'static str, ..5] = [
#[deriving(PartialEq, PartialOrd, Ord, Eq)]
struct Code(u64);
impl Copy for Code {}
impl Code {
fn hash(&self) -> u64 {
let Code(ret) = *self;

View file

@ -100,6 +100,8 @@ struct Planet {
mass: f64,
}
impl Copy for Planet {}
fn advance(bodies: &mut [Planet, ..N_BODIES], dt: f64, steps: int) {
for _ in range(0, steps) {
let mut b_slice = bodies.as_mut_slice();

View file

@ -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() {

View file

@ -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() {

View file

@ -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() {}

View file

@ -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() {

View file

@ -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

View file

@ -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
}

View file

@ -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]
}

View file

@ -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>,
}

View file

@ -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"]

View file

@ -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"]

View file

@ -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() {}

View file

@ -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() { }

View file

@ -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() { }

View file

@ -104,18 +104,21 @@ use self::AutoDiscriminant::{One, Two, Three};
use self::ManualDiscriminant::{OneHundred, OneThousand, OneMillion};
use self::SingleVariant::TheOnlyVariant;
#[deriving(Copy)]
enum AutoDiscriminant {
One,
Two,
Three
}
#[deriving(Copy)]
enum ManualDiscriminant {
OneHundred = 100,
OneThousand = 1000,
OneMillion = 1000000
}
#[deriving(Copy)]
enum SingleVariant {
TheOnlyVariant
}

View file

@ -147,3 +147,6 @@ fn main() {
}
fn zzz() {()}
impl<T:Copy> Copy for Struct<T> {}

View file

@ -148,3 +148,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for Enum {}

View file

@ -147,3 +147,6 @@ fn main() {
}
fn zzz() {()}
impl<T:Copy> Copy for Struct<T> {}

View file

@ -146,3 +146,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for Struct {}

View file

@ -152,3 +152,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for Struct {}

View file

@ -144,3 +144,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for TupleStruct {}

View file

@ -148,3 +148,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for Struct {}

View file

@ -149,3 +149,6 @@ fn main() {
}
fn zzz() {()}
impl Copy for Struct {}

View file

@ -21,6 +21,8 @@ fn test2() -> int { let val = &0i; { } *val }
struct S { eax: int }
impl Copy for S {}
fn test3() {
let regs = &Cell::new(S {eax: 0});
match true { true => { } _ => { } }

View file

@ -16,6 +16,8 @@ struct Foo {
c: i8
}
impl Copy for Foo {}
#[link(name = "test", kind = "static")]
extern {
fn foo(f: Foo) -> Foo;

View file

@ -11,6 +11,9 @@
#![feature(lang_items)]
#![no_std]
#[lang="copy"]
trait Copy { }
#[lang="sized"]
trait Sized { }

View file

@ -15,6 +15,8 @@ enum newtype {
newvar(int)
}
impl Copy for newtype {}
pub fn main() {
// Test that borrowck treats enums with a single variant

View file

@ -19,10 +19,12 @@ use trait_superkinds_in_metadata::{RequiresCopy};
struct X<T>(T);
impl <T:Sync> RequiresShare for X<T> { }
impl<T:Copy> Copy for X<T> {}
impl <T:Sync+Send> RequiresRequiresShareAndSend for X<T> { }
impl<T:Sync> RequiresShare for X<T> { }
impl <T:Copy> RequiresCopy for X<T> { }
impl<T:Sync+Send> RequiresRequiresShareAndSend for X<T> { }
impl<T:Copy> RequiresCopy for X<T> { }
pub fn main() { }

View file

@ -24,6 +24,8 @@ impl Clone for Foo {
}
}
impl Copy for Foo {}
pub fn main() {
let x = Cell::new(Foo { x: 22 });
let _y = x.get();

View file

@ -14,6 +14,8 @@ use std::cmp;
#[deriving(Show)]
enum cat_type { tuxedo, tabby, tortoiseshell }
impl Copy for cat_type {}
impl cmp::PartialEq for cat_type {
fn eq(&self, other: &cat_type) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -10,6 +10,7 @@
pub fn main() {
enum x { foo }
impl Copy for x {}
impl ::std::cmp::PartialEq for x {
fn eq(&self, other: &x) -> bool {
(*self) as int == (*other) as int

View file

@ -28,6 +28,8 @@ struct MyType {
dummy: uint
}
impl Copy for MyType {}
impl MyTrait for MyType {
fn get(&self) -> MyType { (*self).clone() }
}

View file

@ -12,6 +12,8 @@ enum Foo {
Bar = 0xDEADBEE
}
impl Copy for Foo {}
static X: Foo = Foo::Bar;
pub fn main() {

View file

@ -33,6 +33,8 @@ fn foo2<T:ToBar>(x: &Fat<[T]>) {
#[deriving(PartialEq,Eq)]
struct Bar;
impl Copy for Bar {}
trait ToBar {
fn to_bar(&self) -> Bar;
}

View file

@ -49,6 +49,8 @@ fn foo3(x: &Fat<Fat<[int]>>) {
#[deriving(PartialEq,Eq)]
struct Bar;
impl Copy for Bar {}
trait ToBar {
fn to_bar(&self) -> Bar;
}

View file

@ -17,11 +17,15 @@ struct Fat<Sized? T> {
#[deriving(PartialEq,Eq)]
struct Bar;
impl Copy for Bar {}
#[deriving(PartialEq,Eq)]
struct Bar1 {
f: int
}
impl Copy for Bar1 {}
trait ToBar {
fn to_bar(&self) -> Bar;
fn to_val(&self) -> int;

View file

@ -11,6 +11,8 @@
#[deriving(Show)]
enum chan { chan_t, }
impl Copy for chan {}
impl PartialEq for chan {
fn eq(&self, other: &chan) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -20,6 +20,7 @@ macro_rules! check {
A = 0
}
static C: E = E::V;
impl Copy for E {}
pub fn check() {
assert_eq!(size_of::<E>(), size_of::<$t>());
assert_eq!(E::V as $t, $v as $t);

View file

@ -18,10 +18,14 @@ type EqFn<K> = proc(K, K):'static -> bool;
struct LM { resize_at: uint, size: uint }
impl Copy for LM {}
enum HashMap<K,V> {
HashMap_(LM)
}
impl<K,V> Copy for HashMap<K,V> {}
fn linear_map<K,V>() -> HashMap<K,V> {
HashMap::HashMap_(LM{
resize_at: 32,

View file

@ -15,6 +15,8 @@ mod foo {
// not exported
enum t { t1, t2, }
impl Copy for t {}
impl PartialEq for t {
fn eq(&self, other: &t) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -15,6 +15,8 @@ fn f(arg: &mut A) {
struct A { a: int }
impl Copy for A {}
pub fn main() {
let mut x = A {a: 10};
f(&mut x);

View file

@ -16,6 +16,8 @@
struct I { i: int }
impl Copy for I {}
fn test_rec() {
let rs = if true { I {i: 100} } else { I {i: 101} };
assert_eq!(rs.i, 100);
@ -24,6 +26,8 @@ fn test_rec() {
#[deriving(Show)]
enum mood { happy, sad, }
impl Copy for mood {}
impl PartialEq for mood {
fn eq(&self, other: &mood) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -15,6 +15,8 @@
// Tests for match as expressions resulting in struct types
struct R { i: int }
impl Copy for R {}
fn test_rec() {
let rs = match true { true => R {i: 100}, _ => panic!() };
assert_eq!(rs.i, 100);
@ -23,6 +25,8 @@ fn test_rec() {
#[deriving(Show)]
enum mood { happy, sad, }
impl Copy for mood {}
impl PartialEq for mood {
fn eq(&self, other: &mood) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -13,6 +13,8 @@ use std::cell::Cell;
struct Point {x: int, y: int, z: int}
impl Copy for Point {}
fn f(p: &Cell<Point>) {
assert!((p.get().z == 12));
p.set(Point {x: 10, y: 11, z: 13});

View file

@ -16,6 +16,8 @@ pub struct TwoU16s {
one: u16, two: u16
}
impl Copy for TwoU16s {}
#[link(name = "rust_test_helpers")]
extern {
pub fn rust_dbg_extern_identity_TwoU16s(v: TwoU16s) -> TwoU16s;

View file

@ -16,6 +16,8 @@ pub struct TwoU32s {
one: u32, two: u32
}
impl Copy for TwoU32s {}
#[link(name = "rust_test_helpers")]
extern {
pub fn rust_dbg_extern_identity_TwoU32s(v: TwoU32s) -> TwoU32s;

View file

@ -16,6 +16,8 @@ pub struct TwoU64s {
one: u64, two: u64
}
impl Copy for TwoU64s {}
#[link(name = "rust_test_helpers")]
extern {
pub fn rust_dbg_extern_identity_TwoU64s(v: TwoU64s) -> TwoU64s;

View file

@ -16,6 +16,8 @@ pub struct TwoU8s {
one: u8, two: u8
}
impl Copy for TwoU8s {}
#[link(name = "rust_test_helpers")]
extern {
pub fn rust_dbg_extern_identity_TwoU8s(v: TwoU8s) -> TwoU8s;

View file

@ -14,6 +14,8 @@ pub struct S {
z: u64,
}
impl Copy for S {}
#[link(name = "rust_test_helpers")]
extern {
pub fn get_x(x: S) -> u64;

View file

@ -14,6 +14,8 @@ fn id<T>(x: T) -> T { return x; }
struct Triple {x: int, y: int, z: int}
impl Copy for Triple {}
pub fn main() {
let mut x = 62;
let mut y = 63;

View file

@ -10,6 +10,8 @@
enum Q { R(Option<uint>) }
impl Copy for Q {}
fn xyzzy(q: Q) -> uint {
match q {
Q::R(S) if S.is_some() => { 0 }

View file

@ -10,6 +10,8 @@
struct Pair { x: int, y: int }
impl Copy for Pair {}
pub fn main() {
let a: int =
match 10i { x if x < 7 => { 1i } x if x < 11 => { 2i } 10 => { 3i } _ => { 4i } };

View file

@ -20,6 +20,8 @@ struct XYZ {
z: int
}
impl Copy for XYZ {}
fn main() {
let mut connected = HashSet::new();
let mut border = HashSet::new();

View file

@ -13,6 +13,8 @@ enum Foo {
Baz
}
impl Copy for Foo {}
impl Foo {
fn foo(&self) {
match self {

View file

@ -12,10 +12,13 @@
trait clam<A> {
fn chowder(&self, y: A);
}
struct foo<A> {
x: A,
}
impl<A:Copy> Copy for foo<A> {}
impl<A> clam<A> for foo<A> {
fn chowder(&self, _y: A) {
}

View file

@ -12,6 +12,8 @@ struct cat {
meow: extern "Rust" fn(),
}
impl Copy for cat {}
fn meow() {
println!("meow")
}
@ -24,6 +26,8 @@ fn cat() -> cat {
struct KittyInfo {kitty: cat}
impl Copy for KittyInfo {}
// Code compiles and runs successfully if we add a + before the first arg
fn nyan(kitty: cat, _kitty_info: KittyInfo) {
(kitty.meow)();

View file

@ -13,6 +13,10 @@ enum side { mayo, catsup, vinegar }
enum order { hamburger, fries(side), shake }
enum meal { to_go(order), for_here(order) }
impl Copy for side {}
impl Copy for order {}
impl Copy for meal {}
fn foo(m: Box<meal>, cond: bool) {
match *m {
meal::to_go(_) => { }

View file

@ -29,6 +29,8 @@ struct Point {
y: int,
}
impl Copy for Point {}
// Represents an offset on a canvas. (This has the same structure as a Point.
// but different semantics).
struct Size {
@ -36,11 +38,15 @@ struct Size {
height: int,
}
impl Copy for Size {}
struct Rect {
top_left: Point,
size: Size,
}
impl Copy for Rect {}
// Contains the information needed to do shape rendering via ASCII art.
struct AsciiArt {
width: uint,

View file

@ -13,6 +13,8 @@ struct Vec2 {
y: f64
}
impl Copy for Vec2 {}
// methods we want to export as methods as well as operators
impl Vec2 {
#[inline(always)]

View file

@ -19,11 +19,15 @@ pub struct Point {
y: f64
}
impl Copy for Point {}
pub enum Shape {
Circle(Point, f64),
Rectangle(Point, Point)
}
impl Copy for Shape {}
impl Shape {
pub fn area(&self, sh: Shape) -> f64 {
match sh {

View file

@ -18,7 +18,11 @@ failed to typecheck correctly.
*/
struct X { vec: &'static [int] }
impl Copy for X {}
static V: &'static [X] = &[X { vec: &[1, 2, 3] }];
pub fn main() {
for &v in V.iter() {
println!("{}", v.vec);

View file

@ -13,6 +13,7 @@
// ignore-windows #13361
#![no_std]
#![feature(lang_items)]
extern crate "lang-item-public" as lang_lib;

View file

@ -38,6 +38,8 @@ const VARIANT2_NORTH: EnumWithStructVariants = EnumWithStructVariants::Variant2
pub mod glfw {
pub struct InputState(uint);
impl Copy for InputState {}
pub const RELEASE : InputState = InputState(0);
pub const PRESS : InputState = InputState(1);
pub const REPEAT : InputState = InputState(2);

View file

@ -14,6 +14,8 @@ static mut COUNT: u64 = 1;
struct Foo;
impl Copy for Foo {}
trait Bar {
fn foo1(&self);
fn foo2(self);

View file

@ -14,6 +14,8 @@ static mut COUNT: uint = 1;
struct Foo;
impl Copy for Foo {}
impl Foo {
fn foo(self, x: &Foo) {
unsafe { COUNT *= 2; }

View file

@ -19,12 +19,25 @@
*/
struct S<T> { i:u8, t:T }
impl<T> S<T> { fn unwrap(self) -> T { self.t } }
impl<T:Copy> Copy for S<T> {}
impl<T> S<T> {
fn unwrap(self) -> T {
self.t
}
}
#[deriving(PartialEq, Show)]
struct A((u32, u32));
impl Copy for A {}
#[deriving(PartialEq, Show)]
struct B(u64);
impl Copy for B {}
pub fn main() {
static Ca: S<A> = S { i: 0, t: A((13, 104)) };
static Cb: S<B> = S { i: 0, t: B(31337) };

View file

@ -18,6 +18,8 @@ struct MyType {
dummy: uint
}
impl Copy for MyType {}
impl MyTrait<uint> for MyType {
fn get(&self) -> uint { self.dummy }
}

View file

@ -27,6 +27,8 @@ struct MyType {
dummy: uint
}
impl Copy for MyType {}
impl MyTrait<uint> for MyType {
fn get(&self) -> uint { self.dummy }
}

View file

@ -10,7 +10,14 @@
struct mytype(Mytype);
struct Mytype {compute: fn(mytype) -> int, val: int}
impl Copy for mytype {}
struct Mytype {
compute: fn(mytype) -> int,
val: int,
}
impl Copy for Mytype {}
fn compute(i: mytype) -> int {
let mytype(m) = i;

View file

@ -13,6 +13,8 @@ pub struct Foo {
_f2: int,
}
impl Copy for Foo {}
#[inline(never)]
pub fn foo(f: &mut Foo) -> Foo {
let ret = *f;

View file

@ -15,6 +15,8 @@ struct DerefWrapper<X, Y> {
y: Y
}
impl<X:Copy,Y:Copy> Copy for DerefWrapper<X,Y> {}
impl<X, Y> DerefWrapper<X, Y> {
fn get_x(self) -> X {
self.x
@ -33,6 +35,8 @@ mod priv_test {
pub y: Y
}
impl<X:Copy,Y:Copy> Copy for DerefWrapperHideX<X,Y> {}
impl<X, Y> DerefWrapperHideX<X, Y> {
pub fn new(x: X, y: Y) -> DerefWrapperHideX<X, Y> {
DerefWrapperHideX {

View file

@ -19,6 +19,8 @@ struct Foo {
baz: u64
}
impl Copy for Foo {}
pub fn main() {
let foos = [Foo { bar: 1, baz: 2 }, .. 10];

View file

@ -10,6 +10,8 @@
struct Point {x: int, y: int}
impl Copy for Point {}
type rect = (Point, Point);
fn fst(r: rect) -> Point { let (fst, _) = r; return fst; }

View file

@ -13,6 +13,8 @@
struct Rect {x: int, y: int, w: int, h: int}
impl Copy for Rect {}
fn f(r: Rect, x: int, y: int, w: int, h: int) {
assert_eq!(r.x, x);
assert_eq!(r.y, y);

View file

@ -29,6 +29,8 @@ struct C {
f: int
}
impl Copy for C {}
fn get_v1(a: &A) -> &int {
// Region inferencer must deduce that &v < L2 < L1
let foo = &a.value; // L1

View file

@ -19,6 +19,8 @@ struct Box<'a> {
t: &'a int
}
impl<'a> Copy for Box<'a> {}
impl<'a> GetRef<'a> for Box<'a> {
fn get(&self) -> &'a int {
self.t

View file

@ -19,6 +19,8 @@ struct Box<'a, T:'a> {
t: &'a T
}
impl<'a,T:'a> Copy for Box<'a,T> {}
impl<'a,T:Clone> GetRef<'a,T> for Box<'a,T> {
fn get(&self) -> &'a T {
self.t

View file

@ -19,6 +19,8 @@ struct Box<T> {
t: T
}
impl<T:Copy> Copy for Box<T> {}
impl<T:Clone> Get<T> for Box<T> {
fn get(&self) -> T {
self.t.clone()

View file

@ -32,6 +32,9 @@ enum TypeStructure<'tcx> {
TypeInt,
TypeFunction(Type<'tcx>, Type<'tcx>),
}
impl<'tcx> Copy for TypeStructure<'tcx> {}
impl<'tcx> PartialEq for TypeStructure<'tcx> {
fn eq(&self, other: &TypeStructure<'tcx>) -> bool {
match (*self, *other) {
@ -93,6 +96,8 @@ struct NodeId {
id: uint
}
impl Copy for NodeId {}
type Ast<'ast> = &'ast AstStructure<'ast>;
struct AstStructure<'ast> {
@ -100,12 +105,16 @@ struct AstStructure<'ast> {
kind: AstKind<'ast>
}
impl<'ast> Copy for AstStructure<'ast> {}
enum AstKind<'ast> {
ExprInt,
ExprVar(uint),
ExprLambda(Ast<'ast>),
}
impl<'ast> Copy for AstKind<'ast> {}
fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>,
ast: Ast<'ast>) -> Type<'tcx>
{

View file

@ -15,6 +15,8 @@ struct Value {
n: int
}
impl Copy for Value {}
impl Value {
fn squared(mut self) -> Value {
self.n *= self.n;

View file

@ -1,67 +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.
// Exercises a bug in the shape code that was exposed
// on x86_64: when there is an enum embedded in an
// interior record which is then itself interior to
// something else, shape calculations were off.
#[deriving(Clone, Show)]
enum opt_span {
//hack (as opposed to option), to make `span` compile
os_none,
os_some(Box<Span>),
}
#[deriving(Clone, Show)]
struct Span {
lo: uint,
hi: uint,
expanded_from: opt_span,
}
#[deriving(Clone, Show)]
struct Spanned<T> {
data: T,
span: Span,
}
type ty_ = uint;
#[deriving(Clone, Show)]
struct Path_ {
global: bool,
idents: Vec<String> ,
types: Vec<Box<ty>>,
}
type path = Spanned<Path_>;
type ty = Spanned<ty_>;
#[deriving(Clone, Show)]
struct X {
sp: Span,
path: path,
}
pub fn main() {
let sp: Span = Span {lo: 57451u, hi: 57542u, expanded_from: opt_span::os_none};
let t: Box<ty> = box Spanned { data: 3u, span: sp.clone() };
let p_: Path_ = Path_ {
global: true,
idents: vec!("hi".to_string()),
types: vec!(t),
};
let p: path = Spanned { data: p_, span: sp.clone() };
let x = X { sp: sp, path: p };
println!("{}", x.path.clone());
println!("{}", x.clone());
}

View file

@ -15,6 +15,8 @@ use std::ops;
#[simd] struct f32x4(f32, f32, f32, f32);
impl Copy for f32x4 {}
fn add<T: ops::Add<T, T>>(lhs: T, rhs: T) -> T {
lhs + rhs
}

View file

@ -14,11 +14,17 @@
#[repr(u8)]
enum Eu { Lu = 0, Hu = 255 }
impl Copy for Eu {}
static CLu: Eu = Eu::Lu;
static CHu: Eu = Eu::Hu;
#[repr(i8)]
enum Es { Ls = -128, Hs = 127 }
impl Copy for Es {}
static CLs: Es = Es::Ls;
static CHs: Es = Es::Hs;

View file

@ -11,8 +11,13 @@
// ignore-lexer-test FIXME #15883
pub struct Quad { a: u64, b: u64, c: u64, d: u64 }
impl Copy for Quad {}
pub struct Floats { a: f64, b: u8, c: f64 }
impl Copy for Floats {}
mod rustrt {
use super::{Floats, Quad};

View file

@ -13,6 +13,8 @@
#[deriving(Show)]
enum foo { large, small, }
impl Copy for foo {}
impl PartialEq for foo {
fn eq(&self, other: &foo) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -20,6 +20,8 @@ enum color {
orange = 8 >> 1
}
impl Copy for color {}
impl PartialEq for color {
fn eq(&self, other: &color) -> bool {
((*self) as uint) == ((*other) as uint)

View file

@ -18,6 +18,8 @@ struct Struct {
y: int,
}
impl Copy for Struct {}
impl Trait<&'static str> for Struct {
fn f(&self, x: &'static str) {
println!("Hi, {}!", x);

View file

@ -19,6 +19,8 @@ struct Struct {
y: int,
}
impl Copy for Struct {}
impl Trait for Struct {
fn f(&self) {
println!("Hi!");

View file

@ -18,8 +18,11 @@ trait Equal {
fn isEq(a: &Self, b: &Self) -> bool;
}
#[deriving(Clone)]
enum Color { cyan, magenta, yellow, black }
impl Copy for Color {}
impl Equal for Color {
fn isEq(a: &Color, b: &Color) -> bool {
match (*a, *b) {
@ -32,6 +35,7 @@ impl Equal for Color {
}
}
#[deriving(Clone)]
enum ColorTree {
leaf(Color),
branch(Box<ColorTree>, Box<ColorTree>)
@ -40,9 +44,12 @@ enum ColorTree {
impl Equal for ColorTree {
fn isEq(a: &ColorTree, b: &ColorTree) -> bool {
match (a, b) {
(&leaf(x), &leaf(y)) => { Equal::isEq(&x, &y) }
(&leaf(ref x), &leaf(ref y)) => {
Equal::isEq(&(*x).clone(), &(*y).clone())
}
(&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
Equal::isEq(&**l1, &**l2) && Equal::isEq(&**r1, &**r2)
Equal::isEq(&(**l1).clone(), &(**l2).clone()) &&
Equal::isEq(&(**r1).clone(), &(**r2).clone())
}
_ => { false }
}

Some files were not shown because too many files have changed in this diff Show more