Skip to content

Commit 6659998

Browse files
authored
Remove .forget_exclusive from Ptr::try_into_valid (#1257)
This causes methods like `TryFromBytes::try_mut_from` to fail compilation during post-monomorphization when used with types containing `UnsafeCell`s. This wasn't caught because our tests hadn't been updated to exercise this condition. This commit also updates the tests so this can't slip through in the future.
1 parent 5b095e0 commit 6659998

File tree

2 files changed

+5
-33
lines changed

2 files changed

+5
-33
lines changed

src/impls.rs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,12 +1107,6 @@ mod tests {
11071107
&self,
11081108
bytes: &'bytes [u8],
11091109
) -> Option<Option<&'bytes T>>;
1110-
1111-
#[allow(clippy::needless_lifetimes)]
1112-
fn test_try_from_mut<'bytes>(
1113-
&self,
1114-
bytes: &'bytes mut [u8],
1115-
) -> Option<Option<&'bytes mut T>>;
11161110
}
11171111

11181112
impl<T: TryFromBytes + Immutable + KnownLayout + ?Sized> TestTryFromRef<T> for AutorefWrapper<T> {
@@ -1123,14 +1117,6 @@ mod tests {
11231117
) -> Option<Option<&'bytes T>> {
11241118
Some(T::try_ref_from(bytes).ok())
11251119
}
1126-
1127-
#[allow(clippy::needless_lifetimes)]
1128-
fn test_try_from_mut<'bytes>(
1129-
&self,
1130-
bytes: &'bytes mut [u8],
1131-
) -> Option<Option<&'bytes mut T>> {
1132-
Some(T::try_mut_from(bytes).ok())
1133-
}
11341120
}
11351121

11361122
pub(super) trait TestTryReadFrom<T> {
@@ -1224,16 +1210,6 @@ mod tests {
12241210
None
12251211
}
12261212

1227-
#[allow(clippy::needless_lifetimes)]
1228-
fn test_try_from_mut<'bytes>(&mut self, _bytes: &'bytes mut [u8]) -> Option<Option<&'bytes mut $ty>> {
1229-
assert_on_allowlist!(
1230-
test_try_from_mut($ty):
1231-
ManuallyDrop<[UnsafeCell<bool>]>
1232-
);
1233-
1234-
None
1235-
}
1236-
12371213
fn test_try_read_from(&mut self, _bytes: &[u8]) -> Option<Option<&$ty>> {
12381214
assert_on_allowlist!(
12391215
test_try_read_from($ty):
@@ -1342,10 +1318,8 @@ mod tests {
13421318
let bytes_mut = &mut vec.as_mut_slice()[offset..offset+size];
13431319
bytes_mut.copy_from_slice(bytes);
13441320

1345-
let res = ww.test_try_from_mut(bytes_mut);
1346-
if let Some(res) = res {
1347-
assert!(res.is_some(), "{}::try_mut_from({:?}): got `None`, expected `Some`", stringify!($ty), val);
1348-
}
1321+
let res = <$ty as TryFromBytes>::try_mut_from(bytes_mut);
1322+
assert!(res.is_ok(), "{}::try_mut_from({:?}): got `Err`, expected `Ok`", stringify!($ty), val);
13491323
}
13501324

13511325
let res = bytes.and_then(|bytes| ww.test_try_read_from(bytes));
@@ -1365,10 +1339,8 @@ mod tests {
13651339
assert!(res.is_none(), "{}::try_ref_from({:?}): got Some, expected None", stringify!($ty), c);
13661340
}
13671341

1368-
let res = w.test_try_from_mut(c);
1369-
if let Some(res) = res {
1370-
assert!(res.is_none(), "{}::try_mut_from({:?}): got Some, expected None", stringify!($ty), c);
1371-
}
1342+
let res = <$ty as TryFromBytes>::try_mut_from(c);
1343+
assert!(res.is_err(), "{}::try_mut_from({:?}): got Ok, expected Err", stringify!($ty), c);
13721344

13731345
let res = w.test_try_read_from(c);
13741346
if let Some(res) = res {

src/pointer/ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ mod _transitions {
874874
// This call may panic. If that happens, it doesn't cause any soundness
875875
// issues, as we have not generated any invalid state which we need to
876876
// fix before returning.
877-
if T::is_bit_valid(self.reborrow().forget_exclusive().forget_aligned()) {
877+
if T::is_bit_valid(self.reborrow().forget_aligned()) {
878878
// SAFETY: If `T::is_bit_valid`, code may assume that `self`
879879
// contains a bit-valid instance of `Self`.
880880
Ok(unsafe { self.assume_valid() })

0 commit comments

Comments
 (0)