Skip to content

Enum payloads cannot be mutated in place: no address for a Downcast projection #651

Closed

Description

@alejandro-soto-franco

Summary

Mutating an enum payload in place is not expressible. Both spellings need the address of the payload, and the importer's address walker has no arm for Downcast, so both fail:

enum Slot { Occupied(f32), Empty }

let mut slot = Slot::Occupied(x);

// (1) mutable borrow of a payload
if let Slot::Occupied(value) = &mut slot {
    *value *= 2.0;
}

// (2) direct assignment, which is how (1) often arrives after inlining
// (slot as Occupied).0 = x * 2.0;

(1) fails with

Unsupported construct: Rvalue::Ref: cannot compute an in-memory address for the
mutable borrow of place (((*_1) as variant#0).0) (projection [Deref,
Downcast(VariantIdx(0)), Field(0, ...)]); the value-copy fallback would silently
discard writes through the borrow

and (2) with

Unsupported construct: 2-level projection Downcast(VariantIdx(0)) -> Field(0, ...)
not yet implemented for assignment

Why the refusal is right and still a gap

The first message is a deliberate refusal, not an oversight: Rvalue::Ref will not fall back to a value copy for a mutable borrow, because writes through the borrow would land in the copy and the original place would never change. Refusing loudly is the correct behaviour given no address can be computed. What is missing is the address.

translate_place_addr_from_slot marks the spot:

// Enum-variant downcast (`(x as Variant).field`). Addressing an
// enum payload in memory needs variant/niche layout machinery
// [...] This arm is the designed extension point for the
// enum-layout work tracked in issues #131/#146;
// until that lands, punt [...]
mir::ProjectionElem::Downcast(_) => return Ok(None),

Both #131 and #146 are closed, so the extension point is unclaimed.

What the workaround costs

Today the payload has to be rebuilt from a matched copy:

let slot = match slot {
    Slot::Occupied(value) => Slot::Occupied(value * 2.0),
    Slot::Empty => Slot::Empty,
};

That is expressible, so this is a gap in what compiles rather than in what runs. It bites hardest where the borrowing form is the natural one: a helper taking &mut T, or any if let ... = &mut e over an enum a kernel is updating.

What a solution needs

  1. An address for the payload. A downcast moves no address of its own, since a payload shares the enum's storage; the variant only decides which field the following Field names.
  2. The shared-byte case. Variants share bytes, so a payload field either has a slot of its own or its bytes are already held by a differently typed field of another variant. Both must resolve to the same storage, or a write lands somewhere the reader will not look.
  3. A loud failure for anything it cannot resolve. Falling back to a value copy for a mutable borrow is exactly the silent miscompile the current code refuses to emit.

Open design questions

  • Should the payload address be a new operation, or does MirFieldAddrOp extend to an enum pointee? all_field_types already runs variant by variant, so one flattened index names a (variant, field) pair without a second attribute.
  • Should the assignment path get its own (Downcast, Field) arm, or delegate to the shared walk-and-store helper that the other 2-level pairs already use?
  • Is a payload behind a niche-encoded variant in scope, or should the first pass cover Direct layouts and reject the rest?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions