-
Notifications
You must be signed in to change notification settings - Fork 13
add unfinished safety comments for initialization and pinning macros #61
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
Open
liamjdavis
wants to merge
1
commit into
Rust-for-Linux:main
Choose a base branch
from
liamjdavis:liamjdavis/incomplete-safety-documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -518,7 +518,11 @@ macro_rules! __pinned_drop { | |||||||||
} | ||||||||||
), | ||||||||||
) => { | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: This macro generates the `unsafe impl PinnedDrop`. This is safe as it's | ||||||||||
// an internal part of the `#[pinned_drop]` proc-macro, which ensures correct | ||||||||||
// transformation of the user's `impl`. The added `OnlyCallFromDrop` token restricts | ||||||||||
// `PinnedDrop::drop` calls to the actual drop process (via the `Drop::drop` impl | ||||||||||
// generated by `#[pin_data(PinnedDrop)]`) where `self` is pinned and valid. | ||||||||||
unsafe $($impl_sig)* { | ||||||||||
// Inherit all attributes and the type/ident tokens for the signature. | ||||||||||
$(#[$($attr)*])* | ||||||||||
|
@@ -878,7 +882,12 @@ macro_rules! __pin_data { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: The `__pin_data` macro ensures this `impl PinData` for `__ThePinData` is | ||||||||||
// safe by guaranteeing that `__ThePinData` is `Copy`, `PinData::Datee` is correctly | ||||||||||
// set to `$name` (which itself implements `HasPinData<PinData = __ThePinData>`), and | ||||||||||
// that all generated field accessor methods on `__ThePinData` are sound, uphold their | ||||||||||
// individual safety contracts (such as ensuring valid `slot` pointers), and correctly | ||||||||||
// use either `$crate::PinInit::__pinned_init` or `$crate::Init::__init`. | ||||||||||
unsafe impl<$($impl_generics)*> | ||||||||||
$crate::__internal::PinData for __ThePinData<$($ty_generics)*> | ||||||||||
where $($whr)* | ||||||||||
|
@@ -1000,23 +1009,34 @@ macro_rules! __pin_data { | |||||||||
{ | ||||||||||
$( | ||||||||||
$(#[$($p_attr)*])* | ||||||||||
/// # Safety | ||||||||||
/// | ||||||||||
/// The caller must ensure that `slot` is a valid pointer to uninitialized memory | ||||||||||
/// that is properly aligned and large enough to hold a value of type `$p_type`. | ||||||||||
Comment on lines
+1014
to
+1015
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.
Suggested change
|
||||||||||
/// `slot` is a pointer valid for writes and any value written to it is pinned. | ||||||||||
$pvis unsafe fn $p_field<E>( | ||||||||||
self, | ||||||||||
slot: *mut $p_type, | ||||||||||
init: impl $crate::PinInit<$p_type, E>, | ||||||||||
) -> ::core::result::Result<(), E> { | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: `slot` points to valid, uninitialized memory for a `$p_type`. | ||||||||||
BennoLossin marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
unsafe { $crate::PinInit::__pinned_init(init, slot) } | ||||||||||
} | ||||||||||
)* | ||||||||||
$( | ||||||||||
$(#[$($attr)*])* | ||||||||||
/// # Safety | ||||||||||
/// | ||||||||||
/// The caller must ensure that `slot` is a valid pointer to uninitialized memory | ||||||||||
/// that is properly aligned and large enough to hold a value of type `$type`. | ||||||||||
/// The memory at `slot` must also be valid for writes. | ||||||||||
Comment on lines
+1030
to
+1032
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.
Suggested change
|
||||||||||
/// `slot` is valid for writes. | ||||||||||
$fvis unsafe fn $field<E>( | ||||||||||
self, | ||||||||||
slot: *mut $type, | ||||||||||
init: impl $crate::Init<$type, E>, | ||||||||||
) -> ::core::result::Result<(), E> { | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: `slot` points to valid, uninitialized memory for a `$type`. | ||||||||||
unsafe { $crate::Init::__init(init, slot) } | ||||||||||
} | ||||||||||
)* | ||||||||||
|
@@ -1132,7 +1152,11 @@ macro_rules! __init_internal { | |||||||||
struct __InitOk; | ||||||||||
// Get the data about fields from the supplied type. | ||||||||||
// | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: The `$get_data()` call (which resolves to either `HasPinData::__pin_data()` | ||||||||||
// or `HasInitData::__init_data()`) is safe because this macro is part of the pin-init | ||||||||||
// crate, satisfying the safety requirement that these methods should only be called by | ||||||||||
// macros in the pin-init crate (as documented in their respective `# Safety` sections). | ||||||||||
// The `paste!` macro is used for re-tokenization and does not affect this safety argument. | ||||||||||
let data = unsafe { | ||||||||||
use $crate::__internal::$has_data; | ||||||||||
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal | ||||||||||
|
@@ -1188,7 +1212,12 @@ macro_rules! __init_internal { | |||||||||
let init = move |slot| -> ::core::result::Result<(), $err> { | ||||||||||
init(slot).map(|__InitOk| ()) | ||||||||||
}; | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: The `init` closure, generated by this macro, upholds the contract of | ||||||||||
// `$crate::$construct_closure`. It ensures `slot` is fully initialized on `Ok(())` | ||||||||||
// or properly cleaned (via `DropGuard`s) on `Err(err)`, leaving `slot` safe to | ||||||||||
// deallocate. Pinning invariants are maintained using `PinData`/`InitData` methods | ||||||||||
// for field initialization, and `slot` is not moved (for `!Unpin` types) as it's | ||||||||||
// accessed via a pointer. | ||||||||||
let init = unsafe { $crate::$construct_closure::<_, $err>(init) }; | ||||||||||
init | ||||||||||
}}; | ||||||||||
|
@@ -1338,7 +1367,8 @@ macro_rules! __init_internal { | |||||||||
// Since we are in the closure that is never called, this will never get executed. | ||||||||||
// We abuse `slot` to get the correct type inference here: | ||||||||||
// | ||||||||||
// SAFETY: TODO. | ||||||||||
// SAFETY: This is unreachable code that is used solely for compile-time type checking, | ||||||||||
// it is never executed. | ||||||||||
unsafe { | ||||||||||
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal | ||||||||||
// information that is associated to already parsed fragments, so a path fragment | ||||||||||
|
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
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
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.
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.
Please add a
# Safety
section on this function.