Skip to content

Commit 8bf6594

Browse files
committed
Auto merge of #92268 - jswrenn:transmute, r=oli-obk
Initial implementation of transmutability trait. *T'was the night before Christmas and all through the codebase, not a miri was stirring — no hint of `unsafe`!* This PR provides an initial, **incomplete** implementation of *[MCP 411: Lang Item for Transmutability](rust-lang/compiler-team#411. The `core::mem::BikeshedIntrinsicFrom` trait provided by this PR is implemented on-the-fly by the compiler for types `Src` and `Dst` when the bits of all possible values of type `Src` are safely reinterpretable as a value of type `Dst`. What this PR provides is: - [x] [support for transmutations involving primitives](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/primitives) - [x] [support for transmutations involving arrays](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/arrays) - [x] [support for transmutations involving structs](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/structs) - [x] [support for transmutations involving enums](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/enums) - [x] [support for transmutations involving unions](https://github.com/jswrenn/rust/tree/transmute/src/test/ui/transmutability/unions) - [x] [support for weaker validity checks](https://github.com/jswrenn/rust/blob/transmute/src/test/ui/transmutability/unions/should_permit_intersecting_if_validity_is_assumed.rs) (i.e., `Assume::VALIDITY`) - [x] visibility checking What isn't yet implemented: - [ ] transmutability options passed using the `Assume` struct - [ ] [support for references](https://github.com/jswrenn/rust/blob/transmute/src/test/ui/transmutability/references.rs) - [ ] smarter error messages These features will be implemented in future PRs.
2 parents 094a669 + 2d04c50 commit 8bf6594

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

core/src/mem/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ mod valid_align;
2727
// alignment as a parameter, such as `Layout::padding_needed_for`.
2828
pub(crate) use valid_align::ValidAlign;
2929

30+
mod transmutability;
31+
#[unstable(feature = "transmutability", issue = "99571")]
32+
pub use transmutability::{Assume, BikeshedIntrinsicFrom};
33+
3034
#[stable(feature = "rust1", since = "1.0.0")]
3135
#[doc(inline)]
3236
pub use crate::intrinsics::transmute;

core/src/mem/transmutability.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Are values of a type transmutable into values of another type?
2+
///
3+
/// This trait is implemented on-the-fly by the compiler for types `Src` and `Self` when the bits of
4+
/// any value of type `Self` are safely transmutable into a value of type `Dst`, in a given `Context`,
5+
/// notwithstanding whatever safety checks you have asked the compiler to [`Assume`] are satisfied.
6+
#[unstable(feature = "transmutability", issue = "99571")]
7+
#[cfg_attr(not(bootstrap), lang = "transmute_trait")]
8+
#[rustc_on_unimplemented(
9+
message = "`{Src}` cannot be safely transmuted into `{Self}` in the defining scope of `{Context}`.",
10+
label = "`{Src}` cannot be safely transmuted into `{Self}` in the defining scope of `{Context}`."
11+
)]
12+
pub unsafe trait BikeshedIntrinsicFrom<
13+
Src,
14+
Context,
15+
const ASSUME_ALIGNMENT: bool,
16+
const ASSUME_LIFETIMES: bool,
17+
const ASSUME_VALIDITY: bool,
18+
const ASSUME_VISIBILITY: bool,
19+
> where
20+
Src: ?Sized,
21+
{
22+
}
23+
24+
/// What transmutation safety conditions shall the compiler assume that *you* are checking?
25+
#[unstable(feature = "transmutability", issue = "99571")]
26+
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
27+
pub struct Assume {
28+
/// When `true`, the compiler assumes that *you* are ensuring (either dynamically or statically) that
29+
/// destination referents do not have stricter alignment requirements than source referents.
30+
pub alignment: bool,
31+
32+
/// When `true`, the compiler assume that *you* are ensuring that lifetimes are not extended in a manner
33+
/// that violates Rust's memory model.
34+
pub lifetimes: bool,
35+
36+
/// When `true`, the compiler assumes that *you* are ensuring that the source type is actually a valid
37+
/// instance of the destination type.
38+
pub validity: bool,
39+
40+
/// When `true`, the compiler assumes that *you* have ensured that it is safe for you to violate the
41+
/// type and field privacy of the destination type (and sometimes of the source type, too).
42+
pub visibility: bool,
43+
}

0 commit comments

Comments
 (0)