-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
stabilize size_of_val_raw, align_of_val_raw, Layout::for_value_raw #157572
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,29 +230,33 @@ impl Layout { | |
| /// | ||
| /// - If `T` is `Sized`, this function is always safe to call. | ||
| /// - If the unsized tail of `T` is: | ||
| /// - a [slice], then the length of the slice tail must be an initialized | ||
| /// integer, and the size of the *entire value* | ||
| /// - a [slice] `[U]`, `str`, or a [trait object] `dyn Trait`, then the size of the *entire value* | ||
| /// (dynamic tail length + statically sized prefix) must fit in `isize`. | ||
| /// For the special case where the dynamic tail length is 0, this function | ||
| /// is safe to call. | ||
| /// - a [trait object], then the vtable part of the pointer must point | ||
| /// to a valid vtable for the type `T` acquired by an unsizing coercion, | ||
| /// and the size of the *entire value* | ||
| /// (dynamic tail length + statically sized prefix) must fit in `isize`. | ||
| /// - an (unstable) [extern type], then this function is always safe to | ||
| /// call, but may panic or otherwise return the wrong value, as the | ||
| /// extern type's layout is not known. This is the same behavior as | ||
| /// [`Layout::for_value`] on a reference to an extern type tail. | ||
| /// - otherwise, it is conservatively not allowed to call this function. | ||
| // NOTE: the reason this is safe is that if an overflow were to occur already with size 0, | ||
| // then we would stop compilation as even the "statically known" part of the type would | ||
| // already be too big (or the call may be in dead code and optimized away, but then it | ||
| // doesn't matter). | ||
| /// - No other kind of unsized tail currently exists that satisfies the trait bounds for this | ||
| /// function. If more kinds of unsized tails get introduced in the future, the documentation | ||
| /// of this function will have to be extended before it can be used for such types. | ||
| /// | ||
| /// Here, *unsized tail* refers to the type obtained by recursively descending through the last | ||
| /// field of a tuple or struct until we arrived at a built-in unsized type. | ||
| /// | ||
| /// As a consequence of these rules, it is the case that whenever it is allowed to convert `val` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument is |
||
| /// into a shared reference, then it is also allowed to invoke this function. | ||
| /// | ||
| /// [trait object]: ../../book/ch17-02-trait-objects.html | ||
| /// [extern type]: ../../unstable-book/language-features/extern-types.html | ||
| #[unstable(feature = "layout_for_ptr", issue = "69835")] | ||
| #[stable(feature = "layout_for_ptr", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_const_stable(feature = "layout_for_ptr", since = "CURRENT_RUSTC_VERSION")] | ||
| #[must_use] | ||
| #[inline] | ||
| pub const unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self { | ||
| pub const unsafe fn for_value_raw<T: ?Sized>(val: *const T) -> Self { | ||
| // SAFETY: we pass along the prerequisites of these functions to the caller | ||
| let (size, alignment) = unsafe { (mem::size_of_val_raw(t), Alignment::of_val_raw(t)) }; | ||
| let (size, alignment) = unsafe { (mem::size_of_val_raw(val), Alignment::of_val_raw(val)) }; | ||
| // SAFETY: see rationale in `new` for why this is using the unsafe variant | ||
| unsafe { Layout::from_size_alignment_unchecked(size, alignment) } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/tools/miri/tests/pass/issues/issue-3200-packed-field-offset.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #![feature(layout_for_ptr)] | ||
| use std::mem; | ||
|
|
||
| #[repr(packed, C)] | ||
|
|
||
1 change: 0 additions & 1 deletion
1
src/tools/miri/tests/pass/issues/issue-3200-packed2-field-offset.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #![feature(layout_for_ptr)] | ||
| use std::mem; | ||
|
|
||
| #[repr(packed(4))] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| #![feature(ptr_metadata, layout_for_ptr)] | ||
| #![feature(ptr_metadata)] | ||
|
|
||
| use std::{mem, ptr}; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| //@ run-pass | ||
|
|
||
| #![feature(layout_for_ptr)] | ||
|
|
||
| use std::{mem, ptr}; | ||
|
|
||
| struct Foo(#[allow(dead_code)] u32); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //@ run-pass | ||
| #![feature(layout_for_ptr)] | ||
| use std::mem; | ||
|
|
||
| #[repr(packed(4))] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //@ run-pass | ||
| #![feature(layout_for_ptr)] | ||
| use std::mem; | ||
|
|
||
| #[repr(packed, C)] | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tuples with unsized tails were removed from the language in #137728.
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errr ... actually forget about that. We just removed the coercion and the promise that
(i32, i32)and(i32, dyn Send)have compatible layouts, but(i32, dyn Send)is still technically a type that exists in the language.So forget about this comment.