Skip to content

Conversation

@jberthold
Copy link
Member

#### Context
Motivated by
[iterator-simple-fail.rs](https://github.com/runtimeverification/mir-semantics/blob/3d763c00389dcdb45a68523d6515c6d6fc8b2928/kmir/src/tests/integration/data/prove-rs/iterator-simple-fail.rs#L1-L7)
(which should pass).

The type
[std::mem::MaybeUninit](https://doc.rust-lang.org/std/mem/union.MaybeUninit.html)
is a union that represents a potentially uninitialised location in
memory. This union has two fields, first `()`, and second
[std::mem::ManuallyDrop<T>](https://doc.rust-lang.org/std/mem/struct.ManuallyDrop.html)
which represents the initialised data. When [converting an array to an
iterator](https://github.com/rust-lang/rust/blob/a2545fd6fc66b4323f555223a860c451885d1d2b/library/core/src/array/iter.rs#L57-L70)
a `Transmute` cast is invoked for the array element type (`T` from `[T;
N]`) into `std::mem::MaybeUninit<T>`.

#### This PR
This PR implements the cast `CastKind::Transmute` of `T` into
`std::mem:MaybeUninit<T>`.

The logic of the semantics and saftey of this cast for us is:
- that there is a `Value` to be cast as we cannot construct a `Value`
that is not initialised;
- that the type being cast from `T` is the same as the type of the
unions second field `std::mem::ManuallyDrop<T>`;
- we can then then create a union that is constructed with the second
field, instantiating the `Value` in a struct;
- otherwise we error so that the cast does not `thunk`.

Tests are added to show the passing cases for `transmute` and
`transmute_unchecked`, and the failing case where the types are not
valid for the transmute as explained above.

A follow up PR will handle converting the elements of the array when an
array, this PR is just for a single element.
@jberthold jberthold requested a review from dkcumming November 26, 2025 23:04
@jberthold jberthold marked this pull request as ready for review November 26, 2025 23:04
@jberthold jberthold requested a review from sskeirik November 27, 2025 00:49
@jberthold jberthold merged commit bb8d23e into feature/p-token Nov 27, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants