Skip to content

Commit a1623ff

Browse files
Deny unsafe ops in unsafe fns, part 6
And final part!!!
1 parent b365233 commit a1623ff

25 files changed

+185
-93
lines changed

src/libcore/alloc/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Memory allocation APIs
22
33
#![stable(feature = "alloc_module", since = "1.28.0")]
4-
#![deny(unsafe_op_in_unsafe_fn)]
54

65
mod global;
76
mod layout;

src/libcore/cell.rs

-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@
187187
//!
188188
189189
#![stable(feature = "rust1", since = "1.0.0")]
190-
#![deny(unsafe_op_in_unsafe_fn)]
191190

192191
use crate::cmp::Ordering;
193192
use crate::fmt::{self, Debug, Display};

src/libcore/char/convert.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Character conversions.
22
3-
#![deny(unsafe_op_in_unsafe_fn)]
4-
53
use crate::convert::TryFrom;
64
use crate::fmt;
75
use crate::mem::transmute;

src/libcore/char/methods.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! impl char {}
22
3-
#![deny(unsafe_op_in_unsafe_fn)]
4-
53
use crate::slice;
64
use crate::str::from_utf8_unchecked_mut;
75
use crate::unicode::printable::is_printable;

src/libcore/convert/num.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
2-
31
use super::{From, TryFrom};
42
use crate::num::TryFromIntError;
53

src/libcore/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![stable(feature = "", since = "1.30.0")]
22
#![allow(non_camel_case_types)]
3-
#![deny(unsafe_op_in_unsafe_fn)]
43

54
//! Utilities related to FFI bindings.
65

src/libcore/future/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,7 @@ where
8585
#[unstable(feature = "gen_future", issue = "50547")]
8686
#[inline]
8787
pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
88-
&mut *cx.0.as_ptr().cast()
88+
// SAFETY: the caller must guarantee that `cx.0` is a valid pointer
89+
// that fulfills all the requirements for a mutable reference.
90+
unsafe { &mut *cx.0.as_ptr().cast() }
8991
}

src/libcore/hash/sip.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! An implementation of SipHash.
22
33
#![allow(deprecated)] // the types in this module are deprecated
4-
#![deny(unsafe_op_in_unsafe_fn)]
54

65
use crate::cmp;
76
use crate::marker::PhantomData;

src/libcore/hint.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
//! Hints to compiler that affects how code should be emitted or optimized.
44
5-
#![deny(unsafe_op_in_unsafe_fn)]
6-
75
use crate::intrinsics;
86

97
/// Informs the compiler that this point in the code is not reachable, enabling

src/libcore/intrinsics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
issue = "none"
5454
)]
5555
#![allow(missing_docs)]
56-
#![deny(unsafe_op_in_unsafe_fn)]
5756

5857
use crate::marker::DiscriminantKind;
5958
use crate::mem;

src/libcore/iter/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@
309309
//! [`min`]: trait.Iterator.html#method.min
310310
311311
#![stable(feature = "rust1", since = "1.0.0")]
312-
#![deny(unsafe_op_in_unsafe_fn)]
313312

314313
use crate::ops::Try;
315314

src/libcore/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
#![feature(const_caller_location)]
150150
#![feature(no_niche)] // rust-lang/rust#68303
151151
#![feature(unsafe_block_in_unsafe_fn)]
152+
#![deny(unsafe_op_in_unsafe_fn)]
152153

153154
#[prelude_import]
154155
#[allow(unused)]
@@ -279,7 +280,13 @@ pub mod primitive;
279280
// set up in such a way that directly pulling it here works such that the
280281
// crate uses the this crate as its libcore.
281282
#[path = "../stdarch/crates/core_arch/src/mod.rs"]
282-
#[allow(missing_docs, missing_debug_implementations, dead_code, unused_imports)]
283+
#[allow(
284+
missing_docs,
285+
missing_debug_implementations,
286+
dead_code,
287+
unused_imports,
288+
unsafe_op_in_unsafe_fn
289+
)]
283290
// FIXME: This annotation should be moved into rust-lang/stdarch after clashing_extern_declarations is
284291
// merged. It currently cannot because bootstrap fails as the lint hasn't been defined yet.
285292
#[cfg_attr(not(bootstrap), allow(clashing_extern_declarations))]

src/libcore/mem/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! types, initializing and manipulating memory.
55
66
#![stable(feature = "rust1", since = "1.0.0")]
7-
#![deny(unsafe_op_in_unsafe_fn)]
87

98
use crate::clone;
109
use crate::cmp;

src/libcore/num/f32.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! new code should use the associated constants directly on the primitive type.
1010
1111
#![stable(feature = "rust1", since = "1.0.0")]
12-
#![deny(unsafe_op_in_unsafe_fn)]
1312

1413
use crate::convert::FloatToInt;
1514
#[cfg(not(test))]

