Repository navigation
Description
Summary
Pointer/integer union constants are currently rejected because a byte-only
representation cannot preserve pointer provenance when the evaluated allocation
contains a pointer relocation.
There is, however, a narrower case where provenance is not involved: a
pointer/integer union whose evaluated storage contains no overlapping
relocations already has a complete byte representation. One common example is
a union initialized through its full-width integer alternative.
In that case, rustc's evaluated allocation already contains the complete storage
representation, so the union can be materialized using the existing byte-image
path.
Proposed scope
Support relocation-free mixed pointer/integer unions when:
- the union storage is exactly one pointer word,
- the union has natural pointer alignment,
- all pointer alternatives are compatible thin pointers,
- all integer alternatives are full pointer-width integers, and
- no relocation overlaps the union storage.
For example:
#[repr(C)]
union PointerOrBits {
pointer: *const u32,
bits: u64,
}
const VALUE: PointerOrBits = PointerOrBits {
bits: 0x1122_3344_5566_7788,
};On a 64-bit target, this constant has no pointer relocation. Its evaluated byte
image is therefore sufficient to reconstruct the union storage exactly.
Non-goals
This should remain fail-closed for:
- pointer/integer unions containing an actual pointer relocation,
- fat pointers,
- nested pointer aggregates,
- partial-width integer alternatives,
- mixed pointer address spaces,
- padded or over-aligned pointer unions, and
- mixed pointer/integer device-global union initializers.
Thin-pointer-only unions with relocations should continue to use the existing
provenance-preserving pointer carrier path.
Implementation direction
Keep the existing provenance-aware union classifier unchanged.
Before invoking it during allocation-backed union constant translation, detect
the relocation-free pointer/integer shape described above and route it through
the existing byte-image materializer.
This keeps relocation-bearing behavior unchanged while admitting the case where
the evaluated bytes are the complete storage truth.
Regression coverage
Add coverage for:
- direct relocation-free pointer/integer union constants,
[U; N]relocation-free pointer/integer union constants,- optimized and non-optimized GPU execution,
- rejection of pointer-width mismatches and partial-width integer alternatives,
- LLVM code shape requiring byte-image materialization, and
- absence of
inttoptrreconstruction.
This is a follow-up to the pointer-union constant work in #927.
Activity