Skip to content

Commit f9d1c17

Browse files
committed
Fix rustc compilation and code format
1 parent fb98bf7 commit f9d1c17

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

library/core/src/slice/iter.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ use crate::hint::assert_unchecked;
88
use crate::iter::{
99
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
1010
};
11+
#[cfg(kani)]
12+
use crate::kani;
1113
use crate::marker::PhantomData;
1214
use crate::mem::{self, SizedTypeProperties};
1315
use crate::num::NonZero;
1416
use crate::ptr::{self, without_provenance, without_provenance_mut, NonNull};
1517
use crate::{cmp, fmt};
16-
17-
#[cfg(kani)]
18-
use crate::kani;
19-
2018
use crate::ub_checks::Invariant;
2119

2220
#[stable(feature = "boxed_slice_into_iter", since = "1.80.0")]
@@ -163,14 +161,15 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
163161
}
164162

165163
#[unstable(feature = "ub_checks", issue = "none")]
166-
impl<T> crate::ub_checks::Invariant for Iter<'_, T> {
164+
impl<T> Invariant for Iter<'_, T> {
167165
fn is_safe(&self) -> bool {
168166
let ty_size = crate::mem::size_of::<T>();
169167
let distance = self.ptr.addr().get().abs_diff(self.end_or_len as usize);
170168
if ty_size == 0 || distance == 0 {
171169
self.ptr.is_aligned()
172170
} else {
173-
let slice_ptr: *const [T] = crate::ptr::from_raw_parts(self.ptr.as_ptr(), distance / ty_size);
171+
let slice_ptr: *const [T] =
172+
crate::ptr::from_raw_parts(self.ptr.as_ptr(), distance / ty_size);
174173
crate::ub_checks::same_allocation(self.ptr.as_ptr(), self.end_or_len)
175174
&& self.ptr.addr().get() <= self.end_or_len as usize
176175
&& distance % ty_size == 0
@@ -219,14 +218,15 @@ pub struct IterMut<'a, T: 'a> {
219218
}
220219

221220
#[unstable(feature = "ub_checks", issue = "none")]
222-
impl<T> crate::ub_checks::Invariant for IterMut<'_, T> {
221+
impl<T> Invariant for IterMut<'_, T> {
223222
fn is_safe(&self) -> bool {
224223
let size = crate::mem::size_of::<T>();
225224
if size == 0 {
226225
self.ptr.is_aligned()
227226
} else {
228227
let distance = self.ptr.addr().get().abs_diff(self.end_or_len as usize);
229-
let slice_ptr: *mut [T] = crate::ptr::from_raw_parts_mut(self.ptr.as_ptr(), distance / size);
228+
let slice_ptr: *mut [T] =
229+
crate::ptr::from_raw_parts_mut(self.ptr.as_ptr(), distance / size);
230230
crate::ub_checks::same_allocation(self.ptr.as_ptr(), self.end_or_len)
231231
&& self.ptr.addr().get() <= self.end_or_len as usize
232232
&& distance % size == 0
@@ -3506,7 +3506,8 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for ChunkByMut<'a, T, P> {
35063506
}
35073507

35083508
/// Verify the safety of the code implemented in this module (including generated code from macros).
3509-
#[unstable(feature = "kani", issue="none")]
3509+
#[cfg(kani)]
3510+
#[unstable(feature = "kani", issue = "none")]
35103511
mod verify {
35113512
use super::*;
35123513
use crate::kani;
@@ -3604,17 +3605,29 @@ mod verify {
36043605

36053606
// check_safe_abstraction!(check_advance_back_by, $ty, advance_back_by, kani::any());
36063607

3607-
check_safe_abstraction!(check_is_empty, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.is_empty(); });
3608-
check_safe_abstraction!(check_len, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.len(); });
3609-
check_safe_abstraction!(check_size_hint, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.size_hint(); });
3608+
check_safe_abstraction!(check_is_empty, $ty, |iter: &mut Iter<'_, $ty>| {
3609+
let _ = iter.is_empty();
3610+
});
3611+
check_safe_abstraction!(check_len, $ty, |iter: &mut Iter<'_, $ty>| {
3612+
let _ = iter.len();
3613+
});
3614+
check_safe_abstraction!(check_size_hint, $ty, |iter: &mut Iter<'_, $ty>| {
3615+
let _ = iter.size_hint();
3616+
});
36103617
//check_safe_abstraction!(check_nth, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.nth(kani::any()); });
36113618
//check_safe_abstraction!(check_advance_by, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.advance_by(kani::any()); });
3612-
//check_safe_abstraction!(check_next_back, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.next_back(); });
3619+
check_safe_abstraction!(check_next_back, $ty, |iter: &mut Iter<'_, $ty>| {
3620+
let _ = iter.next_back();
3621+
});
36133622
//check_safe_abstraction!(check_nth_back, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.nth_back(kani::any()); });
3614-
check_safe_abstraction!(check_next, $ty, |iter: &mut Iter<'_, $ty>| { let _ = iter.next(); });
3623+
check_safe_abstraction!(check_next, $ty, |iter: &mut Iter<'_, $ty>| {
3624+
let _ = iter.next();
3625+
});
36153626

