How does MIR model discriminants?


MIR models discriminants more precisely. They should be thought of as a distinct field when it comes to borrows

How does MIR model discriminants? And what's the meaning of should be thought of as a distinct field?

Is it about enum?

Better having an example, thanks.

They are modelled using the pattern known as a tagged union. You can find an example of that here, which I have copied in below:

// This Enum has the same representation as ...
enum MyEnum {
    B(f32, u64),
    C { x: u32, y: u8 },

// ... this struct.
struct MyEnumRepr {
    tag: MyEnumDiscriminant,
    payload: MyEnumFields,

// This is the discriminant enum.
enum MyEnumDiscriminant { A, B, C, D }

// This is the variant union.
union MyEnumFields {
    A: MyAFields,
    B: MyBFields,
    C: MyCFields,
    D: MyDFields,

#[derive(Copy, Clone)]
struct MyAFields(u32);

#[derive(Copy, Clone)]
struct MyBFields(f32, u64);

#[derive(Copy, Clone)]
struct MyCFields { x: u32, y: u8 }

// This struct could be omitted (it is a zero-sized type)
#[derive(Copy, Clone)]
struct MyDFields;

The above describes the layout that Rust guarantees for an #[repr(C)] enum, but it is also the layout that miri uses for #[repr(Rust)] enums (except that the *Fields structs are not marked #[repr(C)] for a #[repr(Rust)] enum.) Of course, this is not guaranteed behavior, since nothing is guaranteed about the layout of #[repr(Rust)] types.


I mainly want to know about the meaning of the later sentence.

Which sentence?

I think that means something like "if you only need to check the discriminant, MIR won't even try to borrow anything else".

1 Like


let x = match some_option {
    None => 0,
    Some(_) => 1,

Only the discriminant need be examined.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.