rust-lang / rust-lang/rust-analyzer
View memory layout gives incorrect layout for unions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
rust-analyzer version: rust-analyzer version: 0.4.1830-standalone
rustc version: rustc 1.77.0 (aedd173a2 2024-03-17)
editor or extension: Code OSS (on arch linux, installed with pacman, code 1.87.2-1)
relevant settings: None
code snippet to reproduce:
union Union {
a: u16,
b: u32,
}
result when hitting Ctrl+Shift+P and going to 'view memory layout':
when hovering on the b: u32 field, the tooltip reports an offset of 0, although it is not placed at an offset of 0 in here:
this is a cool feature, but it just needs some more polishing:
for example, it knows PhantomData<T> always has zero size, although in the tooltip it will say A<{unknown}>, but [T; 0] also has zero size and for that it says 'Not Available'
struct Struct<T> {
a: usize,
b: PhantomData<T>,
}
it does not account for fat pointers:
struct Struct<'a, T: ?Sized> {
a: usize,
b: &'a T,
}
this will report the &T being 8 bytes wide (&{unknown} here too). this is only correct if T: Sized
and for enums just size and align is displayed, nothing is done with the members
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the provided Union example through the View memory layout command, then inspect the existing memory-layout implementation and its tests. Confirm how union field offsets are calculated and determine whether the PhantomData, zero-length array, fat-pointer, and enum cases belong in the same change. Done means the displayed layout and hover offsets match Rust's layout rules for the agreed scope.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100