2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2019-07-26 21:54:25 +00:00
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(non_upper_case_globals)]
|
2021-05-21 17:35:49 +00:00
|
|
|
#![allow(dead_code)]
|
2013-06-04 05:34:51 +00:00
|
|
|
/*!
|
|
|
|
* On x86_64-linux-gnu and possibly other platforms, structs get 8-byte "preferred" alignment,
|
|
|
|
* but their "ABI" alignment (i.e., what actually matters for data layout) is the largest alignment
|
2019-02-09 21:23:30 +00:00
|
|
|
* of any field. (Also, `u64` has 8-byte ABI alignment; this is not always true).
|
2013-06-04 05:34:51 +00:00
|
|
|
*
|
|
|
|
* On such platforms, if monomorphize uses the "preferred" alignment, then it will unify
|
|
|
|
* `A` and `B`, even though `S<A>` and `S<B>` have the field `t` at different offsets,
|
|
|
|
* and apply the wrong instance of the method `unwrap`.
|
|
|
|
*/
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2013-06-04 05:34:51 +00:00
|
|
|
struct S<T> { i:u8, t:T }
|
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]
2014-12-06 01:01:33 +00:00
|
|
|
|
|
|
|
impl<T> S<T> {
|
|
|
|
fn unwrap(self) -> T {
|
|
|
|
self.t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2013-06-04 05:34:51 +00:00
|
|
|
struct A((u32, u32));
|
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]
2014-12-06 01:01:33 +00:00
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2013-06-04 05:34:51 +00:00
|
|
|
struct B(u64);
|
|
|
|
|
|
|
|
pub fn main() {
|
2013-06-13 21:59:34 +00:00
|
|
|
static Ca: S<A> = S { i: 0, t: A((13, 104)) };
|
|
|
|
static Cb: S<B> = S { i: 0, t: B(31337) };
|
2013-11-02 01:06:31 +00:00
|
|
|
assert_eq!(Ca.unwrap(), A((13, 104)));
|
|
|
|
assert_eq!(Cb.unwrap(), B(31337));
|
2013-06-04 05:34:51 +00:00
|
|
|
}
|