Skip to content

Support bare arrays of struct constants #854

Closed

Description

@uurl

Summary

Bare array constants currently support primitive scalars, enums, tuples, and nested arrays, but arrays whose element type is a struct are rejected.

This prevents constants such as:

#[repr(C)]
struct Entry {
    tag: u8,
    value: u32,
}

const TABLE: [Entry; 2] = [
    Entry { tag: 1, value: 10 },
    Entry { tag: 2, value: 20 },
];

from being materialized through the MIR importer when used as bare array values.

Expected behavior

Bare [Struct; N] constants should be materialized using the existing layout-aware aggregate constant decoding path.

The implementation should preserve:

  • rustc-recorded struct field offsets,
  • padding,
  • nested aggregate layout,
  • zero-sized and over-aligned struct behavior,
  • recursive array/aggregate decoding.

Scope

This issue covers bare array values only.

The following remain out of scope:

  • pointer-to-array constants such as &[Struct; N],
  • widening immutable-global array promotion to struct elements,
  • union constants.

Bare arrays may fall back to element-wise materialization when the element type is not eligible for immutable-global promotion. Pointer-to-array constants do not have that fallback and should retain their existing boundary.

Acceptance criteria

  • [Struct; N] bare constants are accepted.
  • Nested struct constants inside arrays are supported.
  • Struct padding and field offsets follow rustc layout information.
  • Over-aligned zero-sized struct arrays behave correctly.
  • Existing pointer-to-array struct rejection remains unchanged.
  • Existing array constant behavior does not regress.
  • Codegen regression coverage validates the generated LLVM shape for padded and nested struct arrays.

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