Skip to content

mir-importer rejects thin-pointer union constants with relocations #926

Closed

Description

@uurl

Description

The MIR importer currently rejects initialized union constants when their storage contains pointer relocations. Pointer-free union constants are materialized from their evaluated byte image, but applying the same strategy to pointer-bearing unions would lose pointer provenance because the allocation bytes only contain the relocation placeholder/addend, not a reconstructible pointer value.

A useful conservative subset can be supported when every non-ZST union field is a compatible thin pointer: reconstruct one typed pointer carrier from the relocation and insert it into the union storage while preserving provenance.

Minimal reproducer

#[derive(Clone, Copy)]
#[repr(C)]
union PointerBits {
    word: *const u32,
    bytes: *const u8,
}

static VALUES: [u32; 3] = [11, 17, 23];

const DIRECT: PointerBits = PointerBits {
    word: &VALUES[0] as *const u32,
};

const TABLE: [PointerBits; 2] = [
    PointerBits {
        word: &VALUES[0] as *const u32,
    },
    PointerBits {
        bytes: &VALUES[2] as *const u32 as *const u8,
    },
];

Using these constants from device code requires preserving both the relocation target and, for the second table entry, the non-zero byte addend.

Expected behavior

Thin-pointer-only union constants should preserve pointer provenance in the same way that pointer fields in array, tuple, struct, and supported enum constants do.

The importer should:

  • reconstruct an exact thin pointer from a relocation at the union storage base;
  • preserve non-zero static byte addends;
  • materialize the union using a typed pointer carrier rather than raw integer/byte reconstruction;
  • continue to reject cases where a single typed carrier is not sufficient to represent the storage safely.

In particular, pointer/integer overlapping unions, fat or nested pointer storage, and over-aligned/padded pointer unions can remain unsupported initially.

Actual behavior

Pointer-free initialized union constants are supported, but union constants containing pointer relocations are rejected rather than reconstructed through a provenance-preserving pointer representation.

Additional context

This is a follow-up to the initialized union constant support added previously. The intended scope is deliberately narrow: support compatible thin-pointer-only union constants without weakening the existing fail-closed behavior for ambiguous pointer/integer or fat-pointer storage.

Activity

  1. added
    bug
    codegen
    on Aug 16, 2026
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

    bugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions