3aa14e3b2e
In its first step of computing transmutability, `rustc_transmutability` constructs a byte-level representation of type layout (`Tree`). Previously, this representation was computed for ADTs by inspecting the ADT definition and performing our own layout computations. This process was error-prone, verbose, and limited our ability to analyze many types (particularly default-repr types). In this PR, we instead construct `Tree`s from `rustc_target::abi::Layout`s. This helps ensure that layout optimizations are reflected our analyses, and increases the kinds of types we can now analyze, including: - default repr ADTs - transparent unions - `UnsafeCell`-containing types Overall, this PR expands the expressvity of `rustc_transmutability` to be much closer to the transmutability analysis performed by miri. Future PRs will work to close the remaining gaps (e.g., support for `Box`, raw pointers, `NonZero*`, coroutines, etc.).
157 lines
3.3 KiB
Rust
157 lines
3.3 KiB
Rust
//@ check-pass
|
|
//! Checks that niche optimizations are encoded correctly.
|
|
#![crate_type = "lib"]
|
|
#![feature(transmutability)]
|
|
#![allow(dead_code, incomplete_features, non_camel_case_types)]
|
|
|
|
mod assert {
|
|
use std::mem::{Assume, BikeshedIntrinsicFrom};
|
|
|
|
pub fn is_transmutable<Src, Dst>()
|
|
where
|
|
Dst: BikeshedIntrinsicFrom<Src, {
|
|
Assume {
|
|
alignment: false,
|
|
lifetimes: false,
|
|
safety: true,
|
|
validity: false,
|
|
}
|
|
}>
|
|
{}
|
|
|
|
pub fn is_maybe_transmutable<Src, Dst>()
|
|
where
|
|
Dst: BikeshedIntrinsicFrom<Src, {
|
|
Assume {
|
|
alignment: false,
|
|
lifetimes: false,
|
|
safety: true,
|
|
validity: true,
|
|
}
|
|
}>
|
|
{}
|
|
}
|
|
|
|
#[repr(u8)] enum V0 { V = 0 }
|
|
#[repr(u8)] enum V1 { V = 1 }
|
|
#[repr(u8)] enum V2 { V = 2 }
|
|
#[repr(u8)] enum V253 { V = 253 }
|
|
#[repr(u8)] enum V254 { V = 254 }
|
|
#[repr(u8)] enum V255 { V = 255 }
|
|
|
|
fn bool() {
|
|
enum OptionLike {
|
|
A(bool),
|
|
B,
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<OptionLike>() == 1);
|
|
};
|
|
|
|
assert::is_transmutable::<OptionLike, u8>();
|
|
|
|
assert::is_transmutable::<bool, OptionLike>();
|
|
assert::is_transmutable::<V0, OptionLike>();
|
|
assert::is_transmutable::<V1, OptionLike>();
|
|
assert::is_transmutable::<V2, OptionLike>();
|
|
}
|
|
|
|
fn one_niche() {
|
|
#[repr(u8)]
|
|
enum N1 {
|
|
S = 0,
|
|
E = 255 - 1,
|
|
}
|
|
|
|
enum OptionLike {
|
|
A(N1),
|
|
B,
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<OptionLike>() == 1);
|
|
};
|
|
|
|
assert::is_transmutable::<OptionLike, u8>();
|
|
assert::is_transmutable::<V0, OptionLike>();
|
|
assert::is_transmutable::<V254, OptionLike>();
|
|
assert::is_transmutable::<V255, OptionLike>();
|
|
}
|
|
|
|
fn one_niche_alt() {
|
|
#[repr(u8)]
|
|
enum N1 {
|
|
S = 1,
|
|
E = 255 - 1,
|
|
}
|
|
|
|
enum OptionLike {
|
|
A(N1),
|
|
B,
|
|
C,
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<OptionLike>() == 1);
|
|
};
|
|
|
|
assert::is_transmutable::<OptionLike, u8>();
|
|
assert::is_transmutable::<V0, OptionLike>();
|
|
assert::is_transmutable::<V254, OptionLike>();
|
|
assert::is_transmutable::<V255, OptionLike>();
|
|
}
|
|
|
|
fn two_niche() {
|
|
#[repr(u8)]
|
|
enum Niche {
|
|
S = 0,
|
|
E = 255 - 2,
|
|
}
|
|
|
|
enum OptionLike {
|
|
A(Niche),
|
|
B,
|
|
C,
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<OptionLike>() == 1);
|
|
};
|
|
|
|
assert::is_transmutable::<OptionLike, u8>();
|
|
assert::is_transmutable::<V0, OptionLike>();
|
|
assert::is_transmutable::<V253, OptionLike>();
|
|
assert::is_transmutable::<V254, OptionLike>();
|
|
assert::is_transmutable::<V255, OptionLike>();
|
|
}
|
|
|
|
fn no_niche() {
|
|
use std::mem::MaybeUninit;
|
|
|
|
#[repr(u8)]
|
|
enum Niche {
|
|
S = 0,
|
|
E = 255 - 1,
|
|
}
|
|
|
|
enum OptionLike {
|
|
A(Niche),
|
|
B,
|
|
C,
|
|
}
|
|
|
|
const _: () = {
|
|
assert!(std::mem::size_of::<OptionLike>() == 2);
|
|
};
|
|
|
|
#[repr(C)]
|
|
struct Pair<T, U>(T, U);
|
|
|
|
assert::is_transmutable::<V0, Niche>();
|
|
assert::is_transmutable::<V254, Niche>();
|
|
assert::is_transmutable::<Pair<V0, Niche>, OptionLike>();
|
|
assert::is_transmutable::<Pair<V1, MaybeUninit<u8>>, OptionLike>();
|
|
assert::is_transmutable::<Pair<V2, MaybeUninit<u8>>, OptionLike>();
|
|
}
|