Skip to content

Support initialized union constants in the MIR importer #856

Closed

Description

@uurl

Problem

Initialized union constants are currently not materialized by the MIR importer.

This affects cases such as:

  • direct initialized union constants;
  • runtime-indexed arrays of unions ([U; N]);
  • unions nested inside tuples or structs;
  • MaybeUninit<T>-like storage patterns that rely on preserving partially initialized bytes.

Runtime union construction already carries the active field explicitly, but constant allocations do not preserve that information. Reconstructing a union by guessing an active field would therefore be incorrect.

Expected behavior

For pointer-free initialized unions, the importer should materialize the evaluated allocation as its physical byte image and transmute that storage into the corresponding MirUnionType.

The materialization should preserve rustc's initialization mask:

  • initialized bytes become byte constants;
  • uninitialized bytes remain undef;
  • no active union field is inferred.

This allows the representation to remain faithful to the evaluated constant while preserving inactive/uninitialized union storage.

Scope

Support:

  • direct pointer-free initialized union constants;
  • unions nested in tuples and structs;
  • runtime-indexed [U; N] constants;
  • partially initialized storage such as MaybeUninit<T>.

Keep unsupported for now:

  • pointer-bearing union constants or union allocations containing relocations;
  • fat-pointer/provenance-sensitive union constants;
  • pointer-to-array union constants such as &[U; N];
  • device-global initializer relocations.

Acceptance criteria

  • Pointer-free initialized union constants compile and execute correctly.
  • Runtime indexing into [U; N] works.
  • Union constants nested inside tuples and structs work.
  • Uninitialized bytes are preserved as undef rather than zero-filled.
  • Pointer-bearing/provenance-sensitive union constants fail closed.
  • Existing constant-array and relocation behavior remains unchanged.
  • The behavior is covered by array_constants runtime and code-shape tests.

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