Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guarantee char layout #1401

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/types/textual.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause
Since `str` is a [dynamically sized type], it can only be instantiated through a
pointer type, such as `&str`.

## Bit validity
## Layout and bit validity

`char` is guaranteed to have the same size and alignment as `u32` on all platforms.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`char` is guaranteed to have the same size and alignment as `u32` on all platforms.
`char` is guaranteed to have the same size and alignment as `u32` on all platforms.
`Option<char>` is guaranteed to have the same size and alignment as `char` on all platforms.

(Unless we want to put this somewhere else.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion should probably be handled separately, not in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #1426 to track this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I was surprised this was to the reference directly, not to core like the other guarantees have been done. We currently have no guarantees written for specific alignment of anything, as far as I could find.

I was going to suggest something like

/// ```
///     use std::alloc::Layout;
///     assert_eq!(Layout::new::<char>(), Layout::new::<u32>());
///     assert_eq!(Layout::new::<Option<char>>(), Layout::new::<u32>());
/// ```

but then this wasn't a docs change...

Copy link
Contributor Author

@joshlf joshlf Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No opposition to this going in core as well of course, but we already have text in the Reference about the sizes of primitive types: https://doc.rust-lang.org/reference/type-layout.html#primitive-data-layout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put up a PR to add the same text to core: rust-lang/rust#116894


Every byte of a `char` is guaranteed to be initialized (in other words,
`transmute::<char, [u8; size_of::<char>()]>(...)` is always sound -- but since
Expand Down