src/libcore/num/f64.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
//! new code should use the associated constants directly on the primitive type.
1010
1111
#![stable(feature = "rust1", since = "1.0.0")]
12-
#![deny(unsafe_op_in_unsafe_fn)]
1312

1413
use crate::convert::FloatToInt;
1514
#[cfg(not(test))]

src/libcore/num/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! Numeric traits and functions for the built-in numeric types.
44
55
#![stable(feature = "rust1", since = "1.0.0")]
6-
#![deny(unsafe_op_in_unsafe_fn)]
76

87
use crate::convert::Infallible;
98
use crate::fmt;

src/libcore/pin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@
375375
//! [`i32`]: ../../std/primitive.i32.html
376376
377377
#![stable(feature = "pin", since = "1.33.0")]
378-
#![deny(unsafe_op_in_unsafe_fn)]
379378

380379
use crate::cmp::{self, PartialEq, PartialOrd};
381380
use crate::fmt;

src/libcore/ptr/const_ptr.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ impl<T: ?Sized> *const T {
9595
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
9696
#[inline]
9797
pub unsafe fn as_ref<'a>(self) -> Option<&'a T> {
98-
if self.is_null() { None } else { Some(&*self) }
98+
// SAFETY: the caller must guarantee that `self` is valid
99+
// for a reference if it isn't null.
100+
if self.is_null() { None } else { unsafe { Some(&*self) } }
99101
}
100102

101103
/// Calculates the offset from a pointer.
@@ -157,7 +159,8 @@ impl<T: ?Sized> *const T {
157159
where
158160
T: Sized,
159161
{
160-
intrinsics::offset(self, count)
162+
// SAFETY: the caller must uphold the safety contract for `offset`.
163+
unsafe { intrinsics::offset(self, count) }
161164
}
162165

163166
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -292,7 +295,8 @@ impl<T: ?Sized> *const T {
292295
{
293296
let pointee_size = mem::size_of::<T>();
294297
assert!(0 < pointee_size && pointee_size <= isize::MAX as usize);
295-
intrinsics::ptr_offset_from(self, origin)
298+
// SAFETY: the caller must uphold the safety contract for `ptr_offset_from`.
299+
unsafe { intrinsics::ptr_offset_from(self, origin) }
296300
}
297301

298302
/// Returns whether two pointers are guaranteed to be equal.
@@ -471,7 +475,8 @@ impl<T: ?Sized> *const T {
471475
where
472476
T: Sized,
473477
{
474-
self.offset(count as isize)
478+
// SAFETY: the caller must uphold the safety contract for `offset`.
479+
unsafe { self.offset(count as isize) }
475480
}
476481

477482
/// Calculates the offset from a pointer (convenience for
@@ -534,7 +539,8 @@ impl<T: ?Sized> *const T {
534539
where
535540
T: Sized,
536541
{
537-
self.offset((count as isize).wrapping_neg())
542+
// SAFETY: the caller must uphold the safety contract for `offset`.
543+
unsafe { self.offset((count as isize).wrapping_neg()) }
538544
}
539545

540546
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -663,7 +669,8 @@ impl<T: ?Sized> *const T {
663669
where
664670
T: Sized,
665671
{
666-
read(self)
672+
// SAFETY: the caller must uphold the safety contract for `read`.
673+
unsafe { read(self) }
667674
}
668675

669676
/// Performs a volatile read of the value from `self` without moving it. This
@@ -682,7 +689,8 @@ impl<T: ?Sized> *const T {
682689
where
683690
T: Sized,
684691
{
685-
read_volatile(self)
692+
// SAFETY: the caller must uphold the safety contract for `read_volatile`.
693+
unsafe { read_volatile(self) }
686694
}
687695

688696
/// Reads the value from `self` without moving it. This leaves the
@@ -699,7 +707,8 @@ impl<T: ?Sized> *const T {
699707
where
700708
T: Sized,
701709
{
702-
read_unaligned(self)
710+
// SAFETY: the caller must uphold the safety contract for `read_unaligned`.
711+
unsafe { read_unaligned(self) }
703712
}
704713

705714
/// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
@@ -716,7 +725,8 @@ impl<T: ?Sized> *const T {
716725
where
717726
T: Sized,
718727
{
719-
copy(self, dest, count)
728+
// SAFETY: the caller must uphold the safety contract for `copy`.
729+
unsafe { copy(self, dest, count) }
720730
}
721731

722732
/// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
@@ -733,7 +743,8 @@ impl<T: ?Sized> *const T {
733743
where
734744
T: Sized,
735745
{
736-
copy_nonoverlapping(self, dest, count)
746+
// SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
747+
unsafe { copy_nonoverlapping(self, dest, count) }
737748
}
738749

739750
/// Computes the offset that needs to be applied to the pointer in order to make it aligned to

0 commit comments

Comments
 (0)