Skip to content

Commit b5bfcb4

Browse files
authored
Rollup merge of rust-lang#61785 - RalfJung:as-ref, r=rkruppe
note some safety concerns of raw-ptr-to-ref casts
2 parents e94e4a6 + 00bae87 commit b5bfcb4

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/libcore/ptr/mod.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,17 @@ impl<T: ?Sized> *const T {
984984
/// operation because the returned value could be pointing to invalid
985985
/// memory.
986986
///
987+
/// When calling this method, you have to ensure that if the pointer is
988+
/// non-NULL, then it is properly aligned, dereferencable (for the whole
989+
/// size of `T`) and points to an initialized instance of `T`. This applies
990+
/// even if the result of this method is unused!
991+
/// (The part about being initialized is not yet fully decided, but until
992+
/// it is, the only safe approach is to ensure that they are indeed initialized.)
993+
///
987994
/// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
988-
/// not necessarily reflect the actual lifetime of the data.
995+
/// not necessarily reflect the actual lifetime of the data. It is up to the
996+
/// caller to ensure that for the duration of this lifetime, the memory this
997+
/// pointer points to does not get written to outside of `UnsafeCell<U>`.
989998
///
990999
/// # Examples
9911000
///
@@ -1610,8 +1619,17 @@ impl<T: ?Sized> *mut T {
16101619
/// operation because the returned value could be pointing to invalid
16111620
/// memory.
16121621
///
1622+
/// When calling this method, you have to ensure that if the pointer is
1623+
/// non-NULL, then it is properly aligned, dereferencable (for the whole
1624+
/// size of `T`) and points to an initialized instance of `T`. This applies
1625+
/// even if the result of this method is unused!
1626+
/// (The part about being initialized is not yet fully decided, but until
1627+
/// it is, the only safe approach is to ensure that they are indeed initialized.)
1628+
///
16131629
/// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
1614-
/// not necessarily reflect the actual lifetime of the data.
1630+
/// not necessarily reflect the actual lifetime of the data. It is up to the
1631+
/// caller to ensure that for the duration of this lifetime, the memory this
1632+
/// pointer points to does not get written to outside of `UnsafeCell<U>`.
16151633
///
16161634
/// # Examples
16171635
///
@@ -1755,10 +1773,24 @@ impl<T: ?Sized> *mut T {
17551773
///
17561774
/// # Safety
17571775
///
1758-
/// As with `as_ref`, this is unsafe because it cannot verify the validity
1776+
/// As with [`as_ref`], this is unsafe because it cannot verify the validity
17591777
/// of the returned pointer, nor can it ensure that the lifetime `'a`
17601778
/// returned is indeed a valid lifetime for the contained data.
17611779
///
1780+
/// When calling this method, you have to ensure that if the pointer is
1781+
/// non-NULL, then it is properly aligned, dereferencable (for the whole
1782+
/// size of `T`) and points to an initialized instance of `T`. This applies
1783+
/// even if the result of this method is unused!
1784+
/// (The part about being initialized is not yet fully decided, but until
1785+
/// it is the only safe approach is to ensure that they are indeed initialized.)
1786+
///
1787+
/// Additionally, the lifetime `'a` returned is arbitrarily chosen and does
1788+
/// not necessarily reflect the actual lifetime of the data. It is up to the
1789+
/// caller to ensure that for the duration of this lifetime, the memory this
1790+
/// pointer points to does not get accessed through any other pointer.
1791+
///
1792+
/// [`as_ref`]: #method.as_ref
1793+
///
17621794
/// # Examples
17631795
///
17641796
/// Basic usage:

0 commit comments

Comments
 (0)