36163627
// Ensure that clone always generates a safe object.
3617-
check_safe_abstraction!(check_clone, $ty, |iter: &mut Iter<'_, $ty>| { kani::assert(iter.clone().is_safe(), "Clone is safe"); });
3628+
check_safe_abstraction!(check_clone, $ty, |iter: &mut Iter<'_, $ty>| {
3629+
kani::assert(iter.clone().is_safe(), "Clone is safe");
3630+
});
36183631
}
36193632
};
36203633
}
@@ -3623,4 +3636,4 @@ mod verify {
36233636
check_iter_with_ty!(verify_u8, u8, u32::MAX as usize);
36243637
check_iter_with_ty!(verify_char, char, 50);
36253638
check_iter_with_ty!(verify_tup, (char, u8), 50);
3626-
}
3639+
}

library/core/src/ub_checks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ mod predicates {
176176
/// * `src` points to a properly initialized value of type `T`.
177177
///
178178
/// [`crate::ptr`]: https://doc.rust-lang.org/std/ptr/index.html
179-
pub fn can_dereference<T>(src: *const T) -> bool {
179+
pub fn can_dereference<T: ?Sized>(src: *const T) -> bool {
180180
let _ = src;
181181
true
182182
}
@@ -185,28 +185,28 @@ mod predicates {
185185
/// * `dst` must be valid for writes.
186186
/// * `dst` must be properly aligned. Use `write_unaligned` if this is not the
187187
/// case.
188-
pub fn can_write<T>(dst: *mut T) -> bool {
188+
pub fn can_write<T: ?Sized>(dst: *mut T) -> bool {
189189
let _ = dst;
190190
true
191191
}
192192

193193
/// Check if a pointer can be the target of unaligned reads.
194194
/// * `src` must be valid for reads.
195195
/// * `src` must point to a properly initialized value of type `T`.
196-
pub fn can_read_unaligned<T>(src: *const T) -> bool {
196+
pub fn can_read_unaligned<T: ?Sized>(src: *const T) -> bool {
197197
let _ = src;
198198
true
199199
}
200200

201201
/// Check if a pointer can be the target of unaligned writes.
202202
/// * `dst` must be valid for writes.
203-
pub fn can_write_unaligned<T>(dst: *mut T) -> bool {
203+
pub fn can_write_unaligned<T: ?Sized>(dst: *mut T) -> bool {
204204
let _ = dst;
205205
true
206206
}
207207

208208
/// Checks if two pointers point to the same allocation.
209-
pub fn same_allocation<T>(src: *const T, dst: *const T) -> bool {
209+
pub fn same_allocation<T: ?Sized>(src: *const T, dst: *const T) -> bool {
210210
let _ = (src, dst);
211211
true
212212
}

0 commit comments

Comments
 (0)