From 8a5a5732a1527fbdffbc825ae630d911fc130e2e Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sun, 26 Jun 2022 10:07:48 -0400 Subject: [PATCH] Clarify addr and with_addr implementations --- crates/core_simd/src/elements/const_ptr.rs | 14 +++++++++----- crates/core_simd/src/elements/mut_ptr.rs | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/core_simd/src/elements/const_ptr.rs b/crates/core_simd/src/elements/const_ptr.rs index 5a5faad23c81..3485d31e44df 100644 --- a/crates/core_simd/src/elements/const_ptr.rs +++ b/crates/core_simd/src/elements/const_ptr.rs @@ -83,17 +83,21 @@ where #[inline] fn addr(self) -> Self::Usize { - // Safety: Since `addr` discards provenance, this is safe. + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. + // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the + // provenance). unsafe { core::mem::transmute_copy(&self) } - - //TODO switch to casts when available - //self.cast() } #[inline] - fn with_addr(self, addr: Self::Usize) -> Self { + fn with_addr(self, _addr: Self::Usize) -> Self { unimplemented!() /* + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. + // + // In the mean-time, this operation is defined to be "as if" it was + // a wrapping_offset, so we can emulate it as such. This should properly + // restore pointer provenance even under today's compiler. self.cast::<*const u8>() .wrapping_offset(addr.cast::() - self.addr().cast::()) .cast() diff --git a/crates/core_simd/src/elements/mut_ptr.rs b/crates/core_simd/src/elements/mut_ptr.rs index d7b05af0eac5..39fe9f356211 100644 --- a/crates/core_simd/src/elements/mut_ptr.rs +++ b/crates/core_simd/src/elements/mut_ptr.rs @@ -78,17 +78,21 @@ where #[inline] fn addr(self) -> Self::Usize { - // Safety: Since `addr` discards provenance, this is safe. + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. + // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the + // provenance). unsafe { core::mem::transmute_copy(&self) } - - //TODO switch to casts when available - //self.cast() } #[inline] - fn with_addr(self, addr: Self::Usize) -> Self { + fn with_addr(self, _addr: Self::Usize) -> Self { unimplemented!() /* + // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic. + // + // In the mean-time, this operation is defined to be "as if" it was + // a wrapping_offset, so we can emulate it as such. This should properly + // restore pointer provenance even under today's compiler. self.cast::<*mut u8>() .wrapping_offset(addr.cast::() - self.addr().cast::()) .cast()