@@ -243,19 +243,24 @@ impl<T: ?Sized> *const T {
243
243
// a wrapping_offset, so we can emulate it as such. This should properly
244
244
// restore pointer provenance even under today's compiler.
245
245
let self_addr = self . addr ( ) ;
246
- // Unfortunately, the CHERI-compatible way of defining this operation
247
- // optimizes worse, so we special case it... in a somewhat ad-hoc way
248
- // (checking for 128 bit pointers) because at the time of this writing,
249
- // we don't actually support CHERI yet. Ideally this would be
250
- // `cfg!(target_supports_large_wrapping_offsets)` or something, see
251
- // #96152 for details.
252
- if cfg ! ( target_pointer_width = "128" ) {
253
- let offset = ( addr as isize ) . wrapping_sub ( self_addr as isize ) ;
254
- // This is the canonical desugarring of this operation
255
- self . cast :: < u8 > ( ) . wrapping_offset ( offset) . cast :: < T > ( )
256
- } else {
257
- self . cast :: < u8 > ( ) . wrapping_sub ( self_addr) . wrapping_add ( addr) . cast :: < T > ( )
258
- }
246
+ // In an ideal world (err, an ideal world we'd have an intrinsic, but
247
+ // short of that), we'd implement this as follows:
248
+ // ```
249
+ // let offset = (addr as isize).wrapping_sub(self_addr as isize);
250
+ // self.cast::<u8>().wrapping_offset(offset).cast::<T>()
251
+ // ```
252
+ // This is the canonical desugaring of this operation, and is compatible
253
+ // with targets which don't allow large wrapping add/sub/offset
254
+ // operations, including CHERI.
255
+ //
256
+ // Unfortunately, this causes worse codegen than the following
257
+ // implementation, which should be correct on all targets we currently
258
+ // support (at the moment AFAICT, we don't yet support CHERI, or we'd
259
+ // special-case it to use the desugaring listed above).
260
+ //
261
+ // As a result, we use the following implementation, which would be
262
+ // wrong on CHERI, but right everywhere else.
263
+ self . cast :: < u8 > ( ) . wrapping_sub ( self_addr) . wrapping_add ( addr) . cast :: < T > ( )
259
264
}
260
265
261
266
/// Creates a new pointer by mapping `self`'s address to a new one.
0 commit comments