Skip to content

Tracking issue for RFC 1758: Specify repr(transparent) #43036

Description

@aturon

RFC

This is a tracking issue for the RFC "Specify repr(transparent)" (rust-lang/rfcs#1758).

Steps:

Activity

  1. added
    B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.
    T-langRelevant to the language team
    on Jul 3, 2017
  2. changed the title [-]Tracking issue for RFC 1758: Specify [/-] [+]Tracking issue for RFC 1758: Specify `repr(transparent)`[/+] on Jul 3, 2017
  3. eddyb commented on Jul 3, 2017

    @eddyb
    Contributor

    Mentoring instructions unavailable. This is sadly not that easy, as we have too much code that still relies on assumptions that such types do not exist. I have an in-progress branch which requires implementing the necessary support for "newtype unpacking" but no time frame for completion.

  4. RalfJung commented on Jul 3, 2017

    @RalfJung
    Member

    Is making types like Cell repr(transparent) (which e.g. rust-lang/rfcs#1789 relies on) part of the implementation of the RFC or would that require its own RFC?

  5. aturon commented on Jul 3, 2017

    @aturon
    ContributorAuthor

    @RalfJung For a change at that level, a PR approved by the libs team suffices.

  6. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jul 27, 2017
  7. hanna-kruppe commented on Dec 16, 2017

    @hanna-kruppe
    Contributor

    The RFC makes contradictory statements about the interaction with repr(align(N)): The Motivation section says

    Given this attribute delegates all representation concerns, no other repr attribute should be present on the type. This means the following definitions are illegal:

    #[repr(transparent, align = "128")]
    struct BogusAlign(f64);
    
    #[repr(transparent, packed)]
    struct BogusPacked(f64);

    ... while the Detailed Design section says:

    This new representation cannot be used with any other representation attribute but alignment, to be able to specify a transparent wrapper with additional alignment constraints:

    #[repr(transparent, align = "128")]
    struct OverAligned(f64); // Behaves as a bare f64 with 128 bits alignment.
    
    #[repr(C, transparent)]
    struct BogusRepr(f64); // Nonsensical, repr cannot be C and transparent.

    AFAICT the RFC initially prohibited repr(transparent, align), then in the discussion some people asked for it to be allowed, but later the rationale for that was called into question and there was no consensus or (explicit) decision. So it's hard to see which alternative was intended to be the final state by the RFC author and the lang team.

  8. eddyb commented on Dec 16, 2017

    @eddyb
    Contributor

    I'd disallow it since it significantly complicates the implementation IMO. Is there any motivation for allowing the combination?

  9. hanna-kruppe commented on Dec 16, 2017

    @hanna-kruppe
    Contributor

    Discussion is mostly rust-lang/rfcs#1758 (comment) plus the immediate replies. The motivation is not immediately clear to me, just that a nested wrapper type with align(N) and no transparent is not equivalent to a single type with repr(align(N), transparent). Perhaps @briansmith or @nox could elaborate?

  10. hanna-kruppe commented on Dec 16, 2017

    @hanna-kruppe
    Contributor

    @nox tells me on IRC they have no use case for it. Let's amend the RFC to consistently reject repr(transparent, align). If someone does find a use case, they can deal with the implementation effort and amend the RFC again.

  11. briansmith commented on Dec 16, 2017

    @briansmith
    Contributor

    Sorry, it's been forever since we had that conversation. Maybe I'm overlooking something, but I think #[repr(transparent, align=16)] would be very common. For example:

    #[repr(transparent, align=16)]
    struct Aes256Key([u8; 32]);
    
    #[repr(transparent, align=16)]
    struct AesNonce([u8; 16]);
    
    // Keep this in sync with the C definition in aes.h
    #[repr(C)]
    struct Aes256Context {
        key: Aes256Key,
        ...
        nonce: AesNonce,
        ...
    }
    
    extern aes256Encrypt(context: &mut Aes256Context, ...);

    I'm not sure how one would express this otherwise.

  12. eddyb commented on Dec 16, 2017

    @eddyb
    Contributor

    Is there any ABI where you need #[repr(transparent)] in conjunction with an aggregate type (e.g. a non-transparent struct, or in your case, an array)?
    AFAIK, none of the ABIs we support today has such a distinction, so #[repr(transparent)] wouldn't really have to do anything to be correct in those cases, but maybe I'm wrong?

    EDIT: Also, are you ever passing or returning Aes256Key or AesNonce by value to/from a function? That's the only time #[repr(transparent)] actually does anything.
    The calling convention part of the ABI is orthogonal to the memory layout, and the latter is identical between a struct with one field and the field itself, pretty much everywhere.

  13. 63 remaining items

  14. SimonSapin commented on Jun 26, 2018

    @SimonSapin
    Contributor

    Oops, this should have been closed by #51562.

  15. added 2 commits that reference this issue on Jul 3, 2018
  16. RReverser commented on Aug 5, 2018

    @RReverser
    Contributor

    Could I ask here for some eyes on a suggestion to add #[repr(transparent)] to Box<T> and whether there are any obvious downsides? Thanks! #52976

  17. added a commit that references this issue on Sep 6, 2022
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

    B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions