@@ -735,9 +735,9 @@ impl<T: ?Sized> NonNull<T> {
735735 //github.com/
736736 //github.com/ * `self` and `origen` must either
737737 //github.com/
738+ //github.com/ * point to the same address, or
738739 //github.com/ * both be *derived from* a pointer to the same [allocated object], and the memory range between
739- //github.com/ the two pointers must be either empty or in bounds of that object. (See below for an example.)
740- //github.com/ * or both be derived from an integer literal/constant, and point to the same address.
740+ //github.com/ the two pointers must be in bounds of that object. (See below for an example.)
741741 //github.com/
742742 //github.com/ * The distance between the pointers, in bytes, must be an exact multiple
743743 //github.com/ of the size of `T`.
@@ -789,14 +789,15 @@ impl<T: ?Sized> NonNull<T> {
789789 //github.com/ let ptr1 = NonNull::new(Box::into_raw(Box::new(0u8))).unwrap();
790790 //github.com/ let ptr2 = NonNull::new(Box::into_raw(Box::new(1u8))).unwrap();
791791 //github.com/ let diff = (ptr2.addr().get() as isize).wrapping_sub(ptr1.addr().get() as isize);
792- //github.com/ // Make ptr2_other an "alias" of ptr2, but derived from ptr1.
793- //github.com/ let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff)).unwrap();
792+ //github.com/ // Make ptr2_other an "alias" of ptr2.add(1), but derived from ptr1.
793+ //github.com/ let diff_plus_1 = diff.wrapping_add(1);
794+ //github.com/ let ptr2_other = NonNull::new(ptr1.as_ptr().wrapping_byte_offset(diff_plus_1)).unwrap();
794795 //github.com/ assert_eq!(ptr2.addr(), ptr2_other.addr());
795796 //github.com/ // Since ptr2_other and ptr2 are derived from pointers to different objects,
796797 //github.com/ // computing their offset is undefined behavior, even though
797- //github.com/ // they point to the same address !
798+ //github.com/ // they point to addresses that are in-bounds of the same object !
798799 //github.com/
799- //github.com/ let zero = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior
800+ //github.com/ let one = unsafe { ptr2_other.offset_from(ptr2) }; // Undefined Behavior! ⚠️
800801 //github.com/ ```
801802 #[ inline]
802803 #[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
0 commit comments