Skip to content

#[repr(align(8))] is not accepted for arrays #83595

Closed

Description

@manuthambi

Code

struct Data {
    #[repr(align(8))]
    x: [usize; 20]
}

#[repr(align)] was accepted for arrays in rust v1.50, but is rejected in v1.51.

error[E0517]: attribute should be applied to a struct, enum, or union
 --> src/main.rs:3:12
  |
3 |     #[repr(align(8))]
  |            ^^^^^^^^
4 |     x: [usize; 20]
  |     -------------- not a struct, enum, or union

I am not entirely sure whether the attribute actually did the alignment in 1.50, or the compiler just ignored it. Regardless, is this expected behavior, and if it is how do I align an array?

Activity

  1. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Mar 28, 2021
  2. ehuss commented on Mar 28, 2021

    @ehuss
    Contributor

    Extra validation was added for situations where attributes were ignored (in this case it was #80641). Previous versions issued an unused-attribute warning.

    If you want to align an array, I think you can wrap it in a struct with #[repr(align(8))] struct Arr([usize; 20]).

  3. added
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Mar 28, 2021
  4. camelid commented on Mar 28, 2021

    @camelid
    Member

    Nominating to see if this was an intended "lint -> hard error" change.

    (If there's another way to ask if a change is intended that people on T-compiler prefer, please let me know :)

    @rustbot label: +I-nominated

  5. rustbot commented on Mar 28, 2021

    @rustbot
  6. rustbot commented on Mar 28, 2021

    @rustbot
  7. added
    T-langRelevant to the language team
    and removed
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 28, 2021
  8. manuthambi commented on Mar 30, 2021

    @manuthambi
    Author

    Extra validation was added for situations where attributes were ignored (in this case it was #80641). Previous versions issued an unused-attribute warning.

    1.50 didn't warn I think. I haven't specifically configured unusued_attributes in my project. Anyways, it is a good idea to lint/error for unused attributes.

    If you want to align an array, I think you can wrap it in a struct with #[repr(align(8))] struct Arr([usize; 20]).

    That works, if the compiler guarantees that there will not be any padding at the beginning of Arr struct. Does it?

  9. scottmcm commented on Apr 20, 2021

    @scottmcm
    Member

    I am not entirely sure whether the attribute actually did the alignment in 1.50, or the compiler just ignored it.

    It just ignored it: https://rust.godbolt.org/z/4rso564PP

    (Note also the warning for "#[warn(unused_attributes)] on by default".)

    It's my understanding that this is known and expected fallout from fixing compiler bugs where it had previously forgotten to run validation in certain positions.

  10. modified the milestones: 1.52.0, 1.51.0 on Apr 27, 2021
  11. scottmcm commented on Apr 27, 2021

    @scottmcm
    Member

    We discussed this in the lang team meeting today.

    This is an intentional bugfix to resolve a place where attributes weren't being checked but were supposed to have been. Note that this never did anything, so any unsafe code that was expecting this to align things is potentially unsound.

    (There have been a few of these, recently -- for example 1.49 fixed this on enum variants.)


    repr(Rust) has very few guarantees. If you strictly need no padding at the beginning, you can use #[repr(C, align(8))] on the struct: https://rust.godbolt.org/z/x6648cWrE

  12. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Apr 27, 2021
  13. camelid commented on Apr 27, 2021

    @camelid
    Member

    Should I-nominated be removed now that the issue is closed?

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

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsT-langRelevant to the language teamregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions