diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index ca8e5e90318da..03a380a5c567a 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -1699,6 +1699,10 @@ rustc_queries! { query is_freeze_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { desc { "computing whether `{}` is freeze", env.value } } + /// Query backing `Ty::is_unsafe_unpin`. + query is_unsafe_unpin_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { + desc { "computing whether `{}` is `UnsafeUnpin`", env.value } + } /// Query backing `Ty::is_unpin`. query is_unpin_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { desc { "computing whether `{}` is `Unpin`", env.value } diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index 14ebcc968f7af..710ef819806d7 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -1041,9 +1041,11 @@ where hir::Mutability::Not => { PointerKind::SharedRef { frozen: optimize && ty.is_freeze(tcx, typing_env) } } - hir::Mutability::Mut => { - PointerKind::MutableRef { unpin: optimize && ty.is_unpin(tcx, typing_env) } - } + hir::Mutability::Mut => PointerKind::MutableRef { + unpin: optimize + && ty.is_unpin(tcx, typing_env) + && ty.is_unsafe_unpin(tcx, typing_env), + }, }; tcx.layout_of(typing_env.as_query_input(ty)).ok().map(|layout| PointeeInfo { @@ -1138,7 +1140,9 @@ where debug_assert!(pointee.safe.is_none()); let optimize = tcx.sess.opts.optimize != OptLevel::No; pointee.safe = Some(PointerKind::Box { - unpin: optimize && boxed_ty.is_unpin(tcx, typing_env), + unpin: optimize + && boxed_ty.is_unpin(tcx, typing_env) + && boxed_ty.is_unsafe_unpin(tcx, typing_env), global: this.ty.is_box_global(tcx), }); } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index d6e6ffae7300d..2ee8c23793ce0 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1189,14 +1189,23 @@ impl<'tcx> Ty<'tcx> { } } + /// Checks whether values of this type `T` implement the `UnsafeUnpin` trait. + pub fn is_unsafe_unpin(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> bool { + self.is_trivially_unpin() || tcx.is_unsafe_unpin_raw(typing_env.as_query_input(self)) + } + /// Checks whether values of this type `T` implement the `Unpin` trait. + /// + /// Note that this is a safe trait, so it cannot be very semantically meaningful. + /// However, as a hack to mitigate until a + /// proper solution is implemented, we do give special semantics to the `Unpin` trait. pub fn is_unpin(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> bool { self.is_trivially_unpin() || tcx.is_unpin_raw(typing_env.as_query_input(self)) } - /// Fast path helper for testing if a type is `Unpin`. + /// Fast path helper for testing if a type is `Unpin` *and* `UnsafeUnpin`. /// - /// Returning true means the type is known to be `Unpin`. Returning + /// Returning true means the type is known to be `Unpin` and `UnsafeUnpin`. Returning /// `false` means nothing -- could be `Unpin`, might not be. fn is_trivially_unpin(self) -> bool { match self.kind() { diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 34c080c4938f5..72f31bbf62a62 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -306,8 +306,12 @@ fn arg_attrs_for_rust_scalar<'tcx>( let kind = if let Some(kind) = pointee.safe { Some(kind) } else if let Some(pointee) = drop_target_pointee { + assert_eq!(pointee, layout.ty.builtin_deref(true).unwrap()); + assert_eq!(offset, Size::ZERO); // The argument to `drop_in_place` is semantically equivalent to a mutable reference. - Some(PointerKind::MutableRef { unpin: pointee.is_unpin(tcx, cx.typing_env) }) + let mutref = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, pointee); + let layout = cx.layout_of(mutref).unwrap(); + layout.pointee_info_at(&cx, offset).and_then(|pi| pi.safe) } else { None }; diff --git a/compiler/rustc_ty_utils/src/common_traits.rs b/compiler/rustc_ty_utils/src/common_traits.rs index 7219f40710e08..a3f1be77f47a6 100644 --- a/compiler/rustc_ty_utils/src/common_traits.rs +++ b/compiler/rustc_ty_utils/src/common_traits.rs @@ -8,36 +8,43 @@ use rustc_span::DUMMY_SP; use rustc_trait_selection::traits; fn is_copy_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { - is_item_raw(tcx, query, LangItem::Copy) + is_trait_raw(tcx, query, LangItem::Copy) } fn is_use_cloned_raw<'tcx>( tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>, ) -> bool { - is_item_raw(tcx, query, LangItem::UseCloned) + is_trait_raw(tcx, query, LangItem::UseCloned) } fn is_sized_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { - is_item_raw(tcx, query, LangItem::Sized) + is_trait_raw(tcx, query, LangItem::Sized) } fn is_freeze_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { - is_item_raw(tcx, query, LangItem::Freeze) + is_trait_raw(tcx, query, LangItem::Freeze) +} + +fn is_unsafe_unpin_raw<'tcx>( + tcx: TyCtxt<'tcx>, + query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>, +) -> bool { + is_trait_raw(tcx, query, LangItem::UnsafeUnpin) } fn is_unpin_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool { - is_item_raw(tcx, query, LangItem::Unpin) + is_trait_raw(tcx, query, LangItem::Unpin) } fn is_async_drop_raw<'tcx>( tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>, ) -> bool { - is_item_raw(tcx, query, LangItem::AsyncDrop) + is_trait_raw(tcx, query, LangItem::AsyncDrop) } -fn is_item_raw<'tcx>( +fn is_trait_raw<'tcx>( tcx: TyCtxt<'tcx>, query: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>, item: LangItem, @@ -53,6 +60,7 @@ pub(crate) fn provide(providers: &mut Providers) { is_use_cloned_raw, is_sized_raw, is_freeze_raw, + is_unsafe_unpin_raw, is_unpin_raw, is_async_drop_raw, ..*providers diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 57416455e9de8..7187c71799b92 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -927,14 +927,20 @@ marker_impls! { /// This is part of [RFC 3467](https://rust-lang.github.io/rfcs/3467-unsafe-pinned.html), and is /// tracked by [#125735](https://github.com/rust-lang/rust/issues/125735). #[lang = "unsafe_unpin"] -pub(crate) unsafe auto trait UnsafeUnpin {} +#[unstable(feature = "unsafe_unpin", issue = "125735")] +pub unsafe auto trait UnsafeUnpin {} +#[unstable(feature = "unsafe_unpin", issue = "125735")] impl !UnsafeUnpin for UnsafePinned {} -unsafe impl UnsafeUnpin for PhantomData {} -unsafe impl UnsafeUnpin for *const T {} -unsafe impl UnsafeUnpin for *mut T {} -unsafe impl UnsafeUnpin for &T {} -unsafe impl UnsafeUnpin for &mut T {} +marker_impls! { +#[unstable(feature = "unsafe_unpin", issue = "125735")] + unsafe UnsafeUnpin for + {T: ?Sized} PhantomData, + {T: ?Sized} *const T, + {T: ?Sized} *mut T, + {T: ?Sized} &T, + {T: ?Sized} &mut T, +} /// Types that do not require any pinning guarantees. /// @@ -1027,6 +1033,7 @@ impl !Unpin for PhantomPinned {} // continue working. Ideally PhantomPinned could just wrap an `UnsafePinned<()>` to get the same // effect, but we can't add a new field to an already stable unit struct -- that would be a breaking // change. +#[unstable(feature = "unsafe_unpin", issue = "125735")] impl !UnsafeUnpin for PhantomPinned {} marker_impls! { diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 2b5279efb7f79..e24a31ed3e916 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -31,7 +31,7 @@ use crate::{fmt, intrinsics, ptr, ub_checks}; issue = "none" )] pub unsafe trait ZeroablePrimitive: Sized + Copy + private::Sealed { - #[doc(hidden)] + /// A type like `Self` but with a niche that includes zero. type NonZeroInner: Sized + Copy; } diff --git a/src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs b/src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs index a21898c506ab9..2d054557da7e0 100644 --- a/src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs +++ b/src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs @@ -71,7 +71,9 @@ impl NewPermission { access: None, protector: None, } - } else if pointee.is_unpin(*cx.tcx, cx.typing_env()) { + } else if pointee.is_unpin(*cx.tcx, cx.typing_env()) + && pointee.is_unsafe_unpin(*cx.tcx, cx.typing_env()) + { // A regular full mutable reference. On `FnEntry` this is `noalias` and `dereferenceable`. NewPermission::Uniform { perm: Permission::Unique, @@ -129,7 +131,9 @@ impl NewPermission { fn from_box_ty<'tcx>(ty: Ty<'tcx>, kind: RetagKind, cx: &crate::MiriInterpCx<'tcx>) -> Self { // `ty` is not the `Box` but the field of the Box with this pointer (due to allocator handling). let pointee = ty.builtin_deref(true).unwrap(); - if pointee.is_unpin(*cx.tcx, cx.typing_env()) { + if pointee.is_unpin(*cx.tcx, cx.typing_env()) + && pointee.is_unsafe_unpin(*cx.tcx, cx.typing_env()) + { // A regular box. On `FnEntry` this is `noalias`, but not `dereferenceable` (hence only // a weak protector). NewPermission::Uniform { diff --git a/src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs b/src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs index 173145788ee39..4f170f48aed81 100644 --- a/src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs +++ b/src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs @@ -133,7 +133,8 @@ impl<'tcx> NewPermission { retag_kind: RetagKind, cx: &crate::MiriInterpCx<'tcx>, ) -> Option { - let ty_is_unpin = pointee.is_unpin(*cx.tcx, cx.typing_env()); + let ty_is_unpin = pointee.is_unpin(*cx.tcx, cx.typing_env()) + && pointee.is_unsafe_unpin(*cx.tcx, cx.typing_env()); let ty_is_freeze = pointee.is_freeze(*cx.tcx, cx.typing_env()); let is_protected = retag_kind == RetagKind::FnEntry; diff --git a/src/tools/miri/tests/pass/both_borrows/unsafe_pinned.rs b/src/tools/miri/tests/pass/both_borrows/unsafe_pinned.rs index 0c75a07bfa2af..8e222e6279564 100644 --- a/src/tools/miri/tests/pass/both_borrows/unsafe_pinned.rs +++ b/src/tools/miri/tests/pass/both_borrows/unsafe_pinned.rs @@ -9,8 +9,36 @@ fn mutate(x: &UnsafePinned) { unsafe { ptr.write(42) }; } +fn mut_alias(x: &mut UnsafePinned, y: &mut UnsafePinned) { + unsafe { + x.get().write(0); + y.get().write(0); + x.get().write(0); + y.get().write(0); + } +} + +// Also try this with a type for which we implement `Unpin`, just to be extra mean. +struct MyUnsafePinned(UnsafePinned); +impl Unpin for MyUnsafePinned {} + +fn my_mut_alias(x: &mut MyUnsafePinned, y: &mut MyUnsafePinned) { + unsafe { + x.0.get().write(0); + y.0.get().write(0); + x.0.get().write(0); + y.0.get().write(0); + } +} + fn main() { - let x = UnsafePinned::new(0); + let mut x = UnsafePinned::new(0i32); mutate(&x); - assert_eq!(x.into_inner(), 42); + assert_eq!(unsafe { x.get().read() }, 42); + + let ptr = &raw mut x; + unsafe { mut_alias(&mut *ptr, &mut *ptr) }; + + let ptr = ptr.cast::>(); + unsafe { my_mut_alias(&mut *ptr, &mut *ptr) }; } diff --git a/tests/auxiliary/minicore.rs b/tests/auxiliary/minicore.rs index 95b217c130317..c4546f4dea93a 100644 --- a/tests/auxiliary/minicore.rs +++ b/tests/auxiliary/minicore.rs @@ -76,6 +76,9 @@ pub trait BikeshedGuaranteedNoDrop {} #[lang = "freeze"] pub unsafe auto trait Freeze {} +#[lang = "unsafe_unpin"] +pub unsafe auto trait UnsafeUnpin {} + #[lang = "unpin"] #[diagnostic::on_unimplemented( note = "consider using the `pin!` macro\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope", diff --git a/tests/codegen-llvm/function-arguments.rs b/tests/codegen-llvm/function-arguments.rs index b5febca3b87b5..46e153a0cfc62 100644 --- a/tests/codegen-llvm/function-arguments.rs +++ b/tests/codegen-llvm/function-arguments.rs @@ -1,9 +1,9 @@ //@ compile-flags: -Copt-level=3 -C no-prepopulate-passes #![crate_type = "lib"] #![feature(rustc_attrs)] -#![feature(allocator_api)] +#![feature(allocator_api, unsafe_unpin)] -use std::marker::PhantomPinned; +use std::marker::{PhantomPinned, UnsafeUnpin}; use std::mem::MaybeUninit; use std::num::NonZero; use std::ptr::NonNull; @@ -259,11 +259,21 @@ pub fn trait_raw(_: *const dyn Drop) {} // CHECK: @trait_box(ptr noalias noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}}) #[no_mangle] -pub fn trait_box(_: Box) {} +pub fn trait_box(_: Box) {} + +// Ensure that removing *either* `Unpin` or `UnsafeUnpin` is enough to lose the attribute. +// CHECK: @trait_box_pin1(ptr noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}}) +#[no_mangle] +pub fn trait_box_pin1(_: Box) {} +// CHECK: @trait_box_pin2(ptr noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}}) +#[no_mangle] +pub fn trait_box_pin2(_: Box) {} // CHECK: { ptr, ptr } @trait_option(ptr noalias noundef align 1 %x.0, ptr %x.1) #[no_mangle] -pub fn trait_option(x: Option>) -> Option> { +pub fn trait_option( + x: Option>, +) -> Option> { x } diff --git a/tests/rustdoc-html/auto/auto-trait-bounds-by-associated-type-50159.rs b/tests/rustdoc-html/auto/auto-trait-bounds-by-associated-type-50159.rs index 2803c4da437f3..14981783e5f35 100644 --- a/tests/rustdoc-html/auto/auto-trait-bounds-by-associated-type-50159.rs +++ b/tests/rustdoc-html/auto/auto-trait-bounds-by-associated-type-50159.rs @@ -20,7 +20,8 @@ where //@ has - '//h3[@class="code-header"]' 'impl Send for Switchwhere ::Item: Send' //@ has - '//h3[@class="code-header"]' 'impl Sync for Switchwhere ::Item: Sync' //@ count - '//*[@id="implementations-list"]//*[@class="impl"]' 0 -//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 6 +//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 7 +// The number here will need updating when new auto traits are added: ^ pub struct Switch { pub inner: ::Item2, } diff --git a/tests/rustdoc-html/empty-section.rs b/tests/rustdoc-html/empty-section.rs index 71ebc66d69538..1e6e725a5a7b9 100644 --- a/tests/rustdoc-html/empty-section.rs +++ b/tests/rustdoc-html/empty-section.rs @@ -1,13 +1,15 @@ #![crate_name = "foo"] -#![feature(negative_impls, freeze_impls, freeze)] +#![feature(negative_impls, freeze_impls, freeze, unsafe_unpin)] pub struct Foo; //@ has foo/struct.Foo.html //@ !hasraw - 'Auto Trait Implementations' +// Manually un-implement all auto traits for Foo: impl !Send for Foo {} impl !Sync for Foo {} impl !std::marker::Freeze for Foo {} +impl !std::marker::UnsafeUnpin for Foo {} impl !std::marker::Unpin for Foo {} impl !std::panic::RefUnwindSafe for Foo {} impl !std::panic::UnwindSafe for Foo {} diff --git a/tests/rustdoc-html/synthetic_auto/basic.rs b/tests/rustdoc-html/synthetic_auto/basic.rs index 9daf8963997a4..e8c2f5da9fe63 100644 --- a/tests/rustdoc-html/synthetic_auto/basic.rs +++ b/tests/rustdoc-html/synthetic_auto/basic.rs @@ -2,7 +2,8 @@ //@ has - '//h3[@class="code-header"]' 'impl Send for Foowhere T: Send' //@ has - '//h3[@class="code-header"]' 'impl Sync for Foowhere T: Sync' //@ count - '//*[@id="implementations-list"]//*[@class="impl"]' 0 -//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 6 +//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 7 +// The number here will need updating when new auto traits are added: ^ pub struct Foo { field: T, } diff --git a/tests/rustdoc-html/synthetic_auto/manual.rs b/tests/rustdoc-html/synthetic_auto/manual.rs index bbf361a6e5963..830bfccca09e9 100644 --- a/tests/rustdoc-html/synthetic_auto/manual.rs +++ b/tests/rustdoc-html/synthetic_auto/manual.rs @@ -6,7 +6,8 @@ // 'impl Send for Foo' // //@ count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1 -//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 5 +//@ count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 6 +// The number here will need updating when new auto traits are added: ^ pub struct Foo { field: T, }