Skip to content

Commit ae43fce

Browse files
authored
[pointer] Rename Any -> Unknown/Inaccessible (#1909)
For aliasing, use `Inaccessible`. For alignment and validity, use `Unknown`.
1 parent 85a12e2 commit ae43fce

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

src/pointer/invariant.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,26 @@ pub trait Validity: Sealed {}
8181
/// Exclusive`.
8282
pub trait Reference: Aliasing + Sealed {}
8383

84-
/// No requirement - any invariant is allowed.
85-
pub enum Any {}
86-
impl Aliasing for Any {
84+
/// It is unknown whether any invariant holds.
85+
pub enum Unknown {}
86+
87+
impl Alignment for Unknown {}
88+
impl Validity for Unknown {}
89+
90+
/// The `Ptr<'a, T>` does not permit any reads or writes from or to its referent.
91+
pub enum Inaccessible {}
92+
93+
impl Aliasing for Inaccessible {
8794
const IS_EXCLUSIVE: bool = false;
8895

89-
// SAFETY: Since we don't know what aliasing model this is, we have to be
90-
// conservative. Invariance is strictly more restrictive than any other
91-
// variance model, so this can never cause soundness issues.
92-
//
93-
// `fn() -> T` and `fn(T) -> ()` are covariant and contravariant in `T`,
94-
// respectively. [1] Thus, `fn(T) -> T` is invariant in `T`. Thus, `fn(&'a
95-
// T) -> &'a T` is invariant in `'a` and `T`.
96+
// SAFETY: Inaccessible `Ptr`s permit neither reads nor writes, and so it
97+
// doesn't matter how long the referent actually lives. Thus, covariance is
98+
// fine (and is chosen because it is maximally permissive). Shared
99+
// references are covariant [1].
96100
//
97101
// [1] https://doc.rust-lang.org/1.81.0/reference/subtyping.html#variance
98-
type Variance<'a, T: 'a + ?Sized> = fn(&'a T) -> &'a T;
102+
type Variance<'a, T: 'a + ?Sized> = &'a T;
99103
}
100-
impl Alignment for Any {}
101-
impl Validity for Any {}
102104

103105
/// The `Ptr<'a, T>` adheres to the aliasing rules of a `&'a T`.
104106
///
@@ -212,8 +214,9 @@ mod sealed {
212214

213215
pub trait Sealed {}
214216

215-
impl Sealed for Any {}
217+
impl Sealed for Unknown {}
216218

219+
impl Sealed for Inaccessible {}
217220
impl Sealed for Shared {}
218221
impl Sealed for Exclusive {}
219222

src/pointer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ use crate::Unaligned;
2424
/// to [`TryFromBytes::is_bit_valid`].
2525
///
2626
/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
27-
pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Any> =
27+
pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unknown> =
2828
Ptr<'a, T, (Aliasing, Alignment, invariant::Initialized)>;
2929

3030
/// A semi-user-facing wrapper type representing a maybe-aligned reference, for
3131
/// use in [`TryFromBytes::is_bit_valid`].
3232
///
3333
/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
34-
pub type MaybeAligned<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Any> =
34+
pub type MaybeAligned<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unknown> =
3535
Ptr<'a, T, (Aliasing, Alignment, invariant::Valid)>;
3636

3737
// These methods are defined on the type alias, `MaybeAligned`, so as to bring

src/pointer/ptr.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ mod _transitions {
676676
#[doc(hidden)]
677677
#[must_use]
678678
#[inline]
679-
pub const fn forget_aligned(self) -> Ptr<'a, T, I::WithAlignment<Any>> {
680-
// SAFETY: `Any` is less restrictive than `Aligned`.
679+
pub const fn forget_aligned(self) -> Ptr<'a, T, I::WithAlignment<Unknown>> {
680+
// SAFETY: `Unknown` is less restrictive than `Aligned`.
681681
unsafe { self.assume_invariants() }
682682
}
683683
}
@@ -706,15 +706,14 @@ mod _casts {
706706
/// following properties:
707707
/// - `u` addresses a subset of the bytes addressed by `p`
708708
/// - `u` has the same provenance as `p`
709-
/// - If `I::Aliasing` is [`Any`] or [`Shared`], `UnsafeCell`s in `*u`
710-
/// must exist at ranges identical to those at which `UnsafeCell`s
711-
/// exist in `*p`
709+
/// - If `I::Aliasing` is [`Shared`], `UnsafeCell`s in `*u` must exist
710+
/// at ranges identical to those at which `UnsafeCell`s exist in `*p`
712711
#[doc(hidden)]
713712
#[inline]
714713
pub unsafe fn cast_unsized_unchecked<U: 'a + ?Sized, F: FnOnce(*mut T) -> *mut U>(
715714
self,
716715
cast: F,
717-
) -> Ptr<'a, U, (I::Aliasing, Any, Any)> {
716+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Unknown)> {
718717
let ptr = cast(self.as_inner().as_non_null().as_ptr());
719718

720719
// SAFETY: Caller promises that `cast` returns a pointer whose
@@ -768,9 +767,11 @@ mod _casts {
768767
// pointer will permit mutation of this byte during `'a`, by
769768
// invariant on `self`, no other code assumes that this will
770769
// not happen.
770+
// - `Inaccessible`: There are no restrictions we need to uphold.
771771
// 7. `ptr`, trivially, conforms to the alignment invariant of
772-
// `Any`.
773-
// 8. `ptr`, trivially, conforms to the validity invariant of `Any`.
772+
// `Unknown`.
773+
// 8. `ptr`, trivially, conforms to the validity invariant of
774+
// `Unknown`.
774775
unsafe { Ptr::new(ptr) }
775776
}
776777

@@ -784,7 +785,10 @@ mod _casts {
784785
/// - `u` has the same provenance as `p`
785786
#[doc(hidden)]
786787
#[inline]
787-
pub unsafe fn cast_unsized<U, F, R, S>(self, cast: F) -> Ptr<'a, U, (I::Aliasing, Any, Any)>
788+
pub unsafe fn cast_unsized<U, F, R, S>(
789+
self,
790+
cast: F,
791+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Unknown)>
788792
where
789793
T: Read<I::Aliasing, R>,
790794
U: 'a + ?Sized + Read<I::Aliasing, S>,
@@ -927,10 +931,11 @@ mod _casts {
927931
// 0. Since `U: Read<I::Aliasing, _>`, either:
928932
// - `I::Aliasing` is `Exclusive`, in which case both `src` and
929933
// `ptr` conform to `Exclusive`
930-
// - `I::Aliasing` is `Shared` or `Any` and `U` is `Immutable`
931-
// (we already know that `[u8]: Immutable`). In this case,
932-
// neither `U` nor `[u8]` permit mutation, and so `Shared`
933-
// aliasing is satisfied.
934+
// - `I::Aliasing` is `Shared` or `Inaccessible` and `U` is
935+
// `Immutable` (we already know that `[u8]: Immutable`). In
936+
// this case, neither `U` nor `[u8]` permit mutation, and so
937+
// `Shared` aliasing is satisfied. `Inaccessible` is trivially
938+
// satisfied since it imposes no requirements.
934939
// 1. `ptr` conforms to the alignment invariant of `Aligned` because
935940
// it is derived from `try_cast_into`, which promises that the
936941
// object described by `target` is validly aligned for `U`.
@@ -1070,7 +1075,7 @@ mod _project {
10701075
pub unsafe fn project<U: 'a + ?Sized>(
10711076
self,
10721077
projector: impl FnOnce(*mut T) -> *mut U,
1073-
) -> Ptr<'a, U, (I::Aliasing, Any, Initialized)> {
1078+
) -> Ptr<'a, U, (I::Aliasing, Unknown, Initialized)> {
10741079
// TODO(#1122): If `cast_unsized` were able to reason that, when
10751080
// casting from an `Initialized` pointer, the result is another
10761081
// `Initialized` pointer, we could remove this method entirely.

src/util/macro_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub unsafe fn transmute_mut<'dst, 'src: 'dst, Src: 'src, Dst: 'dst>(
520520
fn try_cast_or_pme<Src, Dst, I, R>(
521521
src: Ptr<'_, Src, I>,
522522
) -> Result<
523-
Ptr<'_, Dst, (I::Aliasing, invariant::Any, invariant::Valid)>,
523+
Ptr<'_, Dst, (I::Aliasing, invariant::Unknown, invariant::Valid)>,
524524
ValidityError<Ptr<'_, Src, I>, Dst>,
525525
>
526526
where

src/util/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ impl<I: invariant::Validity> ValidityVariance<I> for Covariant {
9999
pub enum Invariant {}
100100

101101
impl<I: invariant::Alignment> AlignmentVariance<I> for Invariant {
102-
type Applied = invariant::Any;
102+
type Applied = invariant::Unknown;
103103
}
104104

105105
impl<I: invariant::Validity> ValidityVariance<I> for Invariant {
106-
type Applied = invariant::Any;
106+
type Applied = invariant::Unknown;
107107
}
108108

109109
// SAFETY:

0 commit comments

Comments
 (0)