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

Relax safety precondition of Ptr::cast_unsized #999

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 16 additions & 10 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ mod def {
/// 10. During the lifetime 'a, no code will load or store this memory
/// region treating `UnsafeCell`s as existing at different ranges
/// than they exist in `T`.
/// 11. During the lifetime 'a, no reference will exist to this memory
/// which treats `UnsafeCell`s as existing at different ranges than
/// they exist in `T`.
/// 11. During the lifetime 'a, no live reference or live `Ptr` will
/// exist to this memory which treats `UnsafeCell`s as existing at
/// different ranges than they exist in `T`.
// SAFETY: `NonNull<T>` is covariant over `T` [1].
//
// [1]: https://doc.rust-lang.org/std/ptr/struct.NonNull.html
Expand Down Expand Up @@ -918,8 +918,9 @@ mod _casts {
/// For all kinds of casts, the caller must promise that:
/// - the the size of the object referenced by the resulting pointer is
/// less than or equal to the size of the object referenced by `self`.
/// - `UnsafeCell`s in `U` exist at ranges identical to those at which
/// `UnsafeCell`s exist in `T`.
/// - if the aliasing of `self` is not `Exclusive`, that `UnsafeCell`s
/// in `U` exist at ranges identical to those at which `UnsafeCell`s
/// exist in `T`.
///
/// For pointer-to-pointer casts, it suffices for the caller to
/// additionally promise that `cast(p)` is implemented as:
Expand Down Expand Up @@ -983,7 +984,11 @@ mod _casts {
// store this memory region treating `UnsafeCell`s as existing at
// different ranges than they exist in `U`. This is true by
// invariant on Ptr<'a, T, I>, and preserved through the cast to
// `U` by contract on the caller.
// `U` by contract on the caller:
// - If `ptr` is exclusively aliased, no other live references
// exist.
// - If `ptr` has shared aliasing, the caller has promised that
// `T` and `U` have `UnsafeCell`s at exactly the same ranges.
// 10. See 9.
unsafe { Ptr::new(ptr) }
}
Expand Down Expand Up @@ -1244,10 +1249,11 @@ mod _project {
/// `self` casted to a raw pointer. The pointer it returns must
/// reference only a subset of `self`'s bytes.
/// - `T` is a struct or union type.
/// - The projected pointer contains `UnsafeCell`s at exactly the same
/// ranges at which `UnsafeCell`s appear in the projected-from type.
/// This is necessarily true for projections of struct fields, but not
/// for all projections of union fields.
/// - If the aliasing of `self` is not `Exclusive`, projected pointer
/// must contain `UnsafeCell`s at exactly the same ranges at which
/// `UnsafeCell`s appear in the projected-from type. This is
/// necessarily true for projections of struct fields, but not for all
/// projections of union fields.
///
/// ## Postconditions
///
Expand Down
Loading