2025-06-29 17:43:51 +05:00
|
|
|
//! Test that monomorphization correctly distinguishes types with different ABI alignment.
|
|
|
|
|
//!
|
|
|
|
|
//! On x86_64-linux-gnu and similar platforms, structs get 8-byte "preferred"
|
|
|
|
|
//! alignment, but their "ABI" alignment (what actually matters for data layout)
|
|
|
|
|
//! is the largest alignment of any field. If monomorphization incorrectly uses
|
|
|
|
|
//! "preferred" alignment instead of "ABI" alignment, it might unify types `A`
|
|
|
|
|
//! and `B` even though `S<A>` and `S<B>` have field `t` at different offsets,
|
|
|
|
|
//! leading to incorrect method dispatch for `unwrap()`.
|
2019-07-27 00:54:25 +03:00
|
|
|
|
2025-06-29 17:43:51 +05:00
|
|
|
//@ run-pass
|
2013-06-03 22:34:51 -07:00
|
|
|
|
2015-03-30 09:38:27 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2025-06-29 17:43:51 +05:00
|
|
|
struct S<T> {
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
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-05 17:01:33 -08:00
|
|
|
|
|
|
|
|
impl<T> S<T> {
|
|
|
|
|
fn unwrap(self) -> T {
|
|
|
|
|
self.t
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 09:38:27 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2025-06-29 17:43:51 +05:00
|
|
|
struct A((u32, u32)); // Different ABI alignment than B
|
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-05 17:01:33 -08:00
|
|
|
|
2015-03-30 09:38:27 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2025-06-29 17:43:51 +05:00
|
|
|
struct B(u64); // Different ABI alignment than A
|
2013-06-03 22:34:51 -07:00
|
|
|
|
|
|
|
|
pub fn main() {
|
2025-06-29 17:43:51 +05:00
|
|
|
static CA: S<A> = S { i: 0, t: A((13, 104)) };
|
|
|
|
|
static CB: S<B> = S { i: 0, t: B(31337) };
|
|
|
|
|
|
|
|
|
|
assert_eq!(CA.unwrap(), A((13, 104)));
|
|
|
|
|
assert_eq!(CB.unwrap(), B(31337));
|
2013-06-03 22:34:51 -07:00
|
|
|
}
|