[RFC, V2] exclude packed attr for types that contain aligned types when possible #2770
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NOTE: I know this can use some cleanups and more documentation / comments, etc. but for now I'm just uploading it as an RFC to see if you like the general approach. If you do, then I'll clean it up, but if not I won't bother.
This patch aims to work around a limitation of rustc in which it will not compile types with a
packed
attr that contain types with analign(N)
attr. If thepacked
attr on the outer/parent type is redundant, and can be removed without changing the type's layout, then this patch will remove that attr so that the type can be compiled under rustc. If the type'spacked
attr cannot be removed without changing the layout, then it will be left alone.Handling this correctly requires a change to when bindgen determines whether a type requires an
align
attr. Currently, this happens during the code generation phase. However, we cannot in general assume anything about the order in which code is generated for types, and in particular codegen may occur for a parent type before codegen for all child types contained in that parent. However, in order to strip thepacked
attr in cases where we want to do so, we already need to know about the alignment of all children.In order to make that possible, this patch moves the logic to check if a type requires an explicit
align
attr earlier. This now happens before codegen starts for the first type, so we can use information about the alignment of all child types to determine whether to place apacked
attr on any given type.