mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
1ad218f3af
As our implementation of MCP411 nears completion and we begin to solicit testing, it's no longer reasonable to expect testers to type or remember `BikeshedIntrinsicFrom`. The name degrades the ease-of-reading of documentation, and the overall experience of using compiler safe transmute. Tentatively, we'll instead adopt `TransmuteFrom`. This name seems to be the one most likely to be stabilized, after discussion on Zulip [1]. We may want to revisit the ordering of `Src` and `Dst` before stabilization, at which point we'd likely consider `TransmuteInto` or `Transmute`. [1] https://rust-lang.zulipchat.com/#narrow/stream/216762-project-safe-transmute/topic/What.20should.20.60BikeshedIntrinsicFrom.60.20be.20named.3F
94 lines
2.9 KiB
Rust
94 lines
2.9 KiB
Rust
#![crate_type = "lib"]
|
|
#![feature(transmutability)]
|
|
#![allow(dead_code, incomplete_features, non_camel_case_types)]
|
|
|
|
mod assert {
|
|
use std::mem::{Assume, TransmuteFrom};
|
|
|
|
pub fn is_maybe_transmutable<Src, Dst>()
|
|
where
|
|
Dst: TransmuteFrom<Src, {
|
|
Assume {
|
|
alignment: true,
|
|
lifetimes: true,
|
|
safety: true,
|
|
validity: true,
|
|
}
|
|
}>
|
|
{}
|
|
}
|
|
|
|
fn void() {
|
|
enum Void {}
|
|
|
|
// This transmutation is vacuously acceptable; since one cannot construct a
|
|
// `Void`, unsoundness cannot directly arise from transmuting a void into
|
|
// anything else.
|
|
assert::is_maybe_transmutable::<Void, u128>();
|
|
|
|
assert::is_maybe_transmutable::<(), Void>(); //~ ERROR: cannot be safely transmuted
|
|
}
|
|
|
|
// Non-ZST uninhabited types are, nonetheless, uninhabited.
|
|
fn yawning_void_struct() {
|
|
enum Void {}
|
|
|
|
struct YawningVoid(Void, u128);
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<YawningVoid>() == std::mem::size_of::<u128>());
|
|
// Just to be sure the above constant actually evaluated:
|
|
assert!(false); //~ ERROR: evaluation of constant value failed
|
|
};
|
|
|
|
// This transmutation is vacuously acceptable; since one cannot construct a
|
|
// `Void`, unsoundness cannot directly arise from transmuting a void into
|
|
// anything else.
|
|
assert::is_maybe_transmutable::<YawningVoid, u128>();
|
|
|
|
assert::is_maybe_transmutable::<(), Void>(); //~ ERROR: cannot be safely transmuted
|
|
}
|
|
|
|
// Non-ZST uninhabited types are, nonetheless, uninhabited.
|
|
fn yawning_void_enum() {
|
|
enum Void {}
|
|
|
|
enum YawningVoid {
|
|
A(Void, u128),
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<YawningVoid>() == std::mem::size_of::<u128>());
|
|
// Just to be sure the above constant actually evaluated:
|
|
assert!(false); //~ ERROR: evaluation of constant value failed
|
|
};
|
|
|
|
// This transmutation is vacuously acceptable; since one cannot construct a
|
|
// `Void`, unsoundness cannot directly arise from transmuting a void into
|
|
// anything else.
|
|
assert::is_maybe_transmutable::<YawningVoid, u128>();
|
|
|
|
assert::is_maybe_transmutable::<(), Void>(); //~ ERROR: cannot be safely transmuted
|
|
}
|
|
|
|
// References to uninhabited types are, logically, uninhabited, but for layout
|
|
// purposes are not ZSTs, and aren't treated as uninhabited when they appear in
|
|
// enum variants.
|
|
fn distant_void() {
|
|
enum Void {}
|
|
|
|
enum DistantVoid {
|
|
A(&'static Void)
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<DistantVoid>() == std::mem::size_of::<usize>());
|
|
// Just to be sure the above constant actually evaluated:
|
|
assert!(false); //~ ERROR: evaluation of constant value failed
|
|
};
|
|
|
|
assert::is_maybe_transmutable::<DistantVoid, ()>();
|
|
assert::is_maybe_transmutable::<DistantVoid, &'static Void>();
|
|
assert::is_maybe_transmutable::<u128, DistantVoid>(); //~ ERROR: cannot be safely transmuted
|
|
}
|