@@ -518,7 +518,11 @@ macro_rules! __pinned_drop {
518
518
}
519
519
) ,
520
520
) => {
521
- // SAFETY: TODO.
521
+ // SAFETY: This macro generates the `unsafe impl PinnedDrop`. This is safe as it's
522
+ // an internal part of the `#[pinned_drop]` proc-macro, which ensures correct
523
+ // transformation of the user's `impl`. The added `OnlyCallFromDrop` token restricts
524
+ // `PinnedDrop::drop` calls to the actual drop process (via the `Drop::drop` impl
525
+ // generated by `#[pin_data(PinnedDrop)]`) where `self` is pinned and valid.
522
526
unsafe $( $impl_sig) * {
523
527
// Inherit all attributes and the type/ident tokens for the signature.
524
528
$( #[ $( $attr) * ] ) *
@@ -878,7 +882,12 @@ macro_rules! __pin_data {
878
882
}
879
883
}
880
884
881
- // SAFETY: TODO.
885
+ // SAFETY: The `__pin_data` macro ensures this `impl PinData` for `__ThePinData` is
886
+ // safe by guaranteeing that `__ThePinData` is `Copy`, `PinData::Datee` is correctly
887
+ // set to `$name` (which itself implements `HasPinData<PinData = __ThePinData>`), and
888
+ // that all generated field accessor methods on `__ThePinData` are sound, uphold their
889
+ // individual safety contracts (such as ensuring valid `slot` pointers), and correctly
890
+ // use either `$crate::PinInit::__pinned_init` or `$crate::Init::__init`.
882
891
unsafe impl <$( $impl_generics) * >
883
892
$crate:: __internal:: PinData for __ThePinData<$( $ty_generics) * >
884
893
where $( $whr) *
@@ -1000,23 +1009,32 @@ macro_rules! __pin_data {
1000
1009
{
1001
1010
$(
1002
1011
$( #[ $( $p_attr) * ] ) *
1012
+ /// # Safety
1013
+ ///
1014
+ /// The caller must ensure that `slot` is a valid pointer to uninitialized memory
1015
+ /// that is properly aligned and large enough to hold a value of type `$p_type`.
1003
1016
$pvis unsafe fn $p_field<E >(
1004
1017
self ,
1005
1018
slot: * mut $p_type,
1006
1019
init: impl $crate:: PinInit <$p_type, E >,
1007
1020
) -> :: core:: result:: Result <( ) , E > {
1008
- // SAFETY: TODO .
1021
+ // SAFETY: `slot` points to valid, uninitialized memory for a `$p_type` .
1009
1022
unsafe { $crate:: PinInit :: __pinned_init( init, slot) }
1010
1023
}
1011
1024
) *
1012
1025
$(
1013
1026
$( #[ $( $attr) * ] ) *
1027
+ /// # Safety
1028
+ ///
1029
+ /// The caller must ensure that `slot` is a valid pointer to uninitialized memory
1030
+ /// that is properly aligned and large enough to hold a value of type `$type`.
1031
+ /// The memory at `slot` must also be valid for writes.
1014
1032
$fvis unsafe fn $field<E >(
1015
1033
self ,
1016
1034
slot: * mut $type,
1017
1035
init: impl $crate:: Init <$type, E >,
1018
1036
) -> :: core:: result:: Result <( ) , E > {
1019
- // SAFETY: TODO .
1037
+ // SAFETY: `slot` points to valid, uninitialized memory for a `$type` .
1020
1038
unsafe { $crate:: Init :: __init( init, slot) }
1021
1039
}
1022
1040
) *
@@ -1132,7 +1150,12 @@ macro_rules! __init_internal {
1132
1150
struct __InitOk;
1133
1151
// Get the data about fields from the supplied type.
1134
1152
//
1135
- // SAFETY: TODO.
1153
+ // SAFETY: The `$get_data()` call (which resolves to either `HasPinData::__pin_data()`
1154
+ // or `HasInitData::__init_data()`) is safe because the `#[pin_data]` macro, when applied
1155
+ // to type `$t`, correctly implements the respective trait. This ensures that the returned
1156
+ // metadata accurately reflects `$t`'s structure and pinning properties, fulfilling the
1157
+ // `# Safety` contract of the called `__pin_data()` or `__init_data()` method.
1158
+ // The `paste!` macro is used for re-tokenization and does not affect this safety argument.
1136
1159
let data = unsafe {
1137
1160
use $crate:: __internal:: $has_data;
1138
1161
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1188,7 +1211,12 @@ macro_rules! __init_internal {
1188
1211
let init = move |slot| -> :: core:: result:: Result <( ) , $err> {
1189
1212
init( slot) . map( |__InitOk| ( ) )
1190
1213
} ;
1191
- // SAFETY: TODO.
1214
+ // SAFETY: The `init` closure, generated by this macro, upholds the contract of
1215
+ // `$crate::$construct_closure`. It ensures `slot` is fully initialized on `Ok(())`
1216
+ // or properly cleaned (via `DropGuard`s) on `Err(err)`, leaving `slot` safe to
1217
+ // deallocate. Pinning invariants are maintained using `PinData`/`InitData` methods
1218
+ // for field initialization, and `slot` is not moved (for `!Unpin` types) as it's
1219
+ // accessed via a pointer.
1192
1220
let init = unsafe { $crate:: $construct_closure:: <_, $err>( init) } ;
1193
1221
init
1194
1222
} } ;
@@ -1338,7 +1366,8 @@ macro_rules! __init_internal {
1338
1366
// Since we are in the closure that is never called, this will never get executed.
1339
1367
// We abuse `slot` to get the correct type inference here:
1340
1368
//
1341
- // SAFETY: TODO.
1369
+ // SAFETY: This is unreachable code that is used solely for compile-time type checking,
1370
+ // it is never executed.
1342
1371
unsafe {
1343
1372
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
1344
1373
// information that is associated to already parsed fragments, so a path fragment
0 commit comments