Skip to content

Commit 4fd2c8d

Browse files
authored
Rollup merge of rust-lang#133226 - compiler-errors:opt-in-pointer-like, r=lcnr
Make `PointerLike` opt-in instead of built-in The `PointerLike` trait currently is a built-in trait that computes the layout of the type. This is a bit problematic, because types implement this trait automatically. Since this can be broken due to semver-compatible changes to a type's layout, this is undesirable. Also, calling `layout_of` in the trait system also causes cycles. This PR makes the trait implemented via regular impls, and adds additional validation on top to make sure that those impls are valid. This could eventually be `derive()`d for custom smart pointers, and we can trust *that* as a semver promise rather than risking library authors accidentally breaking it. On the other hand, we may never expose `PointerLike`, but at least now the implementation doesn't invoke `layout_of` which could cause ICEs or cause cycles. Right now for a `PointerLike` impl to be valid, it must be an ADT that is `repr(transparent)` and the non-1zst field needs to implement `PointerLike`. There are also some primitive impls for `&T`/ `&mut T`/`*const T`/`*mut T`/`Box<T>`.
2 parents 3f03a0f + 8b4995a commit 4fd2c8d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

alloc/src/boxed.rs

+6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ use core::error::{self, Error};
191191
use core::fmt;
192192
use core::future::Future;
193193
use core::hash::{Hash, Hasher};
194+
#[cfg(not(bootstrap))]
195+
use core::marker::PointerLike;
194196
use core::marker::{Tuple, Unsize};
195197
use core::mem::{self, SizedTypeProperties};
196198
use core::ops::{
@@ -2131,3 +2133,7 @@ impl<E: Error> Error for Box<E> {
21312133
Error::provide(&**self, request);
21322134
}
21332135
}
2136+
2137+
#[cfg(not(bootstrap))]
2138+
#[unstable(feature = "pointer_like_trait", issue = "none")]
2139+
impl<T> PointerLike for Box<T> {}

alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
#![feature(panic_internals)]
137137
#![feature(pattern)]
138138
#![feature(pin_coerce_unsized_trait)]
139+
#![feature(pointer_like_trait)]
139140
#![feature(ptr_internals)]
140141
#![feature(ptr_metadata)]
141142
#![feature(ptr_sub_ptr)]

core/src/marker.rs

+12
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,18 @@ pub trait Tuple {}
981981
)]
982982
pub trait PointerLike {}
983983

984+
#[cfg(not(bootstrap))]
985+
marker_impls! {
986+
#[unstable(feature = "pointer_like_trait", issue = "none")]
987+
PointerLike for
988+
usize,
989+
{T} &T,
990+
{T} &mut T,
991+
{T} *const T,
992+
{T} *mut T,
993+
{T: PointerLike} crate::pin::Pin<T>,
994+
}
995+
984996
/// A marker for types which can be used as types of `const` generic parameters.
985997
///
986998
/// These types must have a proper equivalence relation (`Eq`) and it must be automatically

0 commit comments

Comments
 (0)