rust-lang / rust-lang/rust-analyzer

View memory layout gives incorrect layout for unions

Open
#17,091 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
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':
image

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:
image

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.