Skip to content

Commit 8c4e417

Browse files
matthiaskrgrgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#134573 - lukas-code:unimpl-dyn-pointerlike, r=compiler-errors
unimplement `PointerLike` for trait objects Values of type `dyn* PointerLike` or `dyn PointerLike` are not pointer-like so these types should not implement `PointerLike`. After rust-lang#133226, `PointerLike` allows user implementations, so we can't just mark it with `#[rustc_deny_explicit_impl(implement_via_object = false)]`. Instead, this PR splits the `#[rustc_deny_explicit_impl(implement_via_object = ...)]` attribute into two separate attributes `#[rustc_deny_explicit_impl]` and `#[rustc_do_not_implement_via_object]` so that we opt out of the automatic `impl PointerLike for dyn PointerLike` and still allow user implementations. For traits that are marked with `#[do_not_implement_via_object]` but not `#[rustc_deny_explicit_impl]` I've also made it possible to add a manual `impl Trait for dyn Trait`. There is no immediate need for this, but it was one line to implement and seems nice to have. fixes rust-lang#134545 fixes rust-lang#134543 r? `@compiler-errors`
2 parents cfad669 + 975e6a4 commit 8c4e417

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

core/src/future/async_drop.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ pub trait AsyncDrop {
133133
}
134134

135135
#[lang = "async_destruct"]
136-
#[rustc_deny_explicit_impl(implement_via_object = false)]
136+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
137+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
138+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
137139
trait AsyncDestruct {
138140
type AsyncDestructor: Future<Output = ()>;
139141
}

core/src/marker.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ unsafe impl<T: Sync + ?Sized> Send for &T {}
141141
)]
142142
#[fundamental] // for Default, for example, which requires that `[T]: !Default` be evaluatable
143143
#[rustc_specialization_trait]
144-
#[rustc_deny_explicit_impl(implement_via_object = false)]
144+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
145+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
146+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
145147
#[rustc_coinductive]
146148
pub trait Sized {
147149
// Empty.
@@ -181,7 +183,9 @@ pub trait Sized {
181183
/// [^1]: Formerly known as *object safe*.
182184
#[unstable(feature = "unsize", issue = "18598")]
183185
#[lang = "unsize"]
184-
#[rustc_deny_explicit_impl(implement_via_object = false)]
186+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
187+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
188+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
185189
pub trait Unsize<T: ?Sized> {
186190
// Empty.
187191
}
@@ -815,7 +819,9 @@ impl<T: ?Sized> StructuralPartialEq for PhantomData<T> {}
815819
reason = "this trait is unlikely to ever be stabilized, use `mem::discriminant` instead"
816820
)]
817821
#[lang = "discriminant_kind"]
818-
#[rustc_deny_explicit_impl(implement_via_object = false)]
822+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
823+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
824+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
819825
pub trait DiscriminantKind {
820826
/// The type of the discriminant, which must satisfy the trait
821827
/// bounds required by `mem::Discriminant`.
@@ -956,7 +962,9 @@ marker_impls! {
956962
#[unstable(feature = "const_destruct", issue = "133214")]
957963
#[lang = "destruct"]
958964
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
959-
#[rustc_deny_explicit_impl(implement_via_object = false)]
965+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
966+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
967+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
960968
#[cfg_attr(not(bootstrap), const_trait)]
961969
pub trait Destruct {}
962970

@@ -967,19 +975,22 @@ pub trait Destruct {}
967975
#[unstable(feature = "tuple_trait", issue = "none")]
968976
#[lang = "tuple_trait"]
969977
#[diagnostic::on_unimplemented(message = "`{Self}` is not a tuple")]
970-
#[rustc_deny_explicit_impl(implement_via_object = false)]
978+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
979+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
980+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
971981
pub trait Tuple {}
972982

973983
/// A marker for pointer-like types.
974984
///
975-
/// All types that have the same size and alignment as a `usize` or
976-
/// `*const ()` automatically implement this trait.
985+
/// This trait can only be implemented for types that have the same size and alignment
986+
/// as a `usize` or `*const ()`.
977987
#[unstable(feature = "pointer_like_trait", issue = "none")]
978988
#[lang = "pointer_like"]
979989
#[diagnostic::on_unimplemented(
980990
message = "`{Self}` needs to have the same ABI as a pointer",
981991
label = "`{Self}` needs to be a pointer-like type"
982992
)]
993+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
983994
pub trait PointerLike {}
984995

985996
#[cfg(not(bootstrap))]
@@ -1068,7 +1079,9 @@ marker_impls! {
10681079
reason = "internal trait for implementing various traits for all function pointers"
10691080
)]
10701081
#[lang = "fn_ptr_trait"]
1071-
#[rustc_deny_explicit_impl(implement_via_object = false)]
1082+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
1083+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
1084+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
10721085
pub trait FnPtr: Copy + Clone {
10731086
/// Returns the address of the function pointer.
10741087
#[lang = "fn_ptr_addr"]

core/src/mem/transmutability.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ use crate::marker::{ConstParamTy_, UnsizedConstParamTy};
8484
/// `usize` is stable, but not portable.
8585
#[unstable(feature = "transmutability", issue = "99571")]
8686
#[lang = "transmute_trait"]
87-
#[rustc_deny_explicit_impl(implement_via_object = false)]
87+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
88+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
89+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
8890
#[rustc_coinductive]
8991
pub unsafe trait TransmuteFrom<Src, const ASSUME: Assume = { Assume::NOTHING }>
9092
where

core/src/ptr/metadata.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ use crate::ptr::NonNull;
5353
///
5454
/// [`to_raw_parts`]: *const::to_raw_parts
5555
#[lang = "pointee_trait"]
56-
#[rustc_deny_explicit_impl(implement_via_object = false)]
56+
#[cfg_attr(bootstrap, rustc_deny_explicit_impl(implement_via_object = false))]
57+
#[cfg_attr(not(bootstrap), rustc_deny_explicit_impl)]
58+
#[cfg_attr(not(bootstrap), rustc_do_not_implement_via_object)]
5759
pub trait Pointee {
5860
/// The type for metadata in pointers and references to `Self`.
5961
#[lang = "metadata_type"]

0 commit comments

Comments
 (0)