Skip to content

Commit 4d09d7c

Browse files
committed
Remove obsolete safety proofs
In #2408, we simplified the safety precondition of `unsafe_impl!`, but did not remove safety proofs at call sites made redundant by that simplification. This commit removes those now-obsolete proofs. gherrit-pr-id: I70d5aa5ace6bd2e39e679eac7f00a66d4b843d57
1 parent 6d4d811 commit 4d09d7c

File tree

2 files changed

+11
-78
lines changed

2 files changed

+11
-78
lines changed

src/impls.rs

+11-72
Original file line numberDiff line numberDiff line change
@@ -100,32 +100,14 @@ safety_comment! {
100100
unsafe_impl!(bool: Immutable, FromZeros, IntoBytes, Unaligned);
101101
assert_unaligned!(bool);
102102
/// SAFETY:
103-
/// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
104-
/// closure:
105-
/// - Given `t: *mut bool` and `let r = *mut u8`, `r` refers to an object
106-
/// of the same size as that referred to by `t`. This is true because
107-
/// `bool` and `u8` have the same size (1 byte) [1]. Neither `r` nor `t`
108-
/// contain `UnsafeCell`s because neither `bool` nor `u8` do [3].
109-
/// - The impl must only return `true` for its argument if the original
110-
/// `Maybe<bool>` refers to a valid `bool`. We only return true if the
111-
/// `u8` value is 0 or 1, and both of these are valid values for `bool`.
112-
/// [2]
103+
/// The impl must only return `true` for its argument if the original
104+
/// `Maybe<bool>` refers to a valid `bool`. We only return true if the `u8`
105+
/// value is 0 or 1, and both of these are valid values for `bool` [1].
113106
///
114-
/// [1] Per https://doc.rust-lang.org/1.81.0/reference/type-layout.html#primitive-data-layout:
115-
///
116-
/// The size of most primitives is given in this table.
117-
///
118-
/// | Type | `size_of::<Type>() ` |
119-
/// |-----------|----------------------|
120-
/// | `bool` | 1 |
121-
/// | `u8`/`i8` | 1 |
122-
///
123-
/// [2] Per https://doc.rust-lang.org/1.81.0/reference/types/boolean.html:
107+
/// [1] Per https://doc.rust-lang.org/1.81.0/reference/types/boolean.html:
124108
///
125109
/// The value false has the bit pattern 0x00 and the value true has the
126110
/// bit pattern 0x01.
127-
///
128-
/// [3] TODO(#429): Justify this claim.
129111
unsafe_impl!(=> TryFromBytes for bool; |byte| {
130112
let byte = byte.transmute::<u8, invariant::Valid, _>();
131113
*byte.unaligned_as_ref() < 2
@@ -148,27 +130,14 @@ safety_comment! {
148130
/// [1] https://doc.rust-lang.org/1.81.0/reference/types/textual.html
149131
unsafe_impl!(char: Immutable, FromZeros, IntoBytes);
150132
/// SAFETY:
151-
/// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
152-
/// closure:
153-
/// - Given `t: *mut char` and `let r = *mut u32`, `r` refers to an object
154-
/// of the same size as that referred to by `t`. This is true because
155-
/// `char` and `u32` have the same size [1]. Neither `r` nor `t` contain
156-
/// `UnsafeCell`s because neither `char` nor `u32` do [3].
157-
/// - The impl must only return `true` for its argument if the original
158-
/// `Maybe<char>` refers to a valid `char`. `char::from_u32` guarantees
159-
/// that it returns `None` if its input is not a valid `char`. [2]
133+
/// The impl must only return `true` for its argument if the original
134+
/// `Maybe<char>` refers to a valid `char`. `char::from_u32` guarantees that
135+
/// it returns `None` if its input is not a valid `char` [1].
160136
///
161-
/// [1] Per https://doc.rust-lang.org/nightly/reference/types/textual.html#layout-and-bit-validity:
162-
///
163-
/// `char` is guaranteed to have the same size and alignment as `u32` on
164-
/// all platforms.
165-
///
166-
/// [2] Per https://doc.rust-lang.org/core/primitive.char.html#method.from_u32:
137+
/// [1] Per https://doc.rust-lang.org/core/primitive.char.html#method.from_u32:
167138
///
168139
/// `from_u32()` will return `None` if the input is not a valid value for
169140
/// a `char`.
170-
///
171-
/// [3] TODO(#429): Justify this claim.
172141
unsafe_impl!(=> TryFromBytes for char; |c| {
173142
let c = c.transmute::<Unalign<u32>, invariant::Valid, _>();
174143
let c = c.read_unaligned().into_inner();
@@ -196,20 +165,9 @@ safety_comment! {
196165
/// [1] https://doc.rust-lang.org/1.81.0/reference/type-layout.html#str-layout
197166
unsafe_impl!(str: Immutable, FromZeros, IntoBytes, Unaligned);
198167
/// SAFETY:
199-
/// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
200-
/// closure:
201-
/// - Given `t: *mut str` and `let r = *mut [u8]`, `r` refers to an object
202-
/// of the same size as that referred to by `t`. This is true because
203-
/// `str` and `[u8]` have the same representation. [1] Neither `t` nor
204-
/// `r` contain `UnsafeCell`s because `[u8]` doesn't, and both `t` and
205-
/// `r` have that representation.
206-
/// - The impl must only return `true` for its argument if the original
207-
/// `Maybe<str>` refers to a valid `str`. `str::from_utf8` guarantees
208-
/// that it returns `Err` if its input is not a valid `str`. [2]
209-
///
210-
/// [1] Per https://doc.rust-lang.org/1.81.0/reference/types/textual.html:
211-
///
212-
/// A value of type `str` is represented the same was as `[u8]`.
168+
/// The impl must only return `true` for its argument if the original
169+
/// `Maybe<str>` refers to a valid `str`. `str::from_utf8` guarantees that
170+
/// it returns `Err` if its input is not a valid `str` [1].
213171
///
214172
/// [2] Per https://doc.rust-lang.org/core/str/fn.from_utf8.html#errors:
215173
///
@@ -307,25 +265,6 @@ safety_comment! {
307265
unsafe_impl!(NonZeroI128: Immutable, IntoBytes);
308266
unsafe_impl!(NonZeroUsize: Immutable, IntoBytes);
309267
unsafe_impl!(NonZeroIsize: Immutable, IntoBytes);
310-
/// SAFETY:
311-
/// - The safety requirements for `unsafe_impl!` with an `is_bit_valid`
312-
/// closure:
313-
/// - Given `t: *mut NonZeroXxx` and `let r = *mut xxx`, `r` refers to an
314-
/// object of the same size as that referred to by `t`. This is true
315-
/// because `NonZeroXxx` and `xxx` have the same size. [1] Neither `r`
316-
/// nor `t` refer to any `UnsafeCell`s because neither `NonZeroXxx` [2]
317-
/// nor `xxx` do.
318-
/// - The impl must only return `true` for its argument if the original
319-
/// `Maybe<NonZeroXxx>` refers to a valid `NonZeroXxx`. The only `xxx`
320-
/// which is not also a valid `NonZeroXxx` is 0. [1]
321-
///
322-
/// [1] Per https://doc.rust-lang.org/1.81.0/core/num/type.NonZeroU16.html:
323-
///
324-
/// `NonZeroU16` is guaranteed to have the same layout and bit validity as
325-
/// `u16` with the exception that `0` is not a valid instance.
326-
///
327-
/// [2] `NonZeroXxx` self-evidently does not contain `UnsafeCell`s. This is
328-
/// not a proof, but we are accepting this as a known risk per #1358.
329268
unsafe_impl_try_from_bytes_for_nonzero!(
330269
NonZeroU8[u8],
331270
NonZeroI8[i8],

src/wrappers.rs

-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,6 @@ safety_comment! {
129129
/// `UnsafeCell`s exactly when `T` does.
130130
/// - `TryFromBytes`: `Unalign<T>` has the same the same bit validity as
131131
/// `T`, so `T::is_bit_valid` is a sound implementation of `is_bit_valid`.
132-
/// Furthermore:
133-
/// - Since `T` and `Unalign<T>` have the same layout, they have the same
134-
/// size (as required by `unsafe_impl!`).
135-
/// - Since `T` and `Unalign<T>` have the same fields, they have
136-
/// `UnsafeCell`s at the same byte ranges (as required by
137-
/// `unsafe_impl!`).
138132
impl_or_verify!(T => Unaligned for Unalign<T>);
139133
impl_or_verify!(T: Immutable => Immutable for Unalign<T>);
140134
impl_or_verify!(

0 commit comments

Comments
 (0)