rust-lang / rust-lang/reference

Does T and U having the same layout mean that `Foo<T>` and `Foo<U>` have the same layout?

Open
#1,019 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-layout
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

Hey!

I'm not sure whether here is the right place to open this issue, but I've been in a discussion with @nox about the things that are allowed with repr(transparent) structs.

In particular, suppose this code:

struct Foo<T>(T);
struct Bar;
#[repr(transparent)] struct Baz(Bar);

fn foo(x: Foo<Bar>) -> Foo<Baz> {
    unsafe { std::mem::transmute(x) }
}

Is it correct? In other words, is Foo<T> guaranteed to have the same layout as Foo<U> if T has the same layout as U? (knowing that Foo is repr(Rust))

I'm thinking the answer to this question should be added to the repr(transparent) part of the reference, but I couldn't find the answer in the related RFC; so I couldn't find an answer to this question.

Is the answer to the question known? (In practice it is to the best of my knowledge currently the same layout, but I don't know whether it is a guarantee given for the foreseeable future)

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

Read the related RFC and the repr(transparent) section of the Rust Reference, then evaluate the Foo<Bar> and Foo<Baz> example in the issue. Done means determining whether the layout guarantee is specified and documenting the answer in the appropriate Reference section.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.