Compute transmutability from `rustc_target::abi::Layout`
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.).
2024-03-19 09:49:13 -05:00
|
|
|
//@ 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 {
|
2024-08-27 09:05:54 -05:00
|
|
|
use std::mem::{Assume, TransmuteFrom};
|
Compute transmutability from `rustc_target::abi::Layout`
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.).
2024-03-19 09:49:13 -05:00
|
|
|
|
|
|
|
pub fn is_transmutable<Src, Dst>()
|
|
|
|
where
|
2024-08-27 09:05:54 -05:00
|
|
|
Dst: TransmuteFrom<Src, {
|
Compute transmutability from `rustc_target::abi::Layout`
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.).
2024-03-19 09:49:13 -05:00
|
|
|
Assume {
|
|
|
|
alignment: false,
|
|
|
|
lifetimes: false,
|
|
|
|
safety: true,
|
|
|
|
validity: false,
|
|
|
|
}
|
|
|
|
}>
|
|
|
|
{}
|
|
|
|
|
|
|
|
pub fn is_maybe_transmutable<Src, Dst>()
|
|
|
|
where
|
2024-08-27 09:05:54 -05:00
|
|
|
Dst: TransmuteFrom<Src, {
|
Compute transmutability from `rustc_target::abi::Layout`
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.).
2024-03-19 09:49:13 -05:00
|
|
|
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>();
|
|
|
|
}
|
2024-09-14 16:30:07 -05:00
|
|
|
|
|
|
|
fn niche_fields() {
|
|
|
|
enum Kind {
|
|
|
|
A(bool, bool),
|
|
|
|
B(bool),
|
|
|
|
}
|
|
|
|
|
2024-09-14 16:52:03 -05:00
|
|
|
assert::is_maybe_transmutable::<u16, Kind>();
|
2024-09-14 16:30:07 -05:00
|
|
|
}
|