@@ -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,34 @@ 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`.
1016
+ /// `slot` is a pointer valid for writes and any value written to it is pinned.
1003
1017
$pvis unsafe fn $p_field<E >(
1004
1018
self ,
1005
1019
slot: * mut $p_type,
1006
1020
init: impl $crate:: PinInit <$p_type, E >,
1007
1021
) -> :: core:: result:: Result <( ) , E > {
1008
- // SAFETY: TODO .
1022
+ // SAFETY: `slot` points to valid, uninitialized memory for a `$p_type` .
1009
1023
unsafe { $crate:: PinInit :: __pinned_init( init, slot) }
1010
1024
}
1011
1025
) *
1012
1026
$(
1013
1027
$( #[ $( $attr) * ] ) *
1028
+ /// # Safety
1029
+ ///
1030
+ /// The caller must ensure that `slot` is a valid pointer to uninitialized memory
1031
+ /// that is properly aligned and large enough to hold a value of type `$type`.
1032
+ /// The memory at `slot` must also be valid for writes.
1033
+ /// `slot` is valid for writes.
1014
1034
$fvis unsafe fn $field<E >(
1015
1035
self ,
1016
1036
slot: * mut $type,
1017
1037
init: impl $crate:: Init <$type, E >,
1018
1038
) -> :: core:: result:: Result <( ) , E > {
1019
- // SAFETY: TODO .
1039
+ // SAFETY: `slot` points to valid, uninitialized memory for a `$type` .
1020
1040
unsafe { $crate:: Init :: __init( init, slot) }
1021
1041
}
1022
1042
) *
@@ -1132,7 +1152,11 @@ macro_rules! __init_internal {
1132
1152
struct __InitOk;
1133
1153
// Get the data about fields from the supplied type.
1134
1154
//
1135
- // SAFETY: TODO.
1155
+ // SAFETY: The `$get_data()` call (which resolves to either `HasPinData::__pin_data()`
1156
+ // or `HasInitData::__init_data()`) is safe because this macro is part of the pin-init
1157
+ // crate, satisfying the safety requirement that these methods should only be called by
1158
+ // macros in the pin-init crate (as documented in their respective `# Safety` sections).
1159
+ // The `paste!` macro is used for re-tokenization and does not affect this safety argument.
1136
1160
let data = unsafe {
1137
1161
use $crate:: __internal:: $has_data;
1138
1162
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1188,7 +1212,12 @@ macro_rules! __init_internal {
1188
1212
let init = move |slot| -> :: core:: result:: Result <( ) , $err> {
1189
1213
init( slot) . map( |__InitOk| ( ) )
1190
1214
} ;
1191
- // SAFETY: TODO.
1215
+ // SAFETY: The `init` closure, generated by this macro, upholds the contract of
1216
+ // `$crate::$construct_closure`. It ensures `slot` is fully initialized on `Ok(())`
1217
+ // or properly cleaned (via `DropGuard`s) on `Err(err)`, leaving `slot` safe to
1218
+ // deallocate. Pinning invariants are maintained using `PinData`/`InitData` methods
1219
+ // for field initialization, and `slot` is not moved (for `!Unpin` types) as it's
1220
+ // accessed via a pointer.
1192
1221
let init = unsafe { $crate:: $construct_closure:: <_, $err>( init) } ;
1193
1222
init
1194
1223
} } ;
@@ -1338,7 +1367,8 @@ macro_rules! __init_internal {
1338
1367
// Since we are in the closure that is never called, this will never get executed.
1339
1368
// We abuse `slot` to get the correct type inference here:
1340
1369
//
1341
- // SAFETY: TODO.
1370
+ // SAFETY: This is unreachable code that is used solely for compile-time type checking,
1371
+ // it is never executed.
1342
1372
unsafe {
1343
1373
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
1344
1374
// information that is associated to already parsed fragments, so a path fragment
0 commit comments