From 8af1a6a1e5a3d673aec7cf67c25401163d344eaf Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 7 Jan 2024 02:41:28 +0000 Subject: [PATCH 01/40] Make ImplTraitPosition display more descriptive --- compiler/rustc_ast_lowering/src/lib.rs | 18 ++++++------- ...ture-gate-impl_trait_in_fn_trait_return.rs | 4 +-- ...-gate-impl_trait_in_fn_trait_return.stderr | 4 +-- tests/ui/impl-trait/where-allowed.stderr | 26 +++++++++---------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index fb59770d48a2b..2cd3a54eb3631 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -329,24 +329,24 @@ impl std::fmt::Display for ImplTraitPosition { ImplTraitPosition::AsyncBlock => "async blocks", ImplTraitPosition::Bound => "bounds", ImplTraitPosition::Generic => "generics", - ImplTraitPosition::ExternFnParam => "`extern fn` params", - ImplTraitPosition::ClosureParam => "closure params", - ImplTraitPosition::PointerParam => "`fn` pointer params", - ImplTraitPosition::FnTraitParam => "`Fn` trait params", - ImplTraitPosition::TraitParam => "trait method params", - ImplTraitPosition::ImplParam => "`impl` method params", + ImplTraitPosition::ExternFnParam => "`extern fn` parameters", + ImplTraitPosition::ClosureParam => "closure parameters", + ImplTraitPosition::PointerParam => "`fn` pointer parameters", + ImplTraitPosition::FnTraitParam => "the parameters of `Fn` trait bounds", + ImplTraitPosition::TraitParam => "trait method parameters", + ImplTraitPosition::ImplParam => "`impl` method parameters", ImplTraitPosition::ExternFnReturn => "`extern fn` return types", ImplTraitPosition::ClosureReturn => "closure return types", ImplTraitPosition::PointerReturn => "`fn` pointer return types", - ImplTraitPosition::FnTraitReturn => "`Fn` trait return types", + ImplTraitPosition::FnTraitReturn => "the return types of `Fn` trait bounds", ImplTraitPosition::GenericDefault => "generic parameter defaults", ImplTraitPosition::ConstTy => "const types", ImplTraitPosition::StaticTy => "static types", ImplTraitPosition::AssocTy => "associated types", ImplTraitPosition::FieldTy => "field types", - ImplTraitPosition::Cast => "cast types", + ImplTraitPosition::Cast => "cast expression types", ImplTraitPosition::ImplSelf => "impl headers", - ImplTraitPosition::OffsetOf => "`offset_of!` params", + ImplTraitPosition::OffsetOf => "`offset_of!` parameters", }; write!(f, "{name}") diff --git a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs index 1b9530fa82f38..f07abb9d04967 100644 --- a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs +++ b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs @@ -1,6 +1,6 @@ fn f() -> impl Fn() -> impl Sized { || () } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return +//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds fn g() -> &'static dyn Fn() -> impl Sized { &|| () } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return +//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr index f0c0cd040e03c..af56e2bd9ef3e 100644 --- a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr +++ b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr @@ -1,4 +1,4 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:1:24 | LL | fn f() -> impl Fn() -> impl Sized { || () } @@ -7,7 +7,7 @@ LL | fn f() -> impl Fn() -> impl Sized { || () } = note: see issue #99697 for more information = help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32 | LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () } diff --git a/tests/ui/impl-trait/where-allowed.stderr b/tests/ui/impl-trait/where-allowed.stderr index 2d8895030f2fe..9c841342ed384 100644 --- a/tests/ui/impl-trait/where-allowed.stderr +++ b/tests/ui/impl-trait/where-allowed.stderr @@ -43,7 +43,7 @@ LL | type InReturnInTypeAlias = fn() -> impl Debug; = note: see issue #63063 for more information = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer parameters --> $DIR/where-allowed.rs:18:40 | LL | fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() } @@ -55,7 +55,7 @@ error[E0562]: `impl Trait` only allowed in function and inherent method argument LL | fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer parameters --> $DIR/where-allowed.rs:26:38 | LL | fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() } @@ -67,49 +67,49 @@ error[E0562]: `impl Trait` only allowed in function and inherent method argument LL | fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:34:49 | LL | fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/where-allowed.rs:38:51 | LL | fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:42:55 | LL | fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:49:51 | LL | fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/where-allowed.rs:54:53 | LL | fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:58:57 | LL | fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:66:38 | LL | fn in_Fn_parameter_in_generics (_: F) { panic!() } | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/where-allowed.rs:70:40 | LL | fn in_Fn_return_in_generics impl Debug> (_: F) { panic!() } @@ -145,7 +145,7 @@ error[E0562]: `impl Trait` only allowed in function and inherent method argument LL | InTupleVariant(impl Debug), | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `extern fn` params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `extern fn` parameters --> $DIR/where-allowed.rs:138:33 | LL | fn in_foreign_parameters(_: impl Debug); @@ -205,13 +205,13 @@ error[E0562]: `impl Trait` only allowed in function and inherent method argument LL | where T: PartialEq | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait params +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:205:17 | LL | where T: Fn(impl Debug) | ^^^^^^^^^^ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `Fn` trait return types +error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds --> $DIR/where-allowed.rs:212:22 | LL | where T: Fn() -> impl Debug From 0f3957487b167e0cc6339948244312e0978838d1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 7 Jan 2024 02:48:38 +0000 Subject: [PATCH 02/40] Inline some helpers no longer needed due to RPITIT being stable --- compiler/rustc_ast_lowering/src/lib.rs | 76 ++++++++++---------------- 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 2cd3a54eb3631..998a7322fa014 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -304,8 +304,6 @@ enum ImplTraitPosition { ClosureParam, PointerParam, FnTraitParam, - TraitParam, - ImplParam, ExternFnReturn, ClosureReturn, PointerReturn, @@ -333,8 +331,6 @@ impl std::fmt::Display for ImplTraitPosition { ImplTraitPosition::ClosureParam => "closure parameters", ImplTraitPosition::PointerParam => "`fn` pointer parameters", ImplTraitPosition::FnTraitParam => "the parameters of `Fn` trait bounds", - ImplTraitPosition::TraitParam => "trait method parameters", - ImplTraitPosition::ImplParam => "`impl` method parameters", ImplTraitPosition::ExternFnReturn => "`extern fn` return types", ImplTraitPosition::ClosureReturn => "closure return types", ImplTraitPosition::PointerReturn => "`fn` pointer return types", @@ -364,19 +360,6 @@ enum FnDeclKind { Impl, } -impl FnDeclKind { - fn param_impl_trait_allowed(&self) -> bool { - matches!(self, FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait) - } - - fn return_impl_trait_allowed(&self) -> bool { - match self { - FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => true, - _ => false, - } - } -} - #[derive(Copy, Clone)] enum AstOwner<'a> { NonOwner, @@ -1842,19 +1825,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { inputs = &inputs[..inputs.len() - 1]; } let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| { - let itctx = if kind.param_impl_trait_allowed() { - ImplTraitContext::Universal - } else { - ImplTraitContext::Disallowed(match kind { - FnDeclKind::Fn | FnDeclKind::Inherent => { - unreachable!("fn should allow APIT") - } - FnDeclKind::ExternFn => ImplTraitPosition::ExternFnParam, - FnDeclKind::Closure => ImplTraitPosition::ClosureParam, - FnDeclKind::Pointer => ImplTraitPosition::PointerParam, - FnDeclKind::Trait => ImplTraitPosition::TraitParam, - FnDeclKind::Impl => ImplTraitPosition::ImplParam, - }) + let itctx = match kind { + FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => { + ImplTraitContext::Universal + } + FnDeclKind::ExternFn => { + ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnParam) + } + FnDeclKind::Closure => { + ImplTraitContext::Disallowed(ImplTraitPosition::ClosureParam) + } + FnDeclKind::Pointer => { + ImplTraitContext::Disallowed(ImplTraitPosition::PointerParam) + } }; self.lower_ty_direct(¶m.ty, &itctx) })); @@ -1866,26 +1849,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } None => match &decl.output { FnRetTy::Ty(ty) => { - let context = if kind.return_impl_trait_allowed() { - let fn_def_id = self.local_def_id(fn_node_id); - ImplTraitContext::ReturnPositionOpaqueTy { - origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), + let itctx = match kind { + FnDeclKind::Fn + | FnDeclKind::Inherent + | FnDeclKind::Trait + | FnDeclKind::Impl => ImplTraitContext::ReturnPositionOpaqueTy { + origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)), fn_kind: kind, + }, + FnDeclKind::ExternFn => { + ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn) + } + FnDeclKind::Closure => { + ImplTraitContext::Disallowed(ImplTraitPosition::ClosureReturn) + } + FnDeclKind::Pointer => { + ImplTraitContext::Disallowed(ImplTraitPosition::PointerReturn) } - } else { - ImplTraitContext::Disallowed(match kind { - FnDeclKind::Fn - | FnDeclKind::Inherent - | FnDeclKind::Trait - | FnDeclKind::Impl => { - unreachable!("fn should allow return-position impl trait in traits") - } - FnDeclKind::ExternFn => ImplTraitPosition::ExternFnReturn, - FnDeclKind::Closure => ImplTraitPosition::ClosureReturn, - FnDeclKind::Pointer => ImplTraitPosition::PointerReturn, - }) }; - hir::FnRetTy::Return(self.lower_ty(ty, &context)) + hir::FnRetTy::Return(self.lower_ty(ty, &itctx)) } FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(self.lower_span(*span)), }, From bfd63b20c83512d895b0d6f7c92bd38247b82688 Mon Sep 17 00:00:00 2001 From: Miguel Young de la Sota Date: Mon, 30 Aug 2021 14:18:10 -0400 Subject: [PATCH 03/40] Rewrite `Pin

` docs to clarify guarantees and uses The documentation today does not give a complete treatment of pinning from first principles, which appropriately describes how to design types that use it, nor does it provide formal statements of the guarantees users need to be aware of. This rewrite attempts to address these in a way that makes the concept more approachable while also making the documentation more normative. --- library/core/src/pin.rs | 819 +++++++++++++++++++++++++++++----------- 1 file changed, 605 insertions(+), 214 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 7d8c881eab809..2632a44a2aeba 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1,107 +1,235 @@ -//! Types that pin data to its location in memory. -//! -//! It is sometimes useful to have objects that are guaranteed not to move, -//! in the sense that their placement in memory does not change, and can thus be relied upon. -//! A prime example of such a scenario would be building self-referential structs, -//! as moving an object with pointers to itself will invalidate them, which could cause undefined -//! behavior. -//! -//! At a high level, a [Pin]\

ensures that the pointee of any pointer type -//! `P` has a stable location in memory, meaning it cannot be moved elsewhere -//! and its memory cannot be deallocated until it gets dropped. We say that the -//! pointee is "pinned". Things get more subtle when discussing types that -//! combine pinned with non-pinned data; [see below](#projections-and-structural-pinning) -//! for more details. -//! -//! By default, all types in Rust are movable. Rust allows passing all types by-value, -//! and common smart-pointer types such as [Box]\ and [&mut] T allow -//! replacing and moving the values they contain: you can move out of a [Box]\, -//! or you can use [`mem::swap`]. [Pin]\

wraps a pointer type `P`, so -//! [Pin]<[Box]\> functions much like a regular [Box]\: -//! when a [Pin]<[Box]\> gets dropped, so do its contents, and the memory gets -//! deallocated. Similarly, [Pin]<[&mut] T> is a lot like [&mut] T. -//! However, [Pin]\

does not let clients actually obtain a [Box]\ -//! or [&mut] T to pinned data, which implies that you cannot use operations such -//! as [`mem::swap`]: +//! Types that pin data to a location in memory. +//! +//! It is sometimes useful to be able to rely upon a certain value not being able to *move*, +//! in the sense that its address in memory cannot change. This is useful specifically when there +//! is a *pointer* that is pointing at that value. The ability to rely on this guarantee that +//! the *value a pointer is pointing at* (its *pointee*) will not move is necessary to implement +//! things like self-referential structs, as moving an object with pointers that are meant to +//! point into that struct's own data would cause those pointers to no longer be valid (they +//! would still be pointing at the struct's old location in memory). +//! +//! "Pinning" allows us to put a value *which is being pointed at* by some pointer `Ptr` into a state +//! that prevents safe code from *moving* the *pointee* value. In this way, we can allow [`unsafe`] code +//! to rely on the pointer not having its pointee moved out from under it. +//! +//! The rest of this documentation is intended to be the source of truth for users of [`Pin`] +//! in unsafe code; users of [`Pin`] in safe code do not need to read it in detail. +//! +//! There are several sections to this documentation: +//! +//! * [What is "*moving*"?][what-is-moving] +//! * [What is "pinning"?][what-is-pinning] +//! * [Examples of types that have address-sensitive states][address-sensitive-examples] +//! * [Self-referential struct][self-ref] +//! * [Intrusive, doubly-linked list][linked-list] +//! * [Subtle Details][subtle-details] +//! +//! # What is "*moving*"? +//! [what-is-moving]: self#what-is-moving +//! +//! When we say a value is *moved*, we mean that the compiler copies, byte-for-byte, the +//! value from one location to another. A move is mechanically identical to [`Copy`]ing +//! a value and discarding the old version of the data, thus making the moved-from value +//! inaccessible. Whenever we write *move* in italics, we mean this precise definition of +//! moving a value. +//! +//! All values in Rust are trivially *moveable*. This means that the address at which a value is +//! located is not necessarily stable in between borrows, and that the compiler is allowed to +//! *move* it to a new address without running any code to notify that value that its address +//! has changed. //! //! ``` -//! use std::pin::Pin; -//! fn swap_pins(x: Pin<&mut T>, y: Pin<&mut T>) { -//! // `mem::swap` needs `&mut T`, but we cannot get it. -//! // We are stuck, we cannot swap the contents of these references. -//! // We could use `Pin::get_unchecked_mut`, but that is unsafe for a reason: -//! // we are not allowed to use it for moving things out of the `Pin`. +//! #[derive(Default)] +//! struct AddrTracker(Option); +//! +//! impl AddrTracker { +//! fn check_for_move(&mut self) { +//! let current_addr = self as *mut Self as usize; +//! match self.0 { +//! None => self.0 = Some(addr), +//! Some(prev_addr) => assert_eq!(prev_addr, current_addr), +//! } +//! } +//! } +//! +//! fn take_and_return(tracker: AddrTracker) -> AddrTracker { +//! tracker +//! } +//! +//! let mut tracker = AddrTracker::default(); +//! tracker.check_for_move(); +//! +//! let mut tracker = take_and_return(tracker); +//! +//! // May panic! +//! // tracker.check_for_move(); +//! ``` +//! +//! Although the compiler will not insert moves where no semantic move has occurred, there +//! are many places where a value *may* be moved. For example, when passed into a function. +//! In this sense, Rust does not guarantee that `check_for_move()` will +//! never panic, because the compiler is permitted to *move* `tracker` to enable +//! optimizations like pass-by-value. +//! +//! Common smart-pointer types such as [`Box`] and [`&mut T`] allow removing and replacing the +//! values they contain: you can move out of a [`Box`], or you can use [`mem::swap`]. Putting +//! `tracker` behind a pointer isn't enough on its own to ensure that its address does not change. +//! +//! # What is "pinning"? +//! [what-is-pinning]: #what-is-pinning +//! +//! `Pin` wraps a pointer type `P`, but removes the ability to actually obtain a `P` from it in safe +//! code. By disallowing access to the type behind the pointer, `Pin` prevents us from using +//! operations like [`mem::swap`] to *move* out of the pointer. +//! +//! ## Address-sensitive types +//! +//! One noteworthy example of types which require `Pin` for correctness are the [`Future`]` types +//! generated by the compiler for `async fn`s. +//! +//! For these types, any value which lives "across" an `.await` point is stored in the state +//! machine, including values which borrow from other variables in the `async fn`. *Moving* the +//! [`Future`] in between calls to [`poll`] would invalidate these pointers, leaving the next call +//! to [`poll`] with dangling references! +//! +//! Such types are *address-sensitive*: they incorporate the address of `self` into an +//! operation. These types usually follow a common lifecycle: +//! +//! 1. A value is created which can be freely moved around. +//! * e.g. calling an async function +//! 2. An operation causes the value to depend on its own address using [`unsafe`]. +//! * e.g. calling [`poll`] for the first time on the produced [`Future`] +//! 3. Further [`unsafe`] operations assume that its address is stable. +//! * e.g. subsequent calls to [`poll`] +//! 4. The value is destroyed, undoing its address-sensitivity. +//! * e.g. [`drop`]ping the [`Future`] +//! +//! [`Pin

`] helps us implement steps (2) and (3) safely. +//! +//! ## `Pin` and pointers +//! +//! [`Pin

`] can wrap any pointer type, forming a promise that the pointee will not be *moved*. +//! This promise must be upheld by [`unsafe`] code which interacts with the [`Pin

`] so that +//! [`unsafe`] code can place the pointee in an address-sensitive state that will not be broken +//! by a *move*. Operations on an address-sensitive type accept an argument like +//! [Pin]<[`&mut T`]> or [Pin]<[`Box`]> to indicate this contract to +//! the caller. +//! +//! Since [`Pin

`] can wrap any pointer type, it interacts with +//! [`Deref`] and [`DerefMut`]. A [`Pin

`] where [`P: Deref`][Deref] is a +//! "`P`-style pointer" to a pinned [`P::Target`][Target] – so, a +//! [Pin]<[`Box`]> is an owned pointer to a pinned `T`, and a +//! [Pin]<[`Rc`]> is a reference-counted pointer to a pinned `T`. +//! +//! [`Pin

`] requires that implementations of [`Deref`] and [`DerefMut`] return a pointer to +//! pinned data when they are called on a pinned pointer and do not *move* out of their `self` +//! parameter. It is unsound for [`unsafe`] code to wrap such "evil" pointers; see +//! [`Pin

::new_unchecked`] for details. +//! +//! Pinning does not require any compiler "magic", only a specific contract between the library API +//! and its users. This differs from e.g. [`UnsafeCell`] which changes the semantics of a program's +//! compiled output. A [`Pin

`] is a handle to a value which does not allow moving the value out, +//! but Rust still considers all values themselves to be moveable with e.g. [`mem::swap`]. +//! +//! These guarantees are necessary to make our `AddrTracker` example work. If any code +//! sees a [Pin]<&mut AddrTracker>, it can safely assume that it will *always* see +//! [the same object][address-stability] for the same address (for the lifetime of +//! the pointee). If we had written `check_for_move` above to accept a +//! [Pin]<[`&mut Self`]> instead, multiple calls to it *cannot* panic: +//! +//! ``` +//! # use std::pin::Pin; +//! # #[derive(Default)] +//! # struct AddrTracker(usize); +//! impl AddrTracker { +//! fn check_for_move(self: Pin<&mut Self>) { +//! unsafe { +//! let unpinned = Pin::get_unchecked_mut(self); +//! let addr = unpinned as *mut Self as usize; +//! match unpinned.0 { +//! 0 => unpinned.0 = addr, +//! x => assert_eq!(x, addr), +//! } +//! } +//! } //! } +//! +//! let mut tracker = Box::pin(AddrTracker::default()); +//! tracker.as_mut().check_for_move(); +//! tracker.as_mut().check_for_move(); //! ``` //! -//! It is worth reiterating that [Pin]\

does *not* change the fact that a Rust -//! compiler considers all types movable. [`mem::swap`] remains callable for any `T`. Instead, -//! [Pin]\

prevents certain *values* (pointed to by pointers wrapped in -//! [Pin]\

) from being moved by making it impossible to call methods that require -//! [&mut] T on them (like [`mem::swap`]). -//! -//! [Pin]\

can be used to wrap any pointer type `P`, and as such it interacts with -//! [`Deref`] and [`DerefMut`]. A [Pin]\

where P: [Deref] should be -//! considered as a "`P`-style pointer" to a pinned P::[Target] – so, a -//! [Pin]<[Box]\> is an owned pointer to a pinned `T`, and a -//! [Pin]<[Rc]\> is a reference-counted pointer to a pinned `T`. -//! For correctness, [Pin]\

relies on the implementations of [`Deref`] and -//! [`DerefMut`] not to move out of their `self` parameter, and only ever to -//! return a pointer to pinned data when they are called on a pinned pointer. -//! -//! # `Unpin` -//! -//! Many types are always freely movable, even when pinned, because they do not -//! rely on having a stable address. This includes all the basic types (like -//! [`bool`], [`i32`], and references) as well as types consisting solely of these -//! types. Types that do not care about pinning implement the [`Unpin`] -//! auto-trait, which cancels the effect of [Pin]\

. For T: [Unpin], -//! [Pin]<[Box]\> and [Box]\ function identically, as do -//! [Pin]<[&mut] T> and [&mut] T. -//! -//! Note that pinning and [`Unpin`] only affect the pointed-to type P::[Target], -//! not the pointer type `P` itself that got wrapped in [Pin]\

. For example, -//! whether or not [Box]\ is [`Unpin`] has no effect on the behavior of -//! [Pin]<[Box]\> (here, `T` is the pointed-to type). -//! -//! # Example: self-referential struct -//! -//! Before we go into more details to explain the guarantees and choices -//! associated with [Pin]\

, we discuss some examples for how it might be used. -//! Feel free to [skip to where the theoretical discussion continues](#drop-guarantee). +//! [As discussed below][drop-guarantee], this has consequences for running +//! destructors of pinned memory, too. +//! +//! ## [`Unpin`] +//! +//! The vast majority of Rust types are not address-sensitive; these types +//! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of +//! [`Pin

`]. When [`T: Unpin`][Unpin], [Pin]<[`Box`]> and +//! [`Box`] function identically, as do [Pin]<[`&mut T`]> and +//! [`&mut T`]. +//! +//! This includes all of the basic types, like [`bool`], [`i32`], and [`&T`][&], +//! as well as any other type consisting only of those types. You can opt out of +//! [`Unpin`] via the [`PhantomPinned`] marker type. +//! +//! Pinning and [`Unpin`] only affect the pointee type [`P::Target`][Target], not the pointer type +//! `P` itself. For example, whether or not [`Box`] is [`Unpin`] has no effect on the behavior of +//! [Pin]<[`Box`]> because `T` is the pointee type. +//! +//! # Examples of address-sensitive types +//! [address-sensitive-examples]: #examples-of-address-sensitive-types +//! +//! ## Self-referential struct +//! [self-ref]: #a-self-referential-struct +//! [`Unmovable`]: #a-self-referential-struct +//! +//! Self-referential structs are the simplest kind of address-sensitive type. +//! +//! It is often useful for a struct to hold a pointer back into itself, which +//! allows the program to efficiently track subsections of the struct. +//! Below, the `slice` field is a pointer into the `data` field, which +//! we could imagine being used to track a sliding window of `data` in parser +//! code. +//! +//! As mentioned before, this pattern is used extensively by compiler-generated +//! [`Future`]s. //! //! ```rust //! use std::pin::Pin; //! use std::marker::PhantomPinned; //! use std::ptr::NonNull; //! -//! // This is a self-referential struct because the slice field points to the data field. -//! // We cannot inform the compiler about that with a normal reference, -//! // as this pattern cannot be described with the usual borrowing rules. -//! // Instead we use a raw pointer, though one which is known not to be null, -//! // as we know it's pointing at the string. +//! /// This is a self-referential struct because `self.slice` points into `self.data`. //! struct Unmovable { -//! data: String, -//! slice: NonNull, +//! /// Backing buffer. +//! data: [u8; 64], +//! /// Points at `self.data` which we know is itself non-null. Raw pointer because we can't do +//! /// this with a normal reference. +//! slice: NonNull<[u8]>, +//! /// Suppress `Unpin` so that this cannot be moved out of a `Pin` once constructed. //! _pin: PhantomPinned, //! } //! //! impl Unmovable { -//! // To ensure the data doesn't move when the function returns, -//! // we place it in the heap where it will stay for the lifetime of the object, -//! // and the only way to access it would be through a pointer to it. -//! fn new(data: String) -> Pin> { +//! /// Create a new `Unmovable`. +//! /// +//! /// To ensure the data doesn't move we place it on the heap behind a pointer which can +//! /// itself be moved. +//! fn new() -> Pin> { //! let res = Unmovable { -//! data, -//! // we only create the pointer once the data is in place -//! // otherwise it will have already moved before we even started -//! slice: NonNull::dangling(), +//! data: [0; 64], +//! // We only create the pointer once the data is in place +//! // otherwise it will have already moved before we even started. +//! slice: NonNull::from(&mut []), //! _pin: PhantomPinned, //! }; //! let mut boxed = Box::pin(res); //! //! let slice = NonNull::from(&boxed.data); -//! // we know this is safe because modifying a field doesn't move the whole struct +//! // We know this is safe, because modifying a field doesn't move the whole +//! // struct. //! unsafe { //! let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut boxed); //! Pin::get_unchecked_mut(mut_ref).slice = slice; @@ -110,79 +238,232 @@ //! } //! } //! -//! let unmoved = Unmovable::new("hello".to_string()); -//! // The pointer should point to the correct location, -//! // so long as the struct hasn't moved. +//! let unmoved = Unmovable::new(); +//! // The pointer should point to the correct location, so long as the struct hasn't moved. //! // Meanwhile, we are free to move the pointer around. //! # #[allow(unused_mut)] //! let mut still_unmoved = unmoved; //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data)); //! -//! // Since our type doesn't implement Unpin, this will fail to compile: -//! // let mut new_unmoved = Unmovable::new("world".to_string()); +//! // Since our type doesn't implement `Unpin`, this will fail to compile. +//! // We cannot mutably dereference a `Pin` to `!Unpin` data. +//! // let mut new_unmoved = Unmovable::new(); //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved); //! ``` //! -//! # Example: intrusive doubly-linked list +//! ## Intrusive, doubly-linked list +//! [linked-list]: #an-intrusive-doubly-linked-list +//! [`Node`]: #an-intrusive-doubly-linked-list +//! [`List`]: #an-intrusive-doubly-linked-list +//! [`&mut Node`]: #an-intrusive-doubly-linked-list +//! [`List::append`]: #an-intrusive-doubly-linked-list //! -//! In an intrusive doubly-linked list, the collection does not actually allocate -//! the memory for the elements itself. Allocation is controlled by the clients, -//! and elements can live on a stack frame that lives shorter than the collection does. //! -//! To make this work, every element has pointers to its predecessor and successor in -//! the list. Elements can only be added when they are pinned, because moving the elements -//! around would invalidate the pointers. Moreover, the [`Drop`][Drop] implementation of a linked -//! list element will patch the pointers of its predecessor and successor to remove itself -//! from the list. +//! In an intrusive doubly-linked list, the collection does not actually allocate the memory for the +//! nodes itself. Allocation is controlled by the clients, and nodes can live on a stack frame +//! that lives shorter than the collection does provided the nodes are removed from the +//! collection before returning. //! -//! Crucially, we have to be able to rely on [`drop`] being called. If an element -//! could be deallocated or otherwise invalidated without calling [`drop`], the pointers into it -//! from its neighboring elements would become invalid, which would break the data structure. +//! Every node has pointers to its predecessor and successor in the list. Nodes can only be +//! added when they are pinned, because moving the nodes around would invalidate the pointers. +//! The [`Drop`] implementation of a linked list node will patch the pointers of its predecessor +//! and successor to remove itself from the list; not doing so would result in a use-after-free when +//! the list went to refer to the destroyed node. //! -//! Therefore, pinning also comes with a [`drop`]-related guarantee. +//! ```rust +//! use std::marker::PhantomPinned; +//! use std::pin::Pin; +//! use std::ptr; //! -//! # `Drop` guarantee +//! /// The list itself is Unpin, because it doesn't actually hold any data that cannot move +//! /// (although, if nodes held a reference back into it, it would need to be `!Unpin`). +//! /// +//! /// Holds the first and last nodes in the list; they are either both null or both non-null. +//! struct List { +//! start: *mut Node, +//! end: *mut Node, +//! } //! -//! The purpose of pinning is to be able to rely on the placement of some data in memory. -//! To make this work, not just moving the data is restricted; deallocating, repurposing, or -//! otherwise invalidating the memory used to store the data is restricted, too. -//! Concretely, for pinned data you have to maintain the invariant -//! that *its memory will not get invalidated or repurposed from the moment it gets pinned until -//! when [`drop`] is called*. Only once [`drop`] returns or panics, the memory may be reused. +//! impl List { +//! fn new() -> Self { +//! Self { start: ptr::null_mut(), end: ptr::null_mut() } +//! } //! -//! Memory can be "invalidated" by deallocation, but also by -//! replacing a [Some]\(v) by [`None`], or calling [`Vec::set_len`] to "kill" some -//! elements off of a vector. It can be repurposed by using [`ptr::write`] to overwrite it without -//! calling the destructor first. None of this is allowed for pinned data without calling [`drop`]. +//! /// Appends the pinned `node` to the end of the list. +//! /// +//! /// For this function to be correct, we need two guarantees: +//! /// +//! /// 1. `node` never moves again so that our raw pointers to it are always valid +//! /// 2. If a `Node`'s memory would be re-used, its destructor gets run first, removing the +//! /// would-be-dangling references from the list. +//! fn append(&mut self, node: Pin<&mut Node>) { +//! // We could make `List: !Unpin` and track *which* list owns a node if we were fancier. +//! assert!( +//! node.pred.is_null() && node.succ.is_null(), +//! "Node must not already be in another list.", +//! ); //! -//! This is exactly the kind of guarantee that the intrusive linked list from the previous -//! section needs to function correctly. +//! unsafe { +//! // Unpin the `&mut Node`. This is safe, because we're not actually +//! // moving the value, only modifying the pointers inside. This +//! // reference cannot escape this function. +//! let node = Pin::get_unchecked_mut(node); //! -//! Notice that this guarantee does *not* mean that memory does not leak! It is still -//! completely okay to not ever call [`drop`] on a pinned element (e.g., you can still -//! call [`mem::forget`] on a [Pin]<[Box]\>). In the example of the doubly-linked -//! list, that element would just stay in the list. However you must not free or reuse the storage -//! *without calling [`drop`]*. +//! // Rearrange the pointers as appropriate for a doubly-linked list. +//! if self.start.is_null() { +//! self.start = node; +//! } else { +//! (*self.end).succ = node; +//! node.pred = self.end; +//! } +//! self.end = node; +//! } +//! } //! -//! # `Drop` implementation +//! /// Allocates a node on the heap and appends it to the end of the list. +//! fn append_boxed(&mut self) -> Pin> { +//! let mut node = Box::pin(Node { +//! pred: ptr::null_mut(), +//! succ: ptr::null_mut(), +//! data: Data::new(), +//! _pin: PhantomPinned, +//! }); +//! self.append(node.as_mut()); +//! node +//! } +//! } //! -//! If your type uses pinning (such as the two examples above), you have to be careful -//! when implementing [`Drop`][Drop]. The [`drop`] function takes [&mut] self, but this -//! is called *even if your type was previously pinned*! It is as if the -//! compiler automatically called [`Pin::get_unchecked_mut`]. +//! # struct Data; +//! # impl Data { fn new() -> Self { Data } } +//! struct Node { +//! pred: *mut Node, +//! succ: *mut Node, +//! data: Data, +//! /// `Node: Unpin` because `List` expects `Pin`ned pointers to them to remain in place. +//! _pin: PhantomPinned, +//! } //! +<<<<<<< HEAD //! This can never cause a problem in safe code because implementing a type that //! relies on pinning requires unsafe code, but be aware that deciding to make //! use of pinning in your type (for example by implementing some operation on //! [Pin]<[&]Self> or [Pin]<[&mut] Self>) has consequences for your //! [`Drop`][Drop] implementation as well: if an element of your type could have been pinned, //! you must treat [`Drop`][Drop] as implicitly taking [Pin]<[&mut] Self>. +======= +//! impl Drop for Node { +//! /// Remove pointers to `self`, allowing reuse of this memory without clients seeing garbage. +//! fn drop(&mut self) { +//! if self.pred.is_null() || self.succ.is_null() { +//! // Not included: code to remove `self` if it is the head or tail of the list. +//! return; +//! } +>>>>>>> 389e1e2452d (Rewrite `Pin

` docs to clarify guarantees and uses) +//! +//! unsafe { +//! (*self.pred).succ = self.succ; +//! (*self.succ).pred = self.pred; +//! } +//! } +//! } +//! ``` +//! +//! For this to work, a [`drop`-related guarantee][drop-guarantee] is required. If a node could +//! be deallocated or otherwise invalidated without calling [`drop`], the pointers into it from its +//! neighboring elements would become invalid, which would break the data structure. +//! +//! [`List`] itself is *not* address-sensitive. +//! +//! # Subtle details +//! [subtle-details]: #subtle-details +//! +//! [`List::append`] above relies on both of [`Pin

`]'s guarantees: +//! +//! 1. *Address Stability.* If [`unsafe`] code witnesses any [`p: Pin

`][Pin] at any time then +//! it may assume that `p.as_ref().get_ref() as *const _` will remain valid, pointing to the +//! same object until the end of that object's lifetime. +//! 2. *Notice of Destruction.* If `x: T` was ever reachable through any [`Pin

`] type, its +//! destructor must be run (until it either returns or panics) before `x`'s storage can be +//! overwritten. The "until further notice" in (1) includes this mandatory destruction. This is +//! often called the "[`Drop`] guarantee". +//! +//! ## Address Stability +//! [address-stability]: #address-stability +//! +//! The precise meaning of "address stability" is subtle, because "the same object" is not well-defined. +//! It is easiest to reason about it in terms of *visibility of mutations*. If [`unsafe`] code mutates +//! through a [`Pin

`], all code that stashed a raw pointer into it will see the mutation. In other +//! words, [`unsafe`] code can rely on the same value in memory being updated by all uses of a particular +//! [`Pin

`], not to mention that those stashed raw pointers remain valid in the first place. +//! +//! When a [`List`] stores a [`Node`], it needs to assume that appending a second node will mutate the +//! first node, so that later, when the first node is removed, it knows that its predecessor is the +//! second node. //! -//! For example, you could implement [`Drop`][Drop] as follows: +//! When writing generic code, it's not possible to know what [`unsafe`] code has recorded about the +//! pointee's address, so it must be very careful to observe this invariant. Thankfully, most of this +//! is already enforced by the [`Pin

`] API, so only [`unsafe`] code needs to worry about this. +//! +//! ## Notice of Destruction +//! [drop-guarantee]: #notice-of-destruction +//! +//! There needs to be a way for a pinned value to notify any [`unsafe`] code that recorded its address +//! that it is about to be destroyed, so that they can remove its address from their data structures. +//! Thus, in any situation where it would be safe to overwrite a pinned value, the destructor must +//! be called beforehand. +//! +//! The most common storage-reuse situation is when a value on the stack is destroyed as part of a +//! function return, or when heap storage is freed. In both cases, the destructor gets run for us +//! by Rust. However, for heap storage, [`unsafe`] code must make sure to call [`ptr::drop_in_place`] +//! if it wishes to use the [`std::alloc`] APIs manually. +//! +//! However, reuse can happen even if not storage is de-allocated. For example, when a [`Some`] +//! is overwritten by [`None`] using [`ptr::write`], or when [`Vec::set_len`] is used to manually +//! "kill" some elements of a vector. Both of these cases are somewhat contrived, but it is crucial +//! to remember to run destructors of [`Pin`]ned data. As a corollary, the following code can *never* be +//! made safe: +//! +//! ```rust +//! # use std::mem::ManuallyDrop; +//! # use std::pin::Pin; +//! # struct Type; +//! let mut pinned = Box::pin(ManuallyDrop::new(Type)); +//! let inner = unsafe { +//! Pin::map_unchecked_mut(Pin::as_mut(&mut pinned), |x| &mut *x) +//! }; +//! ``` +//! +//! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run, even though +//! normally [`Box`] drops the `T` before freeing the storage. +//! +//! Of course, *leaking* memory is still fine: [`mem::forget`]ing a [`Box`] +//! prevents its storage from ever getting re-used, so destruction notice does not apply. +//! +//! # Implementing an address-sensitive type. +//! +//! This section goes into detail on important considerations for implementing your own +//! address-sensitive types, which are different from merely using [`Pin

`] in a generic +//! way. +//! +//! ## Implementing [`Drop`] for an `!Unpin` Type +//! [drop-impl]: #implementing-drop-for-an-unpin-type +//! +//! The [`drop`] function takes [`&mut self`], but this is called *even if your +//! type was previously pinned*! Implementing [`Drop`] requires some care, since it is as if +//! the compiler automatically called [`Pin::get_unchecked_mut`]. +//! This can never cause a problem in safe code, because implementing an address-sensitive type +//! requires unsafe code (such as the [linked list above][linked-list]). +//! +//! Beware that deciding to make your type address-sensitive by implementing some operation on +//! [Pin]<[&Self][&]> or [Pin]<[`&mut Self`]> has consequences for your +//! [`Drop`] implementation as well: if an element of your type could have been pinned, +//! you must treat [`Drop`] as implicitly taking [Pin]<[`&mut Self`]>. +//! +//! You should implement [`Drop`] as follows: //! //! ```rust,no_run //! # use std::pin::Pin; -//! # struct Type { } +//! # struct Type; //! impl Drop for Type { //! fn drop(&mut self) { //! // `new_unchecked` is okay because we know this value is never used @@ -195,72 +476,136 @@ //! } //! ``` //! -//! The function `inner_drop` has the type that [`drop`] *should* have, so this makes sure that +//! The function `inner_drop` has the type that [`drop`] *should* have. This makes sure that //! you do not accidentally use `self`/`this` in a way that is in conflict with pinning. //! -//! Moreover, if your type is `#[repr(packed)]`, the compiler will automatically +//! Moreover, if your type is [`#[repr(packed)]`][packed], the compiler will automatically //! move fields around to be able to drop them. It might even do //! that for fields that happen to be sufficiently aligned. As a consequence, you cannot use -//! pinning with a `#[repr(packed)]` type. -//! -//! # Projections and Structural Pinning -//! -//! When working with pinned structs, the question arises how one can access the -//! fields of that struct in a method that takes just [Pin]<[&mut] Struct>. -//! The usual approach is to write helper methods (so called *projections*) -//! that turn [Pin]<[&mut] Struct> into a reference to the field, but what type should -//! that reference have? Is it [Pin]<[&mut] Field> or [&mut] Field? -//! The same question arises with the fields of an `enum`, and also when considering -//! container/wrapper types such as [Vec]\, [Box]\, -//! or [RefCell]\. (This question applies to both mutable and shared references, -//! we just use the more common case of mutable references here for illustration.) -//! -//! It turns out that it is actually up to the author of the data structure to decide whether -//! the pinned projection for a particular field turns [Pin]<[&mut] Struct> -//! into [Pin]<[&mut] Field> or [&mut] Field. There are some -//! constraints though, and the most important constraint is *consistency*: -//! every field can be *either* projected to a pinned reference, *or* have -//! pinning removed as part of the projection. If both are done for the same field, -//! that will likely be unsound! -//! -//! As the author of a data structure you get to decide for each field whether pinning +//! pinning with a [`#[repr(packed)]`][packed] type. +//! +//! ## "Assigning" pinned data +//! +//! Although in general it is not valid to swap data through a [`Pin

`], or assign from +//! a [`Pin

`], for the same reason that a *move* is invalid, there is no particular reason +//! to disallow doing it with specialized functions, as long as they know how to update all +//! uses of the pinned address (and any other `unsafe`-assumed invariants). For [`Unmovable`], +//! we could write +//! +//! ``` +//! # use std::pin::Pin; +//! # use std::marker::PhantomPinned; +//! # use std::ptr::NonNull; +//! # struct Unmovable { +//! # data: [u8; 64], +//! # slice: NonNull<[u8]>, +//! # _pin: PhantomPinned, +//! # } +//! # +//! impl Unmovable { +//! // Copies the contents of `src` into `self`, fixing up the self-pointer +//! // in the process. +//! fn assign(self: Pin<&mut Self>, src: Pin<&mut Self>) { +//! unsafe { +//! let unpinned_self = Pin::into_inner_unchecked(self); +//! let unpinned_src = Pin::into_inner_unchecked(src); +//! *unpinned_self = Self { +//! data: unpinned_src.data, +//! slice: NonNull::from(&mut []), +//! _pin: PhantomPinned, +//! }; +//! +//! let data_ptr = unpinned_src.data.as_ptr() as *const u8; +//! let slice_ptr = unpinned_src.slice.as_ptr() as *const u8; +//! let offset = slice_ptr.offset_from(data_ptr) as usize; +//! let len = (*unpinned_src.slice.as_ptr()).len(); +//! +//! unpinned_self.slice = NonNull::from(&mut unpinned_self.data[offset..offset+len]); +//! } +//! } +//! } +//! ``` +//! +//! Even though we can't have the compiler do the assignment for us, it's possible to write +//! such specialized functions for types that might need it. It wouldn't be too difficult +//! implement such a function for the [`Node`], either. +//! +//! Note that it _is_ possible to assign through a [`Pin

`] by way of [`Pin::set()`]. This does +//! not violate any guarantees, since it will run the destructor of the pointee before assigning +//! the new value. +//! +//! ## Projections and Structural Pinning +//! +//! With ordinary structs, it is natural that we want to add *projection* methods +//! that select one of the fields: +//! +//! ``` +//! # struct Field; +//! struct Struct { +//! field: Field, +//! // ... +//! } +//! +//! impl Struct { +//! fn field(&mut self) -> &mut Field { &mut self.field } +//! } +//! ``` +//! +//! When working with address-sensitive types, it's not obvious what the signature of these +//! functions should be. If `field` takes self: [Pin]<[&mut Struct][&mut]>, should it return +//! [`&mut Field`] or [Pin]<[`&mut Field`]>? This question also arises with `enum`s and +//! wrapper types like [`Vec`], [`Box`], and [`RefCell`]. (This question +//! applies just as well to shared references, but we'll examine the more common case +//! of mutable references for illustration). +//! +//! It turns out that it's up to the author of `Struct` to decide which type the projection +//! should produce. The choice must be *consistent* though: each field should only ever +//! be projected as pinned or unpinned; both together will likely be unsound! +//! +//! As the author of a data structure, you get to decide for each field whether pinning //! "propagates" to this field or not. Pinning that propagates is also called "structural", //! because it follows the structure of the type. -//! In the following subsections, we describe the considerations that have to be made -//! for either choice. //! -//! ## Pinning *is not* structural for `field` +//! The choice of whether to pin depends on how the type is being used. If unsafe code +//! that consumes [Pin]\<[&mut Struct][&mut]> also needs to take note of +//! the address of the field itself, it may be evidence that that field is structurally +//! pinned. Unfortunately, there are no hard-and-fast rules. //! -//! It may seem counter-intuitive that the field of a pinned struct might not be pinned, -//! but that is actually the easiest choice: if a [Pin]<[&mut] Field> is never created, -//! nothing can go wrong! So, if you decide that some field does not have structural pinning, -//! all you have to ensure is that you never create a pinned reference to that field. +//! ### When pinning *is not* structural for `field`... +//! +//! While counter-intuitive, it's actually the easier choice: if a [Pin]<[`&mut Field`]> +//! is never created, nothing can go wrong! So, if you decide that some field does not have +//! structural pinning, all you have to ensure is that you never create a pinned reference to that field. //! //! Fields without structural pinning may have a projection method that turns -//! [Pin]<[&mut] Struct> into [&mut] Field: +//! [Pin]<[&mut Struct][&mut]> into [`&mut Field`]: //! //! ```rust,no_run //! # use std::pin::Pin; //! # type Field = i32; //! # struct Struct { field: Field } //! impl Struct { -//! fn pin_get_field(self: Pin<&mut Self>) -> &mut Field { +//! fn field(self: Pin<&mut Self>) -> &mut Field { //! // This is okay because `field` is never considered pinned. //! unsafe { &mut self.get_unchecked_mut().field } //! } //! } //! ``` //! -//! You may also impl [Unpin] for Struct *even if* the type of `field` +//! You may also impl [Unpin] for Struct {} *even if* the type of `field` //! is not [`Unpin`]. What that type thinks about pinning is not relevant -//! when no [Pin]<[&mut] Field> is ever created. +//! when no [Pin]<[`&mut Field`]> is ever created. +//! +//! For example, the `data` field of [`Node`] does *not* need +//! to be structurally pinned, because neither [`List`] nor +//! [`Node`] assume anything about it. //! -//! ## Pinning *is* structural for `field` +//! ### When pinning *is* structural for `field`... //! //! The other option is to decide that pinning is "structural" for `field`, //! meaning that if the struct is pinned then so is the field. //! -//! This allows writing a projection that creates a [Pin]<[&mut] Field>, thus +//! This allows writing a projection that creates a [Pin]<[`&mut Field`]>, thus //! witnessing that the field is pinned: //! //! ```rust,no_run @@ -268,83 +613,98 @@ //! # type Field = i32; //! # struct Struct { field: Field } //! impl Struct { -//! fn pin_get_field(self: Pin<&mut Self>) -> Pin<&mut Field> { +//! fn field(self: Pin<&mut Self>) -> Pin<&mut Field> { //! // This is okay because `field` is pinned when `self` is. //! unsafe { self.map_unchecked_mut(|s| &mut s.field) } //! } //! } //! ``` //! -//! However, structural pinning comes with a few extra requirements: +//! For example, the `prev` and `succ` fields of a [`Node`] +//! are always either null or valid, so [`Node`] could provide a projection with +//! type fn([Pin]<[`&mut Node`]>) -> [Pin]<[`&mut Node`]> for each +//! of them. These fields need to be structurally-pinned, since the outer [`List`] +//! assumes every [`Node`] in it is pinned. //! -//! 1. The struct must only be [`Unpin`] if all the structural fields are -//! [`Unpin`]. This is the default, but [`Unpin`] is a safe trait, so as the author of -//! the struct it is your responsibility *not* to add something like -//! impl\ [Unpin] for Struct\. (Notice that adding a projection operation -//! requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break +//! Structural pinning comes with a few extra requirements: +//! +//! 1. *Structural [`Unpin`].* A struct can be [`Unpin`] if, and only if, all of its +//! structurally-pinned fields are, too. This is [`Unpin`]'s behavior by default. +//! However, as author, it is your responsibility to not write something like +//! unsafe impl\ [Unpin] for Struct\ {}. (Adding *any* projection +//! operation requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break //! the principle that you only have to worry about any of this if you use [`unsafe`].) -//! 2. The destructor of the struct must not move structural fields out of its argument. This -//! is the exact point that was raised in the [previous section][drop-impl]: [`drop`] takes -//! [&mut] self, but the struct (and hence its fields) might have been pinned -//! before. You have to guarantee that you do not move a field inside your [`Drop`][Drop] -//! implementation. In particular, as explained previously, this means that your struct -//! must *not* be `#[repr(packed)]`. -//! See that section for how to write [`drop`] in a way that the compiler can help you -//! not accidentally break pinning. -//! 3. You must make sure that you uphold the [`Drop` guarantee][drop-guarantee]: -//! once your struct is pinned, the memory that contains the -//! content is not overwritten or deallocated without calling the content's destructors. -//! This can be tricky, as witnessed by [VecDeque]\: the destructor of -//! [VecDeque]\ can fail to call [`drop`] on all elements if one of the -//! destructors panics. This violates the [`Drop`][Drop] guarantee, because it can lead to -//! elements being deallocated without their destructor being called. -//! ([VecDeque]\ has no pinning projections, so this -//! does not cause unsoundness.) -//! 4. You must not offer any other operations that could lead to data being moved out of +//! +//! 2. *Pinned Destruction.* As discussed [above][drop-impl], [`drop`] takes +//! [`&mut self`], but the struct (and hence its fields) might have been pinned +//! before. The destructor must be written as if its argument was +//! self: [Pin]\<[`&mut Self`]>, instead. +//! +//! As a consequence, the struct *must not* be [`#[repr(packed)]`][packed]. +//! +//! 3. *Structural Notice of Destruction.* You must uphold the the [`Drop` guarantee][drop-guarantee]: +//! once your struct is pinned, the struct's storage cannot be re-used without calling the +//! structurally-pinned fields' destructors, too. +//! +//! This can be tricky, as witnessed by [`VecDeque`]: the destructor of [`VecDeque`] +//! can fail to call [`drop`] on all elements if one of the destructors panics. This violates +//! the [`Drop` guarantee][drop-guarantee], because it can lead to elements being deallocated without +//! their destructor being called. +//! +//! [`VecDeque`] has no pinning projections, so its destructor is sound. If it wanted +//! to provide such structural pinning, its destructor would need to abort the process if any +//! of the destructors panicked. +//! +//! 4. You must not offer any other operations that could lead to data being *moved* out of //! the structural fields when your type is pinned. For example, if the struct contains an -//! [Option]\ and there is a [`take`][Option::take]-like operation with type -//! fn([Pin]<[&mut] Struct\>) -> [Option]\, -//! that operation can be used to move a `T` out of a pinned `Struct` – which means -//! pinning cannot be structural for the field holding this data. +//! [`Option`] and there is a [`take`][Option::take]-like operation with type +//! fn([Pin]<[&mut Struct\][&mut]>) -> [`Option`], +//! then that operation can be used to move a `T` out of a pinned `Struct` – which +//! means pinning cannot be structural for the field holding this data. //! //! For a more complex example of moving data out of a pinned type, -//! imagine if [RefCell]\ had a method -//! fn get_pin_mut(self: [Pin]<[&mut] Self>) -> [Pin]<[&mut] T>. +//! imagine if [`RefCell`] had a method +//! fn get_pin_mut(self: [Pin]<[`&mut Self`]>) -> [Pin]<[`&mut T`]>. //! Then we could do the following: //! ```compile_fail +//! # use std::cell::RefCell; +//! # use std::pin::Pin; //! fn exploit_ref_cell(rc: Pin<&mut RefCell>) { -//! { let p = rc.as_mut().get_pin_mut(); } // Here we get pinned access to the `T`. -//! let rc_shr: &RefCell = rc.into_ref().get_ref(); -//! let b = rc_shr.borrow_mut(); -//! let content = &mut *b; // And here we have `&mut T` to the same data. +//! // Here we get pinned access to the `T`. +//! let _: Pin<&mut T> = rc.as_mut().get_pin_mut(); +//! +//! // And here we have `&mut T` to the same data. +//! let shared: &RefCell = rc.into_ref().get_ref(); +//! let borrow = shared.borrow_mut(); +//! let content = &mut *borrow; //! } //! ``` -//! This is catastrophic, it means we can first pin the content of the -//! [RefCell]\ (using [RefCell]::get_pin_mut) and then move that +//! This is catastrophic: it means we can first pin the content of the +//! [`RefCell`] (using [RefCell]::get_pin_mut) and then move that //! content using the mutable reference we got later. //! -//! ## Examples +//! ### Structural Pinning examples //! -//! For a type like [Vec]\, both possibilities (structural pinning or not) make -//! sense. A [Vec]\ with structural pinning could have `get_pin`/`get_pin_mut` +//! For a type like [`Vec`], both possibilities (structural pinning or not) make +//! sense. A [`Vec`] with structural pinning could have `get_pin`/`get_pin_mut` //! methods to get pinned references to elements. However, it could *not* allow calling -//! [`pop`][Vec::pop] on a pinned [Vec]\ because that would move the (structurally +//! [`pop`][Vec::pop] on a pinned [`Vec`] because that would move the (structurally //! pinned) contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also //! move the contents. //! -//! A [Vec]\ without structural pinning could -//! impl\ [Unpin] for [Vec]\, because the contents are never pinned -//! and the [Vec]\ itself is fine with being moved as well. +//! A [`Vec`] without structural pinning could +//! impl\ [Unpin] for [`Vec`], because the contents are never pinned +//! and the [`Vec`] itself is fine with being moved as well. //! At that point pinning just has no effect on the vector at all. //! //! In the standard library, pointer types generally do not have structural pinning, -//! and thus they do not offer pinning projections. This is why [Box]\: [Unpin] +//! and thus they do not offer pinning projections. This is why [`Box`]: [Unpin] //! holds for all `T`. It makes sense to do this for pointer types, because moving the -//! [Box]\ does not actually move the `T`: the [Box]\ can be freely -//! movable (aka [`Unpin`]) even if the `T` is not. In fact, even [Pin]<[Box]\> and -//! [Pin]<[&mut] T> are always [`Unpin`] themselves, for the same reason: +//! [`Box`] does not actually move the `T`: the [`Box`] can be freely +//! movable (aka [`Unpin`]) even if the `T` is not. In fact, even [Pin]<[`Box`]> and +//! [Pin]<[`&mut T`]> are always [`Unpin`] themselves, for the same reason: //! their contents (the `T`) are pinned, but the pointers themselves can be moved without moving -//! the pinned data. For both [Box]\ and [Pin]<[Box]\>, +//! the pinned data. For both [`Box`] and [Pin]<[`Box`]>, //! whether the content is pinned is entirely independent of whether the //! pointer is pinned, meaning pinning is *not* structural. //! @@ -352,9 +712,29 @@ //! for the nested futures, as you need to get pinned references to them to call [`poll`]. //! But if your combinator contains any other data that does not need to be pinned, //! you can make those fields not structural and hence freely access them with a -//! mutable reference even when you just have [Pin]<[&mut] Self> (such as in your own -//! [`poll`] implementation). +//! mutable reference even when you just have [Pin]<[`&mut Self`]> +//! (such as in your own [`poll`] implementation). //! +//! [Target]: Deref::Target "ops::Deref::Target" +//! [`drop`]: Drop::drop "ops::Drop::drop" +//! [`poll`]: Future::poll "future::Future::poll" +//! +//! +//! [`std::alloc`]: ../../std/alloc/index.html +//! [`Box`]: ../../std/boxed/struct.Box.html +//! [`Rc`]: ../../std/rc/struct.Rc.html +//! [`Vec`]: ../../std/vec/struct.Vec.html +//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop +//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push +//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len +//! [`VecDeque`]: ../../std/collections/struct.VecDeque.html +//! +//! [`&mut T`]: &mut +//! [`&mut self`]: &mut +//! [`&mut Self`]: &mut +//! [`&mut Field`]: &mut +//! +<<<<<<< HEAD //! [Deref]: crate::ops::Deref "ops::Deref" //! [`Deref`]: crate::ops::Deref "ops::Deref" //! [Target]: crate::ops::Deref::Target "ops::Deref::Target" @@ -377,7 +757,10 @@ //! [`poll`]: crate::future::Future::poll "future::Future::poll" //! [&]: reference "shared reference" //! [&mut]: reference "mutable reference" +======= +>>>>>>> 389e1e2452d (Rewrite `Pin

` docs to clarify guarantees and uses) //! [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe" +//! [packed]: https://doc.rust-lang.org/nomicon/other-reprs.html#reprpacked #![stable(feature = "pin", since = "1.33.0")] @@ -386,6 +769,14 @@ use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::ops::{CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Receiver}; +#[allow(unused_imports)] +use crate::{ + cell::{RefCell, UnsafeCell}, + future::Future, + marker::PhantomPinned, + mem, ptr, +}; + /// A pinned pointer. /// /// This is a wrapper around a kind of pointer which makes that pointer "pin" its From 8241ca6056bfcdc9aa600c2de6fe8d1fa8baed01 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Mon, 25 Sep 2023 02:08:32 +0200 Subject: [PATCH 04/40] mostly done --- library/core/src/pin.rs | 707 ++++++++++++++++++++-------------------- 1 file changed, 359 insertions(+), 348 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 2632a44a2aeba..a75660522a10b 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -2,16 +2,20 @@ //! //! It is sometimes useful to be able to rely upon a certain value not being able to *move*, //! in the sense that its address in memory cannot change. This is useful specifically when there -//! is a *pointer* that is pointing at that value. The ability to rely on this guarantee that -//! the *value a pointer is pointing at* (its *pointee*) will not move is necessary to implement -//! things like self-referential structs, as moving an object with pointers that are meant to -//! point into that struct's own data would cause those pointers to no longer be valid (they -//! would still be pointing at the struct's old location in memory). -//! -//! "Pinning" allows us to put a value *which is being pointed at* by some pointer `Ptr` into a state -//! that prevents safe code from *moving* the *pointee* value. In this way, we can allow [`unsafe`] code -//! to rely on the pointer not having its pointee moved out from under it. -//! +//! are one or more *pointers* pointing at that value. The ability to rely on this guarantee that +//! the value a pointer is pointing at (its **pointee**) will +//! +//! 1. Not be *moved* out of its memory location +//! 2. More generally, remain *valid* at that same memory location +//! +//! is necessary to implement things like self-referential structs and intrusive data structures. +//! +//! "Pinning" allows us to put a value *which is being pointed at* by some pointer `Ptr` into a +//! state that prevents safe code from *moving* or otherwise invalidating the *pointee* value at +//! its location in memory (unless the pointee type implements [`Unpin`], which we'll +//! [discuss more below][self#unpin]). In this way, we can allow [`unsafe`] code to rely on the +//! pointer to be valid to dereference. +//! //! The rest of this documentation is intended to be the source of truth for users of [`Pin`] //! in unsafe code; users of [`Pin`] in safe code do not need to read it in detail. //! @@ -19,7 +23,7 @@ //! //! * [What is "*moving*"?][what-is-moving] //! * [What is "pinning"?][what-is-pinning] -//! * [Examples of types that have address-sensitive states][address-sensitive-examples] +//! * [Examples of types with address-sensitive states][address-sensitive-examples] //! * [Self-referential struct][self-ref] //! * [Intrusive, doubly-linked list][linked-list] //! * [Subtle Details][subtle-details] @@ -28,155 +32,262 @@ //! [what-is-moving]: self#what-is-moving //! //! When we say a value is *moved*, we mean that the compiler copies, byte-for-byte, the -//! value from one location to another. A move is mechanically identical to [`Copy`]ing -//! a value and discarding the old version of the data, thus making the moved-from value -//! inaccessible. Whenever we write *move* in italics, we mean this precise definition of -//! moving a value. +//! value from one location to another. In a purely mechanical sense, this is identical to +//! [`Copy`]ing a value from one place in memory to another. In Rust, "move" carries with it the +//! semantics of ownership transfer from one variable to another, which is the key difference +//! between a [`Copy`] and a move. For the purposes of this module's documentation, however, when +//! we write *move* in italics, we mean *specifically* that the value has moved in the mechanical +//! sense of being located at a new place in memory. //! //! All values in Rust are trivially *moveable*. This means that the address at which a value is -//! located is not necessarily stable in between borrows, and that the compiler is allowed to -//! *move* it to a new address without running any code to notify that value that its address -//! has changed. +//! located is not necessarily stable in between borrows. The compiler is allowed to *move* a value +//! to a new address without running any code to notify that value that its address +//! has changed. Although the compiler will not insert memory *moves* where no semantic move has +//! occurred, there are many places where a value *may* be moved. For example, when doing +//! assignment or passing a value into a function. //! //! ``` //! #[derive(Default)] //! struct AddrTracker(Option); //! //! impl AddrTracker { +//! // If we haven't checked the addr of self yet, store the current +//! // address. If we have, confirm that the current address is the same +//! // as it was last time, or else panic. //! fn check_for_move(&mut self) { //! let current_addr = self as *mut Self as usize; //! match self.0 { -//! None => self.0 = Some(addr), +//! None => self.0 = Some(current_addr), //! Some(prev_addr) => assert_eq!(prev_addr, current_addr), //! } //! } //! } -//! -//! fn take_and_return(tracker: AddrTracker) -> AddrTracker { -//! tracker -//! } //! +//! // Create a tracker and store the initial address //! let mut tracker = AddrTracker::default(); //! tracker.check_for_move(); -//! -//! let mut tracker = take_and_return(tracker); -//! +//! +//! // Here we shadow the variable. This carries a semantic move, and may therefore also +//! // come with a mechanical memory *move* +//! let mut tracker = tracker; +//! //! // May panic! //! // tracker.check_for_move(); //! ``` -//! -//! Although the compiler will not insert moves where no semantic move has occurred, there -//! are many places where a value *may* be moved. For example, when passed into a function. -//! In this sense, Rust does not guarantee that `check_for_move()` will -//! never panic, because the compiler is permitted to *move* `tracker` to enable -//! optimizations like pass-by-value. //! -//! Common smart-pointer types such as [`Box`] and [`&mut T`] allow removing and replacing the -//! values they contain: you can move out of a [`Box`], or you can use [`mem::swap`]. Putting -//! `tracker` behind a pointer isn't enough on its own to ensure that its address does not change. +//! In this sense, Rust does not guarantee that `check_for_move()` will never panic, because the +//! compiler is permitted to *move* `tracker` in many situations. +//! +//! Common smart-pointer types such as [`Box`] and [`&mut T`] also allow *moving* the underlying +//! *value* they point at: you can move out of a [`Box`], or you can use [`mem::replace`] to +//! move a `T` out of a [`&mut T`]. Therefore, putting a value (such as `tracker` above) behind a +//! pointer isn't enough on its own to ensure that its address does not change. //! //! # What is "pinning"? -//! [what-is-pinning]: #what-is-pinning +//! [what-is-pinning]: self#what-is-pinning //! -//! `Pin` wraps a pointer type `P`, but removes the ability to actually obtain a `P` from it in safe -//! code. By disallowing access to the type behind the pointer, `Pin` prevents us from using -//! operations like [`mem::swap`] to *move* out of the pointer. +//! We say that a value has been *pinned* when it has been put into a state where it is guaranteed +//! to remain *valid* and *located at the same place in memory* from the time it is pinned until its +//! [`drop`] is called. //! -//! ## Address-sensitive types +//! ## Address-sensitive values, AKA "why we need pinning" +//! [address-sensitive-values]: self#address-sensitive-values //! -//! One noteworthy example of types which require `Pin` for correctness are the [`Future`]` types -//! generated by the compiler for `async fn`s. +//! Most values in Rust are entirely okay with being *moved* around at-will. +//! Types for which it is *always* the case that *any* value of that type can be +//! *moved* at-will should implement [`Unpin`], which we will discuss more [below][self#unpin]. //! -//! For these types, any value which lives "across" an `.await` point is stored in the state -//! machine, including values which borrow from other variables in the `async fn`. *Moving* the -//! [`Future`] in between calls to [`poll`] would invalidate these pointers, leaving the next call -//! to [`poll`] with dangling references! +//! [`Pin`] is specifically targeted at allowing the implementation of *safe interfaces* around +//! types which have some state during which they become "address-sensitive." A value in such an +//! "address-sensitive" state is *not* okay with being *moved* around at-will. Such a value must +//! stay *un-moved* and valid during the address-sensitive portion of its lifetime because some +//! interface is relying on those invariants to be true in order for its implementation to be sound. //! -//! Such types are *address-sensitive*: they incorporate the address of `self` into an -//! operation. These types usually follow a common lifecycle: +//! As a motivating example of a type which may become address-sensitive, consider a type which +//! contains a pointer to another piece of its own data, *i.e.* a "self-referential" type. In order +//! such a type to be implemented soundly, the pointer which points into `self`'s data must be +//! proven valid whenever it is accessed. But if that value is *moved*, the pointer will still +//! point to the old location that the value was located and not into the new location of `self`, +//! thus becoming invalid. A key example of such self-referrential types are the state machines +//! generated by the compiler to implement [`Future`] for `async fn`s. +//! +//! Such types that have an *address-sensitive* state usually follow a lifecycle +//! that looks something like so: //! //! 1. A value is created which can be freely moved around. -//! * e.g. calling an async function -//! 2. An operation causes the value to depend on its own address using [`unsafe`]. +//! * e.g. calling an async function which returns a state machine implementing [`Future`] +//! 2. An operation causes the value to depend on its own address not changing //! * e.g. calling [`poll`] for the first time on the produced [`Future`] -//! 3. Further [`unsafe`] operations assume that its address is stable. +//! 3. Further pieces of the safe interface of the type use internal [`unsafe`] operations which +//! assume that the address of the value is stable //! * e.g. subsequent calls to [`poll`] -//! 4. The value is destroyed, undoing its address-sensitivity. +//! 4. Before the value is invalidated (e.g. deallocated), it is *dropped*, giving it a chance to +//! "unregister"/clear outstanding pointers to itself //! * e.g. [`drop`]ping the [`Future`] //! -//! [`Pin

`] helps us implement steps (2) and (3) safely. +//! There are two possible ways to ensure the invariants required for 2. and 3. above (which +//! apply to any address-sensitive type, not just self-referrential types) do not get broken. +//! +//! 1. Have the value detect when it is moved and update all the pointers that point to itself +//! 2. Guarantee that the address of the value does not change (and that memory is not re-used +//! for anything else) during the time that the pointers to it are expected to be valid to +//! dereference +//! +//! Since, as we discussed, Rust can move values without notifying them that they have moved, the +//! first option is ruled out. +//! +//! In order to implement the second option, we must in some way enforce its key invariant, +//! *i.e.* prevent the value from being *moved* or otherwise invalidated. (You may notice this +//! sounds an awful lot like the definition of *pinning* a value). There are two ways by +//! which we might enforce this validity invariant in Rust: +//! +//! 1. Offer a wholly `unsafe` API to interact with the object, thus requiring every caller to +//! uphold the invariant themselves; or, +//! 2. Leverage the type system to encode and enforce this invariant by presenting a restricted +//! API surface to interact with the object +//! +//! The first option is quite obviously undesirable, as the `unsafe`ty of the interface will +//! become viral throughout all code that interacts with the object. +//! +//! [`Pin`] is an implementation of the second option, allowing us to pin a value in place +//! until its [`drop`] runs in a way that we can depend on it staying valid in `unsafe` code. //! -//! ## `Pin` and pointers +//! ## Using [`Pin`] to pin values //! -//! [`Pin

`] can wrap any pointer type, forming a promise that the pointee will not be *moved*. -//! This promise must be upheld by [`unsafe`] code which interacts with the [`Pin

`] so that -//! [`unsafe`] code can place the pointee in an address-sensitive state that will not be broken -//! by a *move*. Operations on an address-sensitive type accept an argument like -//! [Pin]<[`&mut T`]> or [Pin]<[`Box`]> to indicate this contract to +//! In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a +//! [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** +//! will not be *moved* or [otherwise invalidated][self#subtle-details]. +//! +//! It is important to stress that the thing in the [`Pin`] is not the value which we want to pin +//! itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather +//! the pointer's ***pointee** value*. +//! +//! In order to accomplish the goal of pinning the pointee value, [`Pin`] restricts access to +//! the wrapped `Ptr` type in safe code. Specifically, [`Pin`] disallows the ability to access +//! the wrapped pointer in ways that would allow the user to *move* the underlying pointee value or +//! otherwise re-use that memory for something else without using [`unsafe`]. For example, a +//! [`Pin<&mut T>`] makes it impossible to obtain the wrapped [&mut] T safely because +//! through that [&mut] T it would be possible to *move* the underlying value out of +//! the pointer with [`mem::replace`], etc. +//! +//! This promise must be upheld manually by [`unsafe`] code which interacts with the [`Pin`] +//! so that other [`unsafe`] code can rely on the pointee value being *un-moved* and valid. +//! Interfaces that operate on values which are in an address-sensitive state accept an argument +//! like [Pin]<[&mut] T> or [Pin]<[Box]\> to indicate this contract to //! the caller. //! -//! Since [`Pin

`] can wrap any pointer type, it interacts with -//! [`Deref`] and [`DerefMut`]. A [`Pin

`] where [`P: Deref`][Deref] is a -//! "`P`-style pointer" to a pinned [`P::Target`][Target] – so, a -//! [Pin]<[`Box`]> is an owned pointer to a pinned `T`, and a -//! [Pin]<[`Rc`]> is a reference-counted pointer to a pinned `T`. +//! [As discussed below][drop-guarantee], this has consequences for running +//! destructors of pinned memory, too. +//! +//! ## Interaction between [`Deref`] and [`Pin`] +//! +//! Since [`Pin`] can wrap any pointer type, it uses [`Deref`] and [`DerefMut`] in +//! order to identify the type of the pinned pointee data and provide (restricted) access to it. //! -//! [`Pin

`] requires that implementations of [`Deref`] and [`DerefMut`] return a pointer to -//! pinned data when they are called on a pinned pointer and do not *move* out of their `self` -//! parameter. It is unsound for [`unsafe`] code to wrap such "evil" pointers; see -//! [`Pin

::new_unchecked`] for details. +//! A [`Pin`] where [`Ptr: Deref`][Deref] is a "`Ptr`-style pointer" to a pinned +//! [`P::Target`][Target] – so, a [Pin]<[Box]\> is an owned pointer to a pinned `T`, +//! and a [Pin]<[Rc]\> is a reference-counted pointer to a pinned `T`. //! -//! Pinning does not require any compiler "magic", only a specific contract between the library API -//! and its users. This differs from e.g. [`UnsafeCell`] which changes the semantics of a program's -//! compiled output. A [`Pin

`] is a handle to a value which does not allow moving the value out, -//! but Rust still considers all values themselves to be moveable with e.g. [`mem::swap`]. +//! [`Pin`] also uses the [`::Target`][Target] type information to modify the +//! interface it is allowed to provide for interacting with that data (for example, when a +//! [`Pin`]-wrapped pointer points at pinned data which implements [`Unpin`], as +//! [discussed below][self#unpin]). //! -//! These guarantees are necessary to make our `AddrTracker` example work. If any code -//! sees a [Pin]<&mut AddrTracker>, it can safely assume that it will *always* see -//! [the same object][address-stability] for the same address (for the lifetime of -//! the pointee). If we had written `check_for_move` above to accept a -//! [Pin]<[`&mut Self`]> instead, multiple calls to it *cannot* panic: +//! [`Pin`] requires that implementations of [`Deref`] and [`DerefMut`] on `Ptr` return a +//! pointer to the pinned data directly and do not *move* out of the `self` parameter during their +//! implementation of [`DerefMut::deref_mut`]. It is unsound for [`unsafe`] code to wrap pointer +//! types with such "malicious" implementations of [`Deref`]; see [`Pin::new_unchecked`] for +//! details. +//! +//! ## Pinning as a library contract +//! +//! Pinning does not require any compiler "magic"[^noalias], only a specific contract between the +//! library API and its users. This differs from e.g. [`UnsafeCell`] which changes the semantics of +//! a program's compiled output. A [`Pin`] is a handle to a value which does not allow moving +//! the value out, but Rust still considers all values themselves to be moveable with e.g. +//! [`mem::replace`]. +//! +//! [^noalias]: There is a bit of nuance here that is still being decided about what the aliasing +//! semantics of `Pin<&mut T>` should be, but this is true as of today. +//! +//! ## Fixing `AddrTracker` +//! +//! The guarantee of a stable address is necessary to make our `AddrTracker` example work. When +//! `check_for_move` sees a [Pin]<&mut AddrTracker>, it can safely assume that value +//! will exist at that same address until said value goes out of scope, and thus multiple calls +//! to it *cannot* panic. //! //! ``` -//! # use std::pin::Pin; -//! # #[derive(Default)] -//! # struct AddrTracker(usize); +//! use core::marker::PhantomPinned; +//! use core::pin::Pin; +//! use core::pin::pin; +//! +//! #[derive(Default)] +//! struct AddrTracker { +//! prev_addr: Option, +//! // remove auto-implemented Unpin bound to mark this type as having some +//! // address-sensitive state. This is discussed more below. +//! _pin: PhantomPinned, +//! } +//! //! impl AddrTracker { //! fn check_for_move(self: Pin<&mut Self>) { -//! unsafe { -//! let unpinned = Pin::get_unchecked_mut(self); -//! let addr = unpinned as *mut Self as usize; -//! match unpinned.0 { -//! 0 => unpinned.0 = addr, -//! x => assert_eq!(x, addr), -//! } +//! let current_addr = &*self as *const Self as usize; +//! match self.prev_addr { +//! None => { +//! // SAFETY: we do not move out of self +//! let self_data_mut = unsafe { self.get_unchecked_mut() }; +//! self_data_mut.prev_addr = Some(current_addr); +//! }, +//! Some(prev_addr) => assert_eq!(prev_addr, current_addr), //! } //! } //! } //! -//! let mut tracker = Box::pin(AddrTracker::default()); -//! tracker.as_mut().check_for_move(); -//! tracker.as_mut().check_for_move(); +//! // 1. Create the value, not yet in an address-sensitive state +//! let tracker = AddrTracker::default(); +//! +//! // 2. Pin the value by putting it behind a Pin-wrapped pointer, thus putting +//! // it into an address-sensitive state +//! let mut ptr_to_pinned_tracker: Pin<&mut AddrTracker> = pin!(tracker); +//! ptr_to_pinned_tracker.as_mut().check_for_move(); +//! +//! // Trying to access `tracker` or pass `ptr_to_pinned_tracker` to anything +//! // that requires mutable access to a non-pinned version of it will no longer +//! // compile +//! +//! // 3. We can now assume that the tracker value will never be moved, thus +//! // this will never panic! +//! ptr_to_pinned_tracker.as_mut().check_for_move(); //! ``` //! -//! [As discussed below][drop-guarantee], this has consequences for running -//! destructors of pinned memory, too. +//! Note that this invariant is enforced by simply making it impossible to call code that would +//! perform a move on the pinned value. This is the case since the only way to access that pinned +//! value is through the [`Pin`]-wrapped [`&mut T`], which in turn restricts our access. //! //! ## [`Unpin`] //! -//! The vast majority of Rust types are not address-sensitive; these types +//! The vast majority of Rust types have no address-sensitive states; these types //! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of -//! [`Pin

`]. When [`T: Unpin`][Unpin], [Pin]<[`Box`]> and -//! [`Box`] function identically, as do [Pin]<[`&mut T`]> and +//! [`Pin

`]. When [`T: Unpin`][Unpin], [Pin]<[Box]\> and +//! [`Box`] function identically, as do [Pin]<[&mut] T> and //! [`&mut T`]. //! -//! This includes all of the basic types, like [`bool`], [`i32`], and [`&T`][&], -//! as well as any other type consisting only of those types. You can opt out of -//! [`Unpin`] via the [`PhantomPinned`] marker type. +//! This includes all of the primitive types, like [`bool`], [`i32`], and [&]T, +//! as well as any other type consisting only of those types. //! -//! Pinning and [`Unpin`] only affect the pointee type [`P::Target`][Target], not the pointer type -//! `P` itself. For example, whether or not [`Box`] is [`Unpin`] has no effect on the behavior of -//! [Pin]<[`Box`]> because `T` is the pointee type. +//! Like other auto-traits, the compiler will automatically determine that a type implements +//! [`Unpin`] if all its fields also implement [`Unpin`]. If you are building a type which is built +//! of only [`Unpin`] types but has an address-sensistive state and thus should not itself +//! implement [`Unpin`], you can opt out of [`Unpin`] via adding a field with the +//! [`PhantomPinned`] marker type, as we did with our latest `AddrTracker` example above. +//! +//! Note that the interaction between a [`Pin`] and [`Unpin`] is through the **pointee type** +//! of the value behind the `Ptr`, [`::Target`][Target]. Whether the `Ptr` type itself +//! implements [`Unpin`] does not affect the behavior of a [`Pin`]. For example, whether or not +//! [`Box`] is [`Unpin`] has no effect on the behavior of [Pin]<[Box]\> because +//! `T` is the type of the pointee value, not [`Box`]. So, whether `T` implements [`Unpin`] is +//! the thing that will affect the behavior of the [Pin]<[Box]\>. //! //! # Examples of address-sensitive types //! [address-sensitive-examples]: #examples-of-address-sensitive-types @@ -215,212 +326,125 @@ //! impl Unmovable { //! /// Create a new `Unmovable`. //! /// -//! /// To ensure the data doesn't move we place it on the heap behind a pointer which can -//! /// itself be moved. +//! /// To ensure the data doesn't move we place it on the heap behind a Pin-wrapped pointer. +//! /// Note that the data is pinned, but the pointer to that data can itself still be moved, +//! /// which is important because it means we can return the pointer from the function. //! fn new() -> Pin> { //! let res = Unmovable { //! data: [0; 64], //! // We only create the pointer once the data is in place //! // otherwise it will have already moved before we even started. -//! slice: NonNull::from(&mut []), +//! slice: NonNull::dangling(), //! _pin: PhantomPinned, //! }; -//! let mut boxed = Box::pin(res); +//! // First we put the data in a box, which will be its final resting place +//! let mut boxed = Box::new(res); //! -//! let slice = NonNull::from(&boxed.data); -//! // We know this is safe, because modifying a field doesn't move the whole -//! // struct. -//! unsafe { -//! let mut_ref: Pin<&mut Self> = Pin::as_mut(&mut boxed); -//! Pin::get_unchecked_mut(mut_ref).slice = slice; -//! } -//! boxed +//! // Then we make the slice field point to the proper part of that boxed data. +//! // From now on we need to make sure we don't move the boxed data. +//! boxed.slice = NonNull::from(&boxed.data); +//! +//! // To do that, we pin the data in place by pointing to it with a Pin-wrapped pointer. +//! // Box::into_pin wraps the existing Box pointer to the data in-place without moving, +//! // so we can safely do this now after inserting the slice pointer above, but we have to +//! // take care that we haven't performed any other semantic moves of `res` in between. +//! let pinned = Box::into_pin(box); +//! +//! // Now we can return the pinned (through a Pin-wrapped box) data +//! pinned //! } //! } //! -//! let unmoved = Unmovable::new(); -//! // The pointer should point to the correct location, so long as the struct hasn't moved. +//! let unmovable: Pin> = Unmovable::new(); +//! +//! // The inner pointee `Unmovable` struct will now never be allowed to move. //! // Meanwhile, we are free to move the pointer around. //! # #[allow(unused_mut)] //! let mut still_unmoved = unmoved; //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data)); //! //! // Since our type doesn't implement `Unpin`, this will fail to compile. -//! // We cannot mutably dereference a `Pin` to `!Unpin` data. +//! // We cannot mutably dereference a `Pin` to `!Unpin` data (without using `unsafe`) //! // let mut new_unmoved = Unmovable::new(); //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved); //! ``` //! //! ## Intrusive, doubly-linked list //! [linked-list]: #an-intrusive-doubly-linked-list -//! [`Node`]: #an-intrusive-doubly-linked-list -//! [`List`]: #an-intrusive-doubly-linked-list -//! [`&mut Node`]: #an-intrusive-doubly-linked-list -//! [`List::append`]: #an-intrusive-doubly-linked-list -//! //! //! In an intrusive doubly-linked list, the collection does not actually allocate the memory for the //! nodes itself. Allocation is controlled by the clients, and nodes can live on a stack frame //! that lives shorter than the collection does provided the nodes are removed from the //! collection before returning. //! -//! Every node has pointers to its predecessor and successor in the list. Nodes can only be -//! added when they are pinned, because moving the nodes around would invalidate the pointers. -//! The [`Drop`] implementation of a linked list node will patch the pointers of its predecessor -//! and successor to remove itself from the list; not doing so would result in a use-after-free when -//! the list went to refer to the destroyed node. -//! -//! ```rust -//! use std::marker::PhantomPinned; -//! use std::pin::Pin; -//! use std::ptr; -//! -//! /// The list itself is Unpin, because it doesn't actually hold any data that cannot move -//! /// (although, if nodes held a reference back into it, it would need to be `!Unpin`). -//! /// -//! /// Holds the first and last nodes in the list; they are either both null or both non-null. -//! struct List { -//! start: *mut Node, -//! end: *mut Node, -//! } -//! -//! impl List { -//! fn new() -> Self { -//! Self { start: ptr::null_mut(), end: ptr::null_mut() } -//! } +//! To make this work, every element has pointers to its predecessor and successor in +//! the list. Elements can only be added when they are pinned, because moving the elements +//! around would invalidate the pointers. Moreover, the [`Drop`][Drop] implementation of a linked +//! list element will patch the pointers of its predecessor and successor to remove itself +//! from the list. //! -//! /// Appends the pinned `node` to the end of the list. -//! /// -//! /// For this function to be correct, we need two guarantees: -//! /// -//! /// 1. `node` never moves again so that our raw pointers to it are always valid -//! /// 2. If a `Node`'s memory would be re-used, its destructor gets run first, removing the -//! /// would-be-dangling references from the list. -//! fn append(&mut self, node: Pin<&mut Node>) { -//! // We could make `List: !Unpin` and track *which* list owns a node if we were fancier. -//! assert!( -//! node.pred.is_null() && node.succ.is_null(), -//! "Node must not already be in another list.", -//! ); +//! Crucially, we have to be able to rely on [`drop`] being called before an element is invalidated. +//! If an element could be deallocated or otherwise invalidated without calling [`drop`], the +//! pointers into it from its neighboring elements would become invalid, which would break the data +//! structure. //! -//! unsafe { -//! // Unpin the `&mut Node`. This is safe, because we're not actually -//! // moving the value, only modifying the pointers inside. This -//! // reference cannot escape this function. -//! let node = Pin::get_unchecked_mut(node); -//! -//! // Rearrange the pointers as appropriate for a doubly-linked list. -//! if self.start.is_null() { -//! self.start = node; -//! } else { -//! (*self.end).succ = node; -//! node.pred = self.end; -//! } -//! self.end = node; -//! } -//! } +//! Therefore, we rely on [the `Drop` guarantee][drop-guarantee] which comes with pinning data. //! -//! /// Allocates a node on the heap and appends it to the end of the list. -//! fn append_boxed(&mut self) -> Pin> { -//! let mut node = Box::pin(Node { -//! pred: ptr::null_mut(), -//! succ: ptr::null_mut(), -//! data: Data::new(), -//! _pin: PhantomPinned, -//! }); -//! self.append(node.as_mut()); -//! node -//! } -//! } +//! # Subtle details +//! [subtle-details]: #subtle-details //! -//! # struct Data; -//! # impl Data { fn new() -> Self { Data } } -//! struct Node { -//! pred: *mut Node, -//! succ: *mut Node, -//! data: Data, -//! /// `Node: Unpin` because `List` expects `Pin`ned pointers to them to remain in place. -//! _pin: PhantomPinned, -//! } +//! The purpose of pinning is not *just* to prevent a value from being *moved*, but rather more +//! generally to be able to rely on the pinned value *remaining valid **at a specific place*** in +//! memory. //! -<<<<<<< HEAD -//! This can never cause a problem in safe code because implementing a type that -//! relies on pinning requires unsafe code, but be aware that deciding to make -//! use of pinning in your type (for example by implementing some operation on -//! [Pin]<[&]Self> or [Pin]<[&mut] Self>) has consequences for your -//! [`Drop`][Drop] implementation as well: if an element of your type could have been pinned, -//! you must treat [`Drop`][Drop] as implicitly taking [Pin]<[&mut] Self>. -======= -//! impl Drop for Node { -//! /// Remove pointers to `self`, allowing reuse of this memory without clients seeing garbage. -//! fn drop(&mut self) { -//! if self.pred.is_null() || self.succ.is_null() { -//! // Not included: code to remove `self` if it is the head or tail of the list. -//! return; -//! } ->>>>>>> 389e1e2452d (Rewrite `Pin

` docs to clarify guarantees and uses) +//! To do so, pinning a value adds an *additional* invariant that must be upheld in order for use +//! of the pinned data to be valid, on top of the ones that must be upheld for a non-pinned value of +//! the same type: //! -//! unsafe { -//! (*self.pred).succ = self.succ; -//! (*self.succ).pred = self.pred; -//! } -//! } -//! } -//! ``` +//! From the moment a value is pinned by constructing a [`Pin`]-wrapped pointer to it, that value +//! must *remain, **valid***, at that same address in memory, *until its [`drop`] handler is +//! called.* //! -//! For this to work, a [`drop`-related guarantee][drop-guarantee] is required. If a node could -//! be deallocated or otherwise invalidated without calling [`drop`], the pointers into it from its -//! neighboring elements would become invalid, which would break the data structure. +//! There is some subtlety to this which we have not yet talked about in detail. The invariant above +//! means that, yes, //! -//! [`List`] itself is *not* address-sensitive. +//! 1. The value must not be moved out of its location in memory //! -//! # Subtle details -//! [subtle-details]: #subtle-details +//! but it also implies that, //! -//! [`List::append`] above relies on both of [`Pin

`]'s guarantees: +//! 2. The memory location that stores the value must not get invalidated or otherwise repurposed +//! during the lifetime of the pinned value until its [`drop`] returns or panics //! -//! 1. *Address Stability.* If [`unsafe`] code witnesses any [`p: Pin

`][Pin] at any time then -//! it may assume that `p.as_ref().get_ref() as *const _` will remain valid, pointing to the -//! same object until the end of that object's lifetime. -//! 2. *Notice of Destruction.* If `x: T` was ever reachable through any [`Pin

`] type, its -//! destructor must be run (until it either returns or panics) before `x`'s storage can be -//! overwritten. The "until further notice" in (1) includes this mandatory destruction. This is -//! often called the "[`Drop`] guarantee". +//! This point is subtle but required for intrusive data structures to be implemented soundly. //! -//! ## Address Stability -//! [address-stability]: #address-stability +//! ## `Drop` guarantee +//! [drop-guarantee]: #notice-of-destruction //! -//! The precise meaning of "address stability" is subtle, because "the same object" is not well-defined. -//! It is easiest to reason about it in terms of *visibility of mutations*. If [`unsafe`] code mutates -//! through a [`Pin

`], all code that stashed a raw pointer into it will see the mutation. In other -//! words, [`unsafe`] code can rely on the same value in memory being updated by all uses of a particular -//! [`Pin

`], not to mention that those stashed raw pointers remain valid in the first place. +//! There needs to be a way for a pinned value to notify any code that is relying on its pinned +//! status that it is about to be destroyed, so that such code can remove its address from their +//! data structures or otherwise change their behavior with the knowledge that they can no longer +//! rely on that value existing at the same location. //! -//! When a [`List`] stores a [`Node`], it needs to assume that appending a second node will mutate the -//! first node, so that later, when the first node is removed, it knows that its predecessor is the -//! second node. +//! Thus, in any situation where we may want to overwrite a pinned value, that value's [`drop`] must +//! be called beforehand (unless the pinned value implements [`Unpin`], in which case we can ignore +//! all of [`Pin`]'s guarantees, as usual). //! -//! When writing generic code, it's not possible to know what [`unsafe`] code has recorded about the -//! pointee's address, so it must be very careful to observe this invariant. Thankfully, most of this -//! is already enforced by the [`Pin

`] API, so only [`unsafe`] code needs to worry about this. +//! The most common storage-reuse situation is when a value on the stack is destroyed as part of a +//! function return, or when heap storage is freed. In both cases, [`drop`] gets run for us +//! by Rust when using standard safe code. However, for heap or otherwise custom-allocated storage, +//! [`unsafe`] code must make sure to call [`ptr::drop_in_place`] before deallocating and re-using +//! said storage. //! -//! ## Notice of Destruction -//! [drop-guarantee]: #notice-of-destruction +//! However, reuse can happen even if no storage is (de-)allocated. For example, if we had an +//! [`Option`] which contained a [`Some(v)`] where `v` is a pinned value, then `v` would be +//! invalidated by setting that option to [`None`]. //! -//! There needs to be a way for a pinned value to notify any [`unsafe`] code that recorded its address -//! that it is about to be destroyed, so that they can remove its address from their data structures. -//! Thus, in any situation where it would be safe to overwrite a pinned value, the destructor must -//! be called beforehand. +//! Similarly, if a [`Vec`] was used to store pinned values and [`Vec::set_len`] is used to manually +//! "kill" some elements of a vector, all value "killed" would become invalidated. //! -//! The most common storage-reuse situation is when a value on the stack is destroyed as part of a -//! function return, or when heap storage is freed. In both cases, the destructor gets run for us -//! by Rust. However, for heap storage, [`unsafe`] code must make sure to call [`ptr::drop_in_place`] -//! if it wishes to use the [`std::alloc`] APIs manually. -//! -//! However, reuse can happen even if not storage is de-allocated. For example, when a [`Some`] -//! is overwritten by [`None`] using [`ptr::write`], or when [`Vec::set_len`] is used to manually -//! "kill" some elements of a vector. Both of these cases are somewhat contrived, but it is crucial -//! to remember to run destructors of [`Pin`]ned data. As a corollary, the following code can *never* be +//! Both of these cases are somewhat contrived, but it is crucial +//! to remember that [`Pin`]ned data *must* be [`drop`]ped before it is invalidated, as a matter of +//! soundness, not just to prevent memory leaks. As a corollary, the following code can *never* be //! made safe: //! //! ```rust @@ -436,8 +460,9 @@ //! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run, even though //! normally [`Box`] drops the `T` before freeing the storage. //! -//! Of course, *leaking* memory is still fine: [`mem::forget`]ing a [`Box`] -//! prevents its storage from ever getting re-used, so destruction notice does not apply. +//! Of course, *leaking* memory in such a way that its underlying storage will never get invalidated +//! or re-used is still fine: [`mem::forget`]ing a [`Box`] prevents its storage from ever getting +//! re-used, so the [`drop`] guarantee does not apply. //! //! # Implementing an address-sensitive type. //! @@ -445,19 +470,20 @@ //! address-sensitive types, which are different from merely using [`Pin

`] in a generic //! way. //! -//! ## Implementing [`Drop`] for an `!Unpin` Type -//! [drop-impl]: #implementing-drop-for-an-unpin-type +//! ## Implementing [`Drop`] for types with address-sensitive state +//! [drop-impl]: self#implementing-drop-for-types-with-address-sensitive-states //! -//! The [`drop`] function takes [`&mut self`], but this is called *even if your -//! type was previously pinned*! Implementing [`Drop`] requires some care, since it is as if -//! the compiler automatically called [`Pin::get_unchecked_mut`]. -//! This can never cause a problem in safe code, because implementing an address-sensitive type -//! requires unsafe code (such as the [linked list above][linked-list]). +//! The [`drop`] function takes [`&mut self`], but this is called *even if that `self` has been +//! pinned*! Implementing [`Drop`] for a type with address-sensitive states, because if `self` was +//! indeed in an address-sensitive state before [`drop`] was called, it is as if the compiler +//! automatically called [`Pin::get_unchecked_mut`]. //! -//! Beware that deciding to make your type address-sensitive by implementing some operation on -//! [Pin]<[&Self][&]> or [Pin]<[`&mut Self`]> has consequences for your -//! [`Drop`] implementation as well: if an element of your type could have been pinned, -//! you must treat [`Drop`] as implicitly taking [Pin]<[`&mut Self`]>. +//! This can never cause a problem in safe code because implementing a type which has an +//! address-sensitive state which relies on pinning for validity (for example by +//! implementing some operation on [Pin]<[&]Self> or [Pin]<[&mut] Self> +//! which would be invalid if that `self` was not pinned) has consequences for your +//! [`Drop`][Drop] implementation as well: if an element of your type could have been pinned, +//! you must treat [`Drop`][Drop] as implicitly taking [Pin]<[&mut] Self>. //! //! You should implement [`Drop`] as follows: //! @@ -476,8 +502,9 @@ //! } //! ``` //! -//! The function `inner_drop` has the type that [`drop`] *should* have. This makes sure that -//! you do not accidentally use `self`/`this` in a way that is in conflict with pinning. +//! The function `inner_drop` has the signature that [`drop`] *should* have in this situation. +//! This makes sure that you do not accidentally use `self`/`this` in a way that is in conflict +//! with pinning's invariants. //! //! Moreover, if your type is [`#[repr(packed)]`][packed], the compiler will automatically //! move fields around to be able to drop them. It might even do @@ -486,11 +513,11 @@ //! //! ## "Assigning" pinned data //! -//! Although in general it is not valid to swap data through a [`Pin

`], or assign from -//! a [`Pin

`], for the same reason that a *move* is invalid, there is no particular reason +//! Although in general it is not valid to swap data through a [`Pin`], or assign from +//! a [`Pin`], for the same reason that a *move* is invalid, there is no particular reason //! to disallow doing it with specialized functions, as long as they know how to update all -//! uses of the pinned address (and any other `unsafe`-assumed invariants). For [`Unmovable`], -//! we could write +//! uses of the pinned address (and any other `unsafe`-assumed invariants). For [`Unmovable`] ( +//! from the example above) we could write //! //! ``` //! # use std::pin::Pin; @@ -527,11 +554,10 @@ //! ``` //! //! Even though we can't have the compiler do the assignment for us, it's possible to write -//! such specialized functions for types that might need it. It wouldn't be too difficult -//! implement such a function for the [`Node`], either. +//! such specialized functions for types that might need it. //! //! Note that it _is_ possible to assign through a [`Pin

`] by way of [`Pin::set()`]. This does -//! not violate any guarantees, since it will run the destructor of the pointee before assigning +//! not violate any guarantees, since it will run [`drop`] on the pointee value before assigning //! the new value. //! //! ## Projections and Structural Pinning @@ -571,11 +597,14 @@ //! the address of the field itself, it may be evidence that that field is structurally //! pinned. Unfortunately, there are no hard-and-fast rules. //! -//! ### When pinning *is not* structural for `field`... +//! ### Choosing pinning *not to be* structural for `field`... //! -//! While counter-intuitive, it's actually the easier choice: if a [Pin]<[`&mut Field`]> -//! is never created, nothing can go wrong! So, if you decide that some field does not have -//! structural pinning, all you have to ensure is that you never create a pinned reference to that field. +//! While counter-intuitive, it's actually the easier choice: if a +//! [Pin]<[`&mut Field`]> is never created, nothing can go wrong (well, so long as no +//! unsound `unsafe` code is written which expects the invariants of such a [`Pin`] to be upheld +//! without actually using pinning to guarantee them)! So, if you decide that some field does not +//! have structural pinning, all you have to ensure is that you never create [`Pin`]-wrapped +//! reference to that field. //! //! Fields without structural pinning may have a projection method that turns //! [Pin]<[&mut Struct][&mut]> into [`&mut Field`]: @@ -594,13 +623,9 @@ //! //! You may also impl [Unpin] for Struct {} *even if* the type of `field` //! is not [`Unpin`]. What that type thinks about pinning is not relevant -//! when no [Pin]<[`&mut Field`]> is ever created. +//! when no [Pin]<[&mut] Field> is ever created. //! -//! For example, the `data` field of [`Node`] does *not* need -//! to be structurally pinned, because neither [`List`] nor -//! [`Node`] assume anything about it. -//! -//! ### When pinning *is* structural for `field`... +//! ### Choosing pinning *to be* structural for `field`... //! //! The other option is to decide that pinning is "structural" for `field`, //! meaning that if the struct is pinned then so is the field. @@ -620,17 +645,11 @@ //! } //! ``` //! -//! For example, the `prev` and `succ` fields of a [`Node`] -//! are always either null or valid, so [`Node`] could provide a projection with -//! type fn([Pin]<[`&mut Node`]>) -> [Pin]<[`&mut Node`]> for each -//! of them. These fields need to be structurally-pinned, since the outer [`List`] -//! assumes every [`Node`] in it is pinned. -//! //! Structural pinning comes with a few extra requirements: //! //! 1. *Structural [`Unpin`].* A struct can be [`Unpin`] if, and only if, all of its //! structurally-pinned fields are, too. This is [`Unpin`]'s behavior by default. -//! However, as author, it is your responsibility to not write something like +//! However, as a libray author, it is your responsibility not to write something like //! unsafe impl\ [Unpin] for Struct\ {}. (Adding *any* projection //! operation requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break //! the principle that you only have to worry about any of this if you use [`unsafe`].) @@ -644,7 +663,7 @@ //! //! 3. *Structural Notice of Destruction.* You must uphold the the [`Drop` guarantee][drop-guarantee]: //! once your struct is pinned, the struct's storage cannot be re-used without calling the -//! structurally-pinned fields' destructors, too. +//! structurally-pinned fields' destructors, as well. //! //! This can be tricky, as witnessed by [`VecDeque`]: the destructor of [`VecDeque`] //! can fail to call [`drop`] on all elements if one of the destructors panics. This violates @@ -687,7 +706,7 @@ //! //! For a type like [`Vec`], both possibilities (structural pinning or not) make //! sense. A [`Vec`] with structural pinning could have `get_pin`/`get_pin_mut` -//! methods to get pinned references to elements. However, it could *not* allow calling +//! methods to get [`Pin`]-wrapped references to elements. However, it could *not* allow calling //! [`pop`][Vec::pop] on a pinned [`Vec`] because that would move the (structurally //! pinned) contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also //! move the contents. @@ -709,47 +728,24 @@ //! pointer is pinned, meaning pinning is *not* structural. //! //! When implementing a [`Future`] combinator, you will usually need structural pinning -//! for the nested futures, as you need to get pinned references to them to call [`poll`]. +//! for the nested futures, as you need to get [`Pin`]-wrapped references to them to call [`poll`]. //! But if your combinator contains any other data that does not need to be pinned, //! you can make those fields not structural and hence freely access them with a //! mutable reference even when you just have [Pin]<[`&mut Self`]> //! (such as in your own [`poll`] implementation). //! -//! [Target]: Deref::Target "ops::Deref::Target" -//! [`drop`]: Drop::drop "ops::Drop::drop" -//! [`poll`]: Future::poll "future::Future::poll" -//! -//! -//! [`std::alloc`]: ../../std/alloc/index.html -//! [`Box`]: ../../std/boxed/struct.Box.html -//! [`Rc`]: ../../std/rc/struct.Rc.html -//! [`Vec`]: ../../std/vec/struct.Vec.html -//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop -//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push -//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len -//! [`VecDeque`]: ../../std/collections/struct.VecDeque.html -//! //! [`&mut T`]: &mut //! [`&mut self`]: &mut //! [`&mut Self`]: &mut //! [`&mut Field`]: &mut -//! -<<<<<<< HEAD //! [Deref]: crate::ops::Deref "ops::Deref" //! [`Deref`]: crate::ops::Deref "ops::Deref" //! [Target]: crate::ops::Deref::Target "ops::Deref::Target" //! [`DerefMut`]: crate::ops::DerefMut "ops::DerefMut" //! [`mem::swap`]: crate::mem::swap "mem::swap" //! [`mem::forget`]: crate::mem::forget "mem::forget" -//! [Vec]: ../../std/vec/struct.Vec.html "Vec" -//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len "Vec::set_len" -//! [Box]: ../../std/boxed/struct.Box.html "Box" -//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop "Vec::pop" -//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push "Vec::push" -//! [Rc]: ../../std/rc/struct.Rc.html "rc::Rc" //! [RefCell]: crate::cell::RefCell "cell::RefCell" //! [`drop`]: Drop::drop -//! [VecDeque]: ../../std/collections/struct.VecDeque.html "collections::VecDeque" //! [`ptr::write`]: crate::ptr::write "ptr::write" //! [`Future`]: crate::future::Future "future::Future" //! [drop-impl]: #drop-implementation @@ -757,10 +753,22 @@ //! [`poll`]: crate::future::Future::poll "future::Future::poll" //! [&]: reference "shared reference" //! [&mut]: reference "mutable reference" -======= ->>>>>>> 389e1e2452d (Rewrite `Pin

` docs to clarify guarantees and uses) //! [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe" //! [packed]: https://doc.rust-lang.org/nomicon/other-reprs.html#reprpacked +//! [`std::alloc`]: ../../std/alloc/index.html +//! [`Box`]: ../../std/boxed/struct.Box.html +//! [Box]: ../../std/boxed/struct.Box.html "Box" +//! [`Rc`]: ../../std/rc/struct.Rc.html +//! [Rc]: ../../std/rc/struct.Rc.html "rc::Rc" +//! [`Vec`]: ../../std/vec/struct.Vec.html +//! [Vec]: ../../std/vec/struct.Vec.html "Vec" +//! [`Vec`]: ../../std/vec/struct/Vec.html "Vec" +//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len "Vec::set_len" +//! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop "Vec::pop" +//! [Vec::push]: ../../std/vec/struct.Vec.html#method.push "Vec::push" +//! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len +//! [`VecDeque`]: ../../std/collections/struct.VecDeque.html +//! [VecDeque]: ../../std/collections/struct.VecDeque.html "collections::VecDeque" #![stable(feature = "pin", since = "1.33.0")] @@ -777,15 +785,15 @@ use crate::{ mem, ptr, }; -/// A pinned pointer. +/// A pointer to a pinned value. /// -/// This is a wrapper around a kind of pointer which makes that pointer "pin" its -/// value in place, preventing the value referenced by that pointer from being moved -/// unless it implements [`Unpin`]. +/// This is a wrapper around any kind of pointer `P` which makes that pointer "pin" its pointee +/// value in place, thus preventing the value referenced by that pointer from being moved or +/// otherwise invalidated unless it implements [`Unpin`]. /// /// `Pin

` is guaranteed to have the same memory layout and ABI as `P`. /// -/// *See the [`pin` module] documentation for an explanation of pinning.* +/// *See the [`pin` module] documentation for an overview and explanation of pinning.* /// /// [`pin` module]: self // @@ -938,7 +946,7 @@ impl Pin

{ /// By using this method, you are making a promise about the `P::Deref` and /// `P::DerefMut` implementations, if they exist. Most importantly, they /// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref` - /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pinned pointer* + /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type P* /// and expect these methods to uphold the pinning invariants. /// Moreover, by calling this method you promise that the reference `P` /// dereferences to will not be moved out of again; in particular, it @@ -1048,7 +1056,7 @@ impl Pin

{ Pin { pointer } } - /// Gets a pinned shared reference from this pinned pointer. + /// Gets a shared reference to the pinned value this [`Pin`] points to. /// /// This is a generic method to go from `&Pin>` to `Pin<&T>`. /// It is safe because, as part of the contract of `Pin::new_unchecked`, @@ -1084,7 +1092,7 @@ impl Pin

{ } impl Pin

{ - /// Gets a pinned mutable reference from this pinned pointer. + /// Gets a mutable reference to the pinned value this `Pin

` points to. /// /// This is a generic method to go from `&mut Pin>` to `Pin<&mut T>`. /// It is safe because, as part of the contract of `Pin::new_unchecked`, @@ -1092,7 +1100,8 @@ impl Pin

{ /// "Malicious" implementations of `Pointer::DerefMut` are likewise /// ruled out by the contract of `Pin::new_unchecked`. /// - /// This method is useful when doing multiple calls to functions that consume the pinned type. + /// This method is useful when doing multiple calls to functions that consume the + /// [`Pin`]-wrapped pointer. /// /// # Example /// @@ -1119,10 +1128,12 @@ impl Pin

{ unsafe { Pin::new_unchecked(&mut *self.pointer) } } - /// Assigns a new value to the memory behind the pinned reference. + /// Assigns a new value to the memory location pointed to by the `Pin

`. /// - /// This overwrites pinned data, but that is okay: its destructor gets - /// run before being overwritten, so no pinning guarantee is violated. + /// This overwrites pinned data, but that is okay: the original pinned value's destructor gets + /// run before being overwritten and the new value is also a valid value of the same type, so + /// no pinning invariant is violated. See [the `pin` module documentation][self#subtle-details] + /// for more information on how this upholds the pinning invariants. /// /// # Example /// @@ -1132,7 +1143,7 @@ impl Pin

{ /// let mut val: u8 = 5; /// let mut pinned: Pin<&mut u8> = Pin::new(&mut val); /// println!("{}", pinned); // 5 - /// pinned.as_mut().set(10); + /// pinned.set(10); /// println!("{}", pinned); // 10 /// ``` #[stable(feature = "pin", since = "1.33.0")] @@ -1181,9 +1192,9 @@ impl<'a, T: ?Sized> Pin<&'a T> { /// It may seem like there is an issue here with interior mutability: in fact, /// it *is* possible to move a `T` out of a `&RefCell`. However, this is /// not a problem as long as there does not also exist a `Pin<&T>` pointing - /// to the same data, and `RefCell` does not let you create a pinned reference - /// to its contents. See the discussion on ["pinning projections"] for further - /// details. + /// to the inner `T` inside the `RefCell`, and `RefCell` does not let you get a + /// `Pin<&T>` pointer to its contents. See the discussion on ["pinning projections"] + /// for further details. /// /// Note: `Pin` also implements `Deref` to the target, which can be used /// to access the inner value. However, `Deref` only provides a reference @@ -1282,9 +1293,9 @@ impl<'a, T: ?Sized> Pin<&'a mut T> { } impl Pin<&'static T> { - /// Get a pinned reference from a static reference. + /// Get a `Pin`-wrapped reference from a `&'static` reference. /// - /// This is safe, because `T` is borrowed for the `'static` lifetime, which + /// This is safe because `T` is borrowed immutably for the `'static` lifetime, which /// never ends. #[stable(feature = "pin_static_ref", since = "1.61.0")] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] @@ -1296,7 +1307,7 @@ impl Pin<&'static T> { } impl<'a, P: DerefMut> Pin<&'a mut Pin

> { - /// Gets a pinned mutable reference from this nested pinned pointer. + /// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer. /// /// This is a generic method to go from `Pin<&mut Pin>>` to `Pin<&mut T>`. It is /// safe because the existence of a `Pin>` ensures that the pointee, `T`, cannot @@ -1335,9 +1346,9 @@ impl<'a, P: DerefMut> Pin<&'a mut Pin

> { } impl Pin<&'static mut T> { - /// Get a pinned mutable reference from a static mutable reference. + /// Get a `Pin`-wrapped mutable reference from a static mutable reference. /// - /// This is safe, because `T` is borrowed for the `'static` lifetime, which + /// This is safe because `T` is borrowed for the `'static` lifetime, which /// never ends. #[stable(feature = "pin_static_ref", since = "1.61.0")] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] From ba3b9342cc9f3d4421141e42d3c59200f712b3fc Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Mon, 25 Sep 2023 12:55:57 +0200 Subject: [PATCH 05/40] Fix examples, finish polishing --- library/core/src/pin.rs | 135 ++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 48 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index a75660522a10b..80e428033aab5 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -158,6 +158,10 @@ //! [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** //! will not be *moved* or [otherwise invalidated][self#subtle-details]. //! +//! We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning reference, or pinning +//! `Box`, etc.) because its existince is the thing that is pinning the underlying pointee in place: +//! it is the metaphorical "pin" securing the data in place on the pinboard (in memory). +//! //! It is important to stress that the thing in the [`Pin`] is not the value which we want to pin //! itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather //! the pointer's ***pointee** value*. @@ -190,7 +194,7 @@ //! //! [`Pin`] also uses the [`::Target`][Target] type information to modify the //! interface it is allowed to provide for interacting with that data (for example, when a -//! [`Pin`]-wrapped pointer points at pinned data which implements [`Unpin`], as +//! pinning pointer points at pinned data which implements [`Unpin`], as //! [discussed below][self#unpin]). //! //! [`Pin`] requires that implementations of [`Deref`] and [`DerefMut`] on `Ptr` return a @@ -247,7 +251,7 @@ //! // 1. Create the value, not yet in an address-sensitive state //! let tracker = AddrTracker::default(); //! -//! // 2. Pin the value by putting it behind a Pin-wrapped pointer, thus putting +//! // 2. Pin the value by putting it behind a pinning pointer, thus putting //! // it into an address-sensitive state //! let mut ptr_to_pinned_tracker: Pin<&mut AddrTracker> = pin!(tracker); //! ptr_to_pinned_tracker.as_mut().check_for_move(); @@ -263,7 +267,7 @@ //! //! Note that this invariant is enforced by simply making it impossible to call code that would //! perform a move on the pinned value. This is the case since the only way to access that pinned -//! value is through the [`Pin`]-wrapped [`&mut T`], which in turn restricts our access. +//! value is through the pinning [Pin]<[&mut] T>>, which in turn restricts our access. //! //! ## [`Unpin`] //! @@ -326,15 +330,16 @@ //! impl Unmovable { //! /// Create a new `Unmovable`. //! /// -//! /// To ensure the data doesn't move we place it on the heap behind a Pin-wrapped pointer. -//! /// Note that the data is pinned, but the pointer to that data can itself still be moved, -//! /// which is important because it means we can return the pointer from the function. +//! /// To ensure the data doesn't move we place it on the heap behind a pinning Box. +//! /// Note that the data is pinned, but the which is pinning it can itself still be moved. +//! /// This is important because it means we can return the pointer from the function, which +//! /// is itself a kind of move! //! fn new() -> Pin> { //! let res = Unmovable { //! data: [0; 64], //! // We only create the pointer once the data is in place //! // otherwise it will have already moved before we even started. -//! slice: NonNull::dangling(), +//! slice: NonNull::from(&[]), //! _pin: PhantomPinned, //! }; //! // First we put the data in a box, which will be its final resting place @@ -344,14 +349,16 @@ //! // From now on we need to make sure we don't move the boxed data. //! boxed.slice = NonNull::from(&boxed.data); //! -//! // To do that, we pin the data in place by pointing to it with a Pin-wrapped pointer. -//! // Box::into_pin wraps the existing Box pointer to the data in-place without moving, -//! // so we can safely do this now after inserting the slice pointer above, but we have to -//! // take care that we haven't performed any other semantic moves of `res` in between. -//! let pinned = Box::into_pin(box); +//! // To do that, we pin the data in place by pointing to it with a pinning +//! // (`Pin`-wrapped) pointer. +//! // +//! // `Box::into_pin` makes existing `Box` pin the data in-place without moving it, +//! // so we can safely do this now *after* inserting the slice pointer above, but we have +//! // to take care that we haven't performed any other semantic moves of `res` in between. +//! let pin = Box::into_pin(boxed); //! -//! // Now we can return the pinned (through a Pin-wrapped box) data -//! pinned +//! // Now we can return the pinned (through a pinning Box) data +//! pin //! } //! } //! @@ -360,11 +367,11 @@ //! // The inner pointee `Unmovable` struct will now never be allowed to move. //! // Meanwhile, we are free to move the pointer around. //! # #[allow(unused_mut)] -//! let mut still_unmoved = unmoved; +//! let mut still_unmoved = unmovable; //! assert_eq!(still_unmoved.slice, NonNull::from(&still_unmoved.data)); //! +//! // We cannot mutably dereference a `Pin` unless the pointee is `Unpin` or we use unsafe. //! // Since our type doesn't implement `Unpin`, this will fail to compile. -//! // We cannot mutably dereference a `Pin` to `!Unpin` data (without using `unsafe`) //! // let mut new_unmoved = Unmovable::new(); //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved); //! ``` @@ -401,7 +408,7 @@ //! of the pinned data to be valid, on top of the ones that must be upheld for a non-pinned value of //! the same type: //! -//! From the moment a value is pinned by constructing a [`Pin`]-wrapped pointer to it, that value +//! From the moment a value is pinned by constructing a [`Pin`]ning pointer to it, that value //! must *remain, **valid***, at that same address in memory, *until its [`drop`] handler is //! called.* //! @@ -436,8 +443,8 @@ //! said storage. //! //! However, reuse can happen even if no storage is (de-)allocated. For example, if we had an -//! [`Option`] which contained a [`Some(v)`] where `v` is a pinned value, then `v` would be -//! invalidated by setting that option to [`None`]. +//! [`Option`] which contained a `Some(v)` where `v` is a pinned value, then `v` would be +//! invalidated by setting that option to `None`. //! //! Similarly, if a [`Vec`] was used to store pinned values and [`Vec::set_len`] is used to manually //! "kill" some elements of a vector, all value "killed" would become invalidated. @@ -451,18 +458,23 @@ //! # use std::mem::ManuallyDrop; //! # use std::pin::Pin; //! # struct Type; -//! let mut pinned = Box::pin(ManuallyDrop::new(Type)); -//! let inner = unsafe { -//! Pin::map_unchecked_mut(Pin::as_mut(&mut pinned), |x| &mut *x) +//! // Pin something inside a `ManuallyDrop`. This is fine on its own. +//! let mut pin: Pin>> = Box::pin(ManuallyDrop::new(Type)); +//! +//! // However, creating a pinning mutable reference to the type *inside* +//! // the `ManuallyDrop` is not! +//! let inner: Pin<&mut Type> = unsafe { +//! Pin::map_unchecked_mut(pin.as_mut(), |x| &mut **x) //! }; //! ``` //! -//! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run, even though -//! normally [`Box`] drops the `T` before freeing the storage. +//! Because [`mem::ManuallyDrop`] inhibits the destructor of `Type`, it won't get run when the +//! [Box]<[ManuallyDrop]\> is dropped, thus violating the drop guarantee of the +//! [Pin]<[&mut] Type>>. //! //! Of course, *leaking* memory in such a way that its underlying storage will never get invalidated //! or re-used is still fine: [`mem::forget`]ing a [`Box`] prevents its storage from ever getting -//! re-used, so the [`drop`] guarantee does not apply. +//! re-used, so the [`drop`] guarantee is still satisfied. //! //! # Implementing an address-sensitive type. //! @@ -478,12 +490,12 @@ //! indeed in an address-sensitive state before [`drop`] was called, it is as if the compiler //! automatically called [`Pin::get_unchecked_mut`]. //! -//! This can never cause a problem in safe code because implementing a type which has an -//! address-sensitive state which relies on pinning for validity (for example by -//! implementing some operation on [Pin]<[&]Self> or [Pin]<[&mut] Self> -//! which would be invalid if that `self` was not pinned) has consequences for your +//! This can never cause a problem in purely safe code because creating a pinning pointer to +//! a type which has an address-sensitive (thus does not implement `Unpin`) requires `unsafe`, +//! but it is important to note that choosing to take advantage of pinning-related guarantees +//! to justify validity in the implementation of your type has consequences for that type's //! [`Drop`][Drop] implementation as well: if an element of your type could have been pinned, -//! you must treat [`Drop`][Drop] as implicitly taking [Pin]<[&mut] Self>. +//! you must treat [`Drop`][Drop] as implicitly taking self: [Pin]<[&mut] Self>. //! //! You should implement [`Drop`] as follows: //! @@ -511,6 +523,15 @@ //! that for fields that happen to be sufficiently aligned. As a consequence, you cannot use //! pinning with a [`#[repr(packed)]`][packed] type. //! +//! ### Implementing [`Drop`] for pointer types which will be used as [`Pin`]ning pointers +//! +//! It should further be noted that creating a pinning pointer of some type `Ptr` to some underlying +//! `T` which is not `Unpin` *also* carries with it implications on the way that `Ptr` type must +//! implement [`Drop`] (as well as [`Deref`] and [`DerefMut`])! When implementing a pointer type +//! that may be used as a pinning pointer, you must also take the same care described above not to +//! *move* out of or otherwise invalidate the pointee during [`Drop`], [`Deref`], or [`DerefMut`] +//! implementations. +//! //! ## "Assigning" pinned data //! //! Although in general it is not valid to swap data through a [`Pin`], or assign from @@ -600,14 +621,14 @@ //! ### Choosing pinning *not to be* structural for `field`... //! //! While counter-intuitive, it's actually the easier choice: if a -//! [Pin]<[`&mut Field`]> is never created, nothing can go wrong (well, so long as no +//! [Pin]<[&mut] Field> is never created, nothing can go wrong (well, so long as no //! unsound `unsafe` code is written which expects the invariants of such a [`Pin`] to be upheld //! without actually using pinning to guarantee them)! So, if you decide that some field does not -//! have structural pinning, all you have to ensure is that you never create [`Pin`]-wrapped +//! have structural pinning, all you have to ensure is that you never create pinning //! reference to that field. //! //! Fields without structural pinning may have a projection method that turns -//! [Pin]<[&mut Struct][&mut]> into [`&mut Field`]: +//! [Pin]<[&mut] Struct> into [`&mut Field`]: //! //! ```rust,no_run //! # use std::pin::Pin; @@ -706,7 +727,7 @@ //! //! For a type like [`Vec`], both possibilities (structural pinning or not) make //! sense. A [`Vec`] with structural pinning could have `get_pin`/`get_pin_mut` -//! methods to get [`Pin`]-wrapped references to elements. However, it could *not* allow calling +//! methods to get pinning references to elements. However, it could *not* allow calling //! [`pop`][Vec::pop] on a pinned [`Vec`] because that would move the (structurally //! pinned) contents! Nor could it allow [`push`][Vec::push], which might reallocate and thus also //! move the contents. @@ -728,8 +749,8 @@ //! pointer is pinned, meaning pinning is *not* structural. //! //! When implementing a [`Future`] combinator, you will usually need structural pinning -//! for the nested futures, as you need to get [`Pin`]-wrapped references to them to call [`poll`]. -//! But if your combinator contains any other data that does not need to be pinned, +//! for the nested futures, as you need to get pinning ([`Pin`]-wrapped) references to them to +//! call [`poll`]. But if your combinator contains any other data that does not need to be pinned, //! you can make those fields not structural and hence freely access them with a //! mutable reference even when you just have [Pin]<[`&mut Self`]> //! (such as in your own [`poll`] implementation). @@ -744,6 +765,7 @@ //! [`DerefMut`]: crate::ops::DerefMut "ops::DerefMut" //! [`mem::swap`]: crate::mem::swap "mem::swap" //! [`mem::forget`]: crate::mem::forget "mem::forget" +//! [ManuallyDrop]: crate::mem::ManuallyDrop "ManuallyDrop" //! [RefCell]: crate::cell::RefCell "cell::RefCell" //! [`drop`]: Drop::drop //! [`ptr::write`]: crate::ptr::write "ptr::write" @@ -785,17 +807,32 @@ use crate::{ mem, ptr, }; -/// A pointer to a pinned value. +/// A pointer which pins its pointee in place. +/// +/// [`Pin`] is a wrapper around some kind of pointer `Ptr` which makes that pointer "pin" its +/// pointee value in place, thus preventing the value referenced by that pointer from being moved or +/// otherwise invalidated at that place in memory unless it implements [`Unpin`]. /// -/// This is a wrapper around any kind of pointer `P` which makes that pointer "pin" its pointee -/// value in place, thus preventing the value referenced by that pointer from being moved or -/// otherwise invalidated unless it implements [`Unpin`]. +/// ## Pinning values with [`Pin`] +/// +/// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a +/// [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** +/// will not be *moved* or [otherwise invalidated][self#subtle-details]. +/// +/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning +/// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in +/// place: it is the metaphorical "pin" securing the data in place on the pinboard (in memory). +/// +/// It is important to stress that the thing in the [`Pin`] is not the value which we want to pin +/// itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather +/// the pointer's ***pointee** value*. /// /// `Pin

` is guaranteed to have the same memory layout and ABI as `P`. /// -/// *See the [`pin` module] documentation for an overview and explanation of pinning.* +/// *See the [`pin` module] documentation for a more thorough exploration of pinning.* /// /// [`pin` module]: self +/// [`Box`]: ../../std/boxed/struct.Box.html // // Note: the `Clone` derive below causes unsoundness as it's possible to implement // `Clone` for mutable references. @@ -982,12 +1019,14 @@ impl Pin

{ /// use std::pin::Pin; /// /// fn move_pinned_rc(mut x: Rc) { - /// let pinned = unsafe { Pin::new_unchecked(Rc::clone(&x)) }; + /// // This should mean the pointee can never move again. + /// let pin = unsafe { Pin::new_unchecked(Rc::clone(&x)) }; /// { - /// let p: Pin<&T> = pinned.as_ref(); - /// // This should mean the pointee can never move again. + /// let p: Pin<&T> = pin.as_ref(); + /// // ... /// } - /// drop(pinned); + /// drop(pin); + /// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️ /// // Now, if `x` was the only reference, we have a mutable reference to /// // data that we pinned above, which we could use to move it as we have @@ -1101,7 +1140,7 @@ impl Pin

{ /// ruled out by the contract of `Pin::new_unchecked`. /// /// This method is useful when doing multiple calls to functions that consume the - /// [`Pin`]-wrapped pointer. + /// pinning pointer. /// /// # Example /// @@ -1293,7 +1332,7 @@ impl<'a, T: ?Sized> Pin<&'a mut T> { } impl Pin<&'static T> { - /// Get a `Pin`-wrapped reference from a `&'static` reference. + /// Get a pinning reference from a `&'static` reference. /// /// This is safe because `T` is borrowed immutably for the `'static` lifetime, which /// never ends. @@ -1346,7 +1385,7 @@ impl<'a, P: DerefMut> Pin<&'a mut Pin

> { } impl Pin<&'static mut T> { - /// Get a `Pin`-wrapped mutable reference from a static mutable reference. + /// Get a pinning mutable reference from a static mutable reference. /// /// This is safe because `T` is borrowed for the `'static` lifetime, which /// never ends. From 584f0986fcec6e2cb61d9753a9ccf3ff951d0b3a Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Mon, 25 Sep 2023 13:28:35 +0200 Subject: [PATCH 06/40] fix broken links --- library/core/src/pin.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 80e428033aab5..146708f45cb6a 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -296,7 +296,7 @@ //! # Examples of address-sensitive types //! [address-sensitive-examples]: #examples-of-address-sensitive-types //! -//! ## Self-referential struct +//! ## A self-referential struct //! [self-ref]: #a-self-referential-struct //! [`Unmovable`]: #a-self-referential-struct //! @@ -425,7 +425,7 @@ //! This point is subtle but required for intrusive data structures to be implemented soundly. //! //! ## `Drop` guarantee -//! [drop-guarantee]: #notice-of-destruction +//! [drop-guarantee]: self#drop-guarantee //! //! There needs to be a way for a pinned value to notify any code that is relying on its pinned //! status that it is about to be destroyed, so that such code can remove its address from their From ec8a01a4798ca76b09e4c626fc47eb8cab960e24 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Mon, 25 Sep 2023 13:35:39 +0200 Subject: [PATCH 07/40] fix one more broken link --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 146708f45cb6a..dcabf359f52f4 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -784,7 +784,7 @@ //! [Rc]: ../../std/rc/struct.Rc.html "rc::Rc" //! [`Vec`]: ../../std/vec/struct.Vec.html //! [Vec]: ../../std/vec/struct.Vec.html "Vec" -//! [`Vec`]: ../../std/vec/struct/Vec.html "Vec" +//! [`Vec`]: ../../std/vec/struct.Vec.html "Vec" //! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len "Vec::set_len" //! [Vec::pop]: ../../std/vec/struct.Vec.html#method.pop "Vec::pop" //! [Vec::push]: ../../std/vec/struct.Vec.html#method.push "Vec::push" From 46f9d77bd1eadd637a78df9e413cb689d39d6246 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Mon, 25 Sep 2023 16:00:35 +0200 Subject: [PATCH 08/40] update doubly linked list commentary and fix links --- library/core/src/pin.rs | 45 ++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index dcabf359f52f4..dc4730f4f5d22 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -26,7 +26,7 @@ //! * [Examples of types with address-sensitive states][address-sensitive-examples] //! * [Self-referential struct][self-ref] //! * [Intrusive, doubly-linked list][linked-list] -//! * [Subtle Details][subtle-details] +//! * [Subtle details and the `Drop` guarantee][subtle-details] //! //! # What is "*moving*"? //! [what-is-moving]: self#what-is-moving @@ -156,7 +156,7 @@ //! //! In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a //! [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** -//! will not be *moved* or [otherwise invalidated][self#subtle-details]. +//! will not be *moved* or [otherwise invalidated][subtle-details]. //! //! We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning reference, or pinning //! `Box`, etc.) because its existince is the thing that is pinning the underlying pointee in place: @@ -376,7 +376,7 @@ //! // std::mem::swap(&mut *still_unmoved, &mut *new_unmoved); //! ``` //! -//! ## Intrusive, doubly-linked list +//! ## An intrusive, doubly-linked list //! [linked-list]: #an-intrusive-doubly-linked-list //! //! In an intrusive doubly-linked list, the collection does not actually allocate the memory for the @@ -384,21 +384,26 @@ //! that lives shorter than the collection does provided the nodes are removed from the //! collection before returning. //! -//! To make this work, every element has pointers to its predecessor and successor in -//! the list. Elements can only be added when they are pinned, because moving the elements -//! around would invalidate the pointers. Moreover, the [`Drop`][Drop] implementation of a linked -//! list element will patch the pointers of its predecessor and successor to remove itself -//! from the list. +//! The full implementation details of such a data structure are outside the scope of this +//! documentation, but we will discuss how [`Pin`] can help to do so. //! -//! Crucially, we have to be able to rely on [`drop`] being called before an element is invalidated. -//! If an element could be deallocated or otherwise invalidated without calling [`drop`], the -//! pointers into it from its neighboring elements would become invalid, which would break the data -//! structure. +//! To make such an intrusive data structure work, every element stores pointers to its predecessor +//! and successor within its own data, rather than having the list structure itself manage those +//! pointers. Elements can only be added when they are pinned, because moving the elements +//! around would invalidate the pointers to it which are contained in the element ahead and behind +//! it. Moreover, the [`Drop`][Drop] implementation of the element types themselves will in some +//! way patch the pointers of its predecessor and successor elements to remove itself from the list. //! -//! Therefore, we rely on [the `Drop` guarantee][drop-guarantee] which comes with pinning data. +//! Crucially, this means we have to be able to rely on [`drop`] always being called before that +//! element is invalidated. If an element could be deallocated or otherwise invalidated without +//! calling [`drop`], the pointers into it which are stored in its neighboring elements would +//! become invalid, which would break the data structure. //! -//! # Subtle details -//! [subtle-details]: #subtle-details +//! Therefore, we rely on [the `Drop` guarantee][drop-guarantee] which comes with pinning data, +//! +//! # Subtle details and the `Drop` guarantee +//! [subtle-details]: self#subtle-details-and-the-drop-guarantee +//! [drop-guarantee]: self#subtle-details-and-the-drop-guarantee //! //! The purpose of pinning is not *just* to prevent a value from being *moved*, but rather more //! generally to be able to rely on the pinned value *remaining valid **at a specific place*** in @@ -425,7 +430,6 @@ //! This point is subtle but required for intrusive data structures to be implemented soundly. //! //! ## `Drop` guarantee -//! [drop-guarantee]: self#drop-guarantee //! //! There needs to be a way for a pinned value to notify any code that is relying on its pinned //! status that it is about to be destroyed, so that such code can remove its address from their @@ -482,7 +486,7 @@ //! address-sensitive types, which are different from merely using [`Pin

`] in a generic //! way. //! -//! ## Implementing [`Drop`] for types with address-sensitive state +//! ## Implementing [`Drop`] for types with address-sensitive states //! [drop-impl]: self#implementing-drop-for-types-with-address-sensitive-states //! //! The [`drop`] function takes [`&mut self`], but this is called *even if that `self` has been @@ -817,7 +821,7 @@ use crate::{ /// /// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a /// [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** -/// will not be *moved* or [otherwise invalidated][self#subtle-details]. +/// will not be *moved* or [otherwise invalidated][subtle-details]. /// /// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning /// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in @@ -833,6 +837,7 @@ use crate::{ /// /// [`pin` module]: self /// [`Box`]: ../../std/boxed/struct.Box.html +/// [subtle-details]: self#subtle-details-and-the-drop-guarantee // // Note: the `Clone` derive below causes unsoundness as it's possible to implement // `Clone` for mutable references. @@ -1171,7 +1176,7 @@ impl Pin

{ /// /// This overwrites pinned data, but that is okay: the original pinned value's destructor gets /// run before being overwritten and the new value is also a valid value of the same type, so - /// no pinning invariant is violated. See [the `pin` module documentation][self#subtle-details] + /// no pinning invariant is violated. See [the `pin` module documentation][subtle-details] /// for more information on how this upholds the pinning invariants. /// /// # Example @@ -1185,6 +1190,8 @@ impl Pin

{ /// pinned.set(10); /// println!("{}", pinned); // 10 /// ``` + /// + /// [subtle-details]: self#subtle-details-and-the-drop-guarantee #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] pub fn set(&mut self, value: P::Target) From db5b19e4726279d03aa91469a0e7df9de09b47a8 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 26 Sep 2023 18:14:18 +0200 Subject: [PATCH 09/40] improve intro and `Unpin`-related discussion --- library/core/src/marker.rs | 52 +++++++----- library/core/src/pin.rs | 160 ++++++++++++++++++++++++++----------- 2 files changed, 145 insertions(+), 67 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 69d54f0640780..bd19d3cc0e290 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -899,25 +899,28 @@ marker_impls! { {T: ?Sized} &mut T, } -/// Types that can be safely moved after being pinned. +/// Types that do not need to follow the rules of pinning. /// -/// Rust itself has no notion of immovable types, and considers moves (e.g., -/// through assignment or [`mem::replace`]) to always be safe. +/// For information on what "pinning" is, see the [`pin` module] documentation. /// -/// The [`Pin`][Pin] type is used instead to prevent moves through the type -/// system. Pointers `P` wrapped in the [`Pin>`][Pin] wrapper can't be -/// moved out of. See the [`pin` module] documentation for more information on -/// pinning. +/// Implementing the `Unpin` trait for `T` lifts the restrictions of pinning off that type. +/// This means that, if `T: Unpin`, it cannot be assumed that a value of type `T` will be bound +/// by the invariants that pinning infers, *even* when "pinned" by a [`Pin`] pointing at it. +/// When a value of type `T` is pointed at by a [`Pin`], [`Pin`] will not restrict access +/// to the pointee value like it normally would, thus allowing the user to do anything that they +/// normally could with a non-[`Pin`]-wrapped `Ptr` to that value. /// -/// Implementing the `Unpin` trait for `T` lifts the restrictions of pinning off -/// the type, which then allows moving `T` out of [`Pin>`][Pin] with -/// functions such as [`mem::replace`]. +/// For more discussion on the consequences of [`Unpin`] within the wider scope of the pinning +/// system, see [the section about `Unpin`] in the [`pin` module]. /// -/// `Unpin` has no consequence at all for non-pinned data. In particular, -/// [`mem::replace`] happily moves `!Unpin` data (it works for any `&mut T`, not -/// just when `T: Unpin`). However, you cannot use [`mem::replace`] on data -/// wrapped inside a [`Pin>`][Pin] because you cannot get the `&mut T` you -/// need for that, and *that* is what makes this system work. +/// `Unpin` has no consequence at all for non-pinned data. In particular, [`mem::replace`] happily +/// moves `!Unpin` data, which would be immovable when pinned ([`mem::replace`] works for any +/// `&mut T`, not just when `T: Unpin`). +/// +/// *However*, you cannot use [`mem::replace`] on `!Unpin` data which is *pinned* by being wrapped +/// inside a [`Pin`] pointing at it. This is because you cannot (safely) use a +/// [`Pin`] to get an `&mut T` to its pointee value, which you would need to call +/// [`mem::replace`], and *that* is what makes this system work. /// /// So this, for example, can only be done on types implementing `Unpin`: /// @@ -935,11 +938,20 @@ marker_impls! { /// mem::replace(&mut *pinned_string, "other".to_string()); /// ``` /// -/// This trait is automatically implemented for almost every type. -/// -/// [`mem::replace`]: crate::mem::replace -/// [Pin]: crate::pin::Pin -/// [`pin` module]: crate::pin +/// This trait is automatically implemented for almost every type. The compiler (and you!) is free +/// to take the conservative stance of marking types as [`Unpin`] by default. This is because if a +/// type implements [`Unpin`], then it is unsound for [`unsafe`] code to assume that type is truly +/// pinned, *even* when viewed through a "pinning" pointer! It is the responsibility of the +/// implementor of [`unsafe`] code that relies upon pinning for soundness to ensure that all the +/// types it expects to be truly pinned do not implement [`Unpin`]. For more details, see the +/// [`pin` module] docs! +/// +/// [`mem::replace`]: crate::mem::replace "mem replace" +/// [`Pin`]: crate::pin::Pin "Pin" +/// [`Pin`]: crate::pin::Pin "Pin" +/// [`pin` module]: crate::pin "pin module" +/// [section about `Unpin`]: crate::pin#unpin "pin module docs about unpin" +/// [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe" #[stable(feature = "pin", since = "1.33.0")] #[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/library/core/src/pin.rs b/library/core/src/pin.rs index dc4730f4f5d22..485ec689e3431 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1,28 +1,53 @@ //! Types that pin data to a location in memory. //! //! It is sometimes useful to be able to rely upon a certain value not being able to *move*, -//! in the sense that its address in memory cannot change. This is useful specifically when there -//! are one or more *pointers* pointing at that value. The ability to rely on this guarantee that -//! the value a pointer is pointing at (its **pointee**) will +//! in the sense that its address in memory cannot change. This is useful especially when there +//! are one or more [*pointers*][pointer] pointing at that value. The ability to rely on this +//! guarantee that the value a [pointer] is pointing at (its **pointee**) will //! //! 1. Not be *moved* out of its memory location //! 2. More generally, remain *valid* at that same memory location //! -//! is necessary to implement things like self-referential structs and intrusive data structures. -//! -//! "Pinning" allows us to put a value *which is being pointed at* by some pointer `Ptr` into a -//! state that prevents safe code from *moving* or otherwise invalidating the *pointee* value at -//! its location in memory (unless the pointee type implements [`Unpin`], which we'll -//! [discuss more below][self#unpin]). In this way, we can allow [`unsafe`] code to rely on the -//! pointer to be valid to dereference. +//! is called "pinning." We would say that a value which satisfies these guarantees has been +//! "pinned," in that it has been permanently (until the end of its lifetime) attached to its +//! location in memory. Pinning a value is incredibly useful in that it provides the necessary +//! guarantees[^guarantees] for [`unsafe`] code to be able to dereference raw pointers to the pinned +//! value for the duration it is pinned (which, [as we'll see later][drop-guarantee], is necessarily +//! from the time the value is first pinned until the end of its lifetime). This concept of +//! "pinning" is necessary to implement safe interfaces on top of things like self-referential types +//! and intrusive data structures which cannot currently be modeled in fully safe Rust using only +//! borrow-checked [references][reference]. +//! +//! "Pinning" allows us to put a *value* which exists at some location in memory into a state where +//! safe code cannot *move* that value to a different location in memory or otherwise invalidate it +//! at its current location (unless it implements [`Unpin`], which we will +//! [talk about below][unpin]). Anything that wants to interact with the pinned value in a way that +//! has the potential to violate these guarantees must promise that it will not actually violate +//! them, using the [`unsafe`] keyword to mark that such a promise is upheld by the user and not +//! the compiler. In this way, we can allow other [`unsafe`] code to rely on any pointers that +//! point to the pinned value to be valid to dereference while it is pinned. +//! +//! [^guarantees]: Pinning on its own does not provide *all* the invariants necessary here. However, +//! in order to validly pin a value in the first place, it must already satisfy the other invariants +//! for it to be valid to dereference a pointer to that value while it is pinned, and using the +//! [`Drop` guarantee][drop-guarantee], we can ensure that any interested parties are notified +//! before the value becomes no longer pinned, i.e. when the value goes out of scope and is +//! invalidated. +//! +//! Note that as long as you don't use [`unsafe`], it's impossible to create or misuse a pinned +//! value in a way that will produce unsoundness. See the documentation of [`Pin`] for more +//! information on the practicalities of how to pin a value and how to use that pinned value from a +//! user's perspective without using [`unsafe`]. //! //! The rest of this documentation is intended to be the source of truth for users of [`Pin`] -//! in unsafe code; users of [`Pin`] in safe code do not need to read it in detail. +//! that are implementing the [`unsafe`] pieces of an interface that relies on pinning for validity; +//! users of [`Pin`] in safe code do not need to read it in detail. //! //! There are several sections to this documentation: //! //! * [What is "*moving*"?][what-is-moving] //! * [What is "pinning"?][what-is-pinning] +//! * [Address sensitivity, AKA "when do we need pinning?"][address-sensitive-values] //! * [Examples of types with address-sensitive states][address-sensitive-examples] //! * [Self-referential struct][self-ref] //! * [Intrusive, doubly-linked list][linked-list] @@ -90,8 +115,8 @@ //! to remain *valid* and *located at the same place in memory* from the time it is pinned until its //! [`drop`] is called. //! -//! ## Address-sensitive values, AKA "why we need pinning" -//! [address-sensitive-values]: self#address-sensitive-values +//! ## Address-sensitive values, AKA "when we need pinning" +//! [address-sensitive-values]: self#address-sensitive-values-aka-when-we-need-pinning //! //! Most values in Rust are entirely okay with being *moved* around at-will. //! Types for which it is *always* the case that *any* value of that type can be @@ -105,9 +130,9 @@ //! //! As a motivating example of a type which may become address-sensitive, consider a type which //! contains a pointer to another piece of its own data, *i.e.* a "self-referential" type. In order -//! such a type to be implemented soundly, the pointer which points into `self`'s data must be +//! for such a type to be implemented soundly, the pointer which points into `self`'s data must be //! proven valid whenever it is accessed. But if that value is *moved*, the pointer will still -//! point to the old location that the value was located and not into the new location of `self`, +//! point to the old address where the value was located and not into the new location of `self`, //! thus becoming invalid. A key example of such self-referrential types are the state machines //! generated by the compiler to implement [`Future`] for `async fn`s. //! @@ -122,7 +147,7 @@ //! assume that the address of the value is stable //! * e.g. subsequent calls to [`poll`] //! 4. Before the value is invalidated (e.g. deallocated), it is *dropped*, giving it a chance to -//! "unregister"/clear outstanding pointers to itself +//! notify anything with pointers to itself that those pointers will be invalidated //! * e.g. [`drop`]ping the [`Future`] //! //! There are two possible ways to ensure the invariants required for 2. and 3. above (which @@ -229,8 +254,9 @@ //! #[derive(Default)] //! struct AddrTracker { //! prev_addr: Option, -//! // remove auto-implemented Unpin bound to mark this type as having some -//! // address-sensitive state. This is discussed more below. +//! // remove auto-implemented `Unpin` bound to mark this type as having some +//! // address-sensitive state. This is essential for our expected pinning +//! // guarantees to work, and is discussed more below. //! _pin: PhantomPinned, //! } //! @@ -273,25 +299,52 @@ //! //! The vast majority of Rust types have no address-sensitive states; these types //! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of -//! [`Pin

`]. When [`T: Unpin`][Unpin], [Pin]<[Box]\> and -//! [`Box`] function identically, as do [Pin]<[&mut] T> and -//! [`&mut T`]. +//! [`Pin`] when the *pointee* type `T` is [`Unpin`]. When [`T: Unpin`][Unpin], +//! [Pin]<[Box]\> functions identically to a non-pinning [`Box`]; similarly, +//! [Pin]<[&mut] T> would impose no additional restrictions above a regular [`&mut T`]. +//! +//! Note that the interaction between a [`Pin`] and [`Unpin`] is through the type of the +//! **pointee** value, [`::Target`][Target]. Whether the `Ptr` type itself +//! implements [`Unpin`] does not affect the behavior of a [`Pin`]. For example, whether or not +//! [`Box`] is [`Unpin`] has no effect on the behavior of [Pin]<[Box]\>, because +//! `T` is the type of the pointee value, not [`Box`]. So, whether `T` implements [`Unpin`] is +//! the thing that will affect the behavior of the [Pin]<[Box]\>. //! -//! This includes all of the primitive types, like [`bool`], [`i32`], and [&]T, -//! as well as any other type consisting only of those types. +//! Builtin types that are [`Unpin`] include all of the primitive types, like [`bool`], [`i32`], +//! and [`f32`], references ([&]T and [&mut] T), etc., as well as many +//! core and standard library types like [`Box`], [`String`], and more. +//! These types are marked [`Unpin`] because they do not have an ddress-sensitive state like the +//! ones we discussed above. If they did have such a state, those parts of their interface would be +//! unsound without being expressed through pinning, and they would then need to not +//! implement [`Unpin`]. +//! +//! The compiler (and users!) is free to take the conservative stance of marking types as [`Unpin`] +//! by default. This is because if a type implements [`Unpin`], then it is unsound for [`unsafe`] +//! code to assume that type is truly pinned, *even* when viewed through a "pinning" pointer! It is +//! the responsibility of *the implementor of [`unsafe`] code that relies upon pinning for +//! soundness* (you, in this case!) to ensure that all the types which that code expects to be truly +//! pinned do not implement [`Unpin`]. //! //! Like other auto-traits, the compiler will automatically determine that a type implements -//! [`Unpin`] if all its fields also implement [`Unpin`]. If you are building a type which is built +//! [`Unpin`] if all its fields also implement [`Unpin`]. If you are building a type which consists //! of only [`Unpin`] types but has an address-sensistive state and thus should not itself -//! implement [`Unpin`], you can opt out of [`Unpin`] via adding a field with the -//! [`PhantomPinned`] marker type, as we did with our latest `AddrTracker` example above. -//! -//! Note that the interaction between a [`Pin`] and [`Unpin`] is through the **pointee type** -//! of the value behind the `Ptr`, [`::Target`][Target]. Whether the `Ptr` type itself -//! implements [`Unpin`] does not affect the behavior of a [`Pin`]. For example, whether or not -//! [`Box`] is [`Unpin`] has no effect on the behavior of [Pin]<[Box]\> because -//! `T` is the type of the pointee value, not [`Box`]. So, whether `T` implements [`Unpin`] is -//! the thing that will affect the behavior of the [Pin]<[Box]\>. +//! implement [`Unpin`], you must opt out of [`Unpin`] via adding a field with the +//! [`PhantomPinned`] marker type, as we did with our latest `AddrTracker` example above. Without +//! doing this, you must not rely on the pinning guarantees to apply to your type! +//! +//! If you have reason to pin a value of a type that implements [`Unpin`] such that pinning-related +//! guarantees actually are respected, you'll need to create your own wrapper type which itself +//! opts out of implementing [`Unpin`] and contains a sub-field with the [`Unpin`] type that you +//! want to pin. +//! +//! Exposing access to the inner field which you want to remain pinned must then be carefully +//! considered as well! Remember, exposing a method that gives access to a +//! [Pin]<[&mut] InnerT>> where `InnerT: [Unpin]` would allow safe code to trivially +//! move the inner value out of that pinning pointer, which is precisely what you're seeking to +//! prevent! Exposing a field of a pinned value through a pinning pointer is called "projecting" +//! a pin, and the more general case of deciding in which cases a pin should be able to be +//! projected or not is called "structural pinning." We will go into more detail about this +//! [below][structural-pinning]. //! //! # Examples of address-sensitive types //! [address-sensitive-examples]: #examples-of-address-sensitive-types @@ -308,7 +361,7 @@ //! we could imagine being used to track a sliding window of `data` in parser //! code. //! -//! As mentioned before, this pattern is used extensively by compiler-generated +//! As mentioned before, this pattern is also used extensively by compiler-generated //! [`Future`]s. //! //! ```rust @@ -379,27 +432,39 @@ //! ## An intrusive, doubly-linked list //! [linked-list]: #an-intrusive-doubly-linked-list //! -//! In an intrusive doubly-linked list, the collection does not actually allocate the memory for the -//! nodes itself. Allocation is controlled by the clients, and nodes can live on a stack frame -//! that lives shorter than the collection does provided the nodes are removed from the -//! collection before returning. +//! In an intrusive doubly-linked list, the collection itself does not own the memory in which +//! each of its elements is stored. Instead, each client is free to allocate space for elements it +//! adds to the list in whichever manner it likes, including on the stack! Elements can live on a +//! stack frame that lives shorter than the collection does provided the elements that live in a +//! given stack frame are removed from the list before going out of scope. +//! +//! To make such an intrusive data structure work, every element stores pointers to its predecessor +//! and successor within its own data, rather than having the list structure itself managing those +//! pointers. It is in this sense that the structure is "intrusive": the details of how an +//! element is stored within the larger structure "intrudes" on the implementation of the element +//! type itself! //! //! The full implementation details of such a data structure are outside the scope of this //! documentation, but we will discuss how [`Pin`] can help to do so. //! -//! To make such an intrusive data structure work, every element stores pointers to its predecessor -//! and successor within its own data, rather than having the list structure itself manage those -//! pointers. Elements can only be added when they are pinned, because moving the elements -//! around would invalidate the pointers to it which are contained in the element ahead and behind -//! it. Moreover, the [`Drop`][Drop] implementation of the element types themselves will in some -//! way patch the pointers of its predecessor and successor elements to remove itself from the list. +//! Using such an intrusive pattern, elements may only be added when they are pinned. If we think +//! about the consequences of adding non-pinned values to such a list, this becomes clear: +//! +//! *Moving* or otherwise invalidating an element's data would invalidate the pointers back to it +//! which are stored in the elements ahead and behind it. Thus, in order to soundly dereference +//! the pointers stored to the next and previous elements, we must satisfy the guarantee that +//! nothing has invalidated those pointers (which point to data which we do not own). +//! +//! Moreover, the [`Drop`][Drop] implementation of each element must in some way notify its +//! predecessor and successor elements that it should be removed from the list before it is fully +//! destroyed, otherwise the pointers back to it would again become invalidated. //! -//! Crucially, this means we have to be able to rely on [`drop`] always being called before that +//! Crucially, this means we have to be able to rely on [`drop`] always being called before an //! element is invalidated. If an element could be deallocated or otherwise invalidated without -//! calling [`drop`], the pointers into it which are stored in its neighboring elements would +//! calling [`drop`], the pointers to it which are stored in its neighboring elements would //! become invalid, which would break the data structure. //! -//! Therefore, we rely on [the `Drop` guarantee][drop-guarantee] which comes with pinning data, +//! Therefore, pinning data also comes with [the "`Drop` guarantee"][drop-guarantee]. //! //! # Subtle details and the `Drop` guarantee //! [subtle-details]: self#subtle-details-and-the-drop-guarantee @@ -586,6 +651,7 @@ //! the new value. //! //! ## Projections and Structural Pinning +//! [structural-pinning]: self#projections-and-structural-pinning //! //! With ordinary structs, it is natural that we want to add *projection* methods //! that select one of the fields: From 6818d9278b10a2b50921e30f00454b99beb207d9 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 26 Sep 2023 20:13:40 +0200 Subject: [PATCH 10/40] improve intro and discussion of pinning as library contract --- library/core/src/pin.rs | 98 +++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 485ec689e3431..40dfb38948770 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -10,22 +10,23 @@ //! //! is called "pinning." We would say that a value which satisfies these guarantees has been //! "pinned," in that it has been permanently (until the end of its lifetime) attached to its -//! location in memory. Pinning a value is incredibly useful in that it provides the necessary -//! guarantees[^guarantees] for [`unsafe`] code to be able to dereference raw pointers to the pinned -//! value for the duration it is pinned (which, [as we'll see later][drop-guarantee], is necessarily -//! from the time the value is first pinned until the end of its lifetime). This concept of -//! "pinning" is necessary to implement safe interfaces on top of things like self-referential types -//! and intrusive data structures which cannot currently be modeled in fully safe Rust using only -//! borrow-checked [references][reference]. +//! location in memory, as though pinned to a pinboard. Pinning a value is incredibly useful in +//! that it provides the necessary guarantees[^guarantees] for [`unsafe`] code to be able to +//! dereference raw pointers to the pinned value for the duration it is pinned (which, +//! [as we'll see later][drop-guarantee], is necessarily from the time the value is first pinned +//! until the end of its lifetime). This concept of "pinning" is necessary to implement safe +//! interfaces on top of things like self-referential types and intrusive data structures which +//! cannot currently be modeled in fully safe Rust using only borrow-checked +//! [references][reference]. //! //! "Pinning" allows us to put a *value* which exists at some location in memory into a state where //! safe code cannot *move* that value to a different location in memory or otherwise invalidate it //! at its current location (unless it implements [`Unpin`], which we will -//! [talk about below][unpin]). Anything that wants to interact with the pinned value in a way that -//! has the potential to violate these guarantees must promise that it will not actually violate -//! them, using the [`unsafe`] keyword to mark that such a promise is upheld by the user and not -//! the compiler. In this way, we can allow other [`unsafe`] code to rely on any pointers that -//! point to the pinned value to be valid to dereference while it is pinned. +//! [talk about below][self#unpin]). Anything that wants to interact with the pinned value in a way +//! that has the potential to violate these guarantees must promise that it will not actually +//! violate them, using the [`unsafe`] keyword to mark that such a promise is upheld by the user +//! and not the compiler. In this way, we can allow other [`unsafe`] code to rely on any pointers +//! that point to the pinned value to be valid to dereference while it is pinned. //! //! [^guarantees]: Pinning on its own does not provide *all* the invariants necessary here. However, //! in order to validly pin a value in the first place, it must already satisfy the other invariants @@ -61,7 +62,7 @@ //! [`Copy`]ing a value from one place in memory to another. In Rust, "move" carries with it the //! semantics of ownership transfer from one variable to another, which is the key difference //! between a [`Copy`] and a move. For the purposes of this module's documentation, however, when -//! we write *move* in italics, we mean *specifically* that the value has moved in the mechanical +//! we write *move* in italics, we mean *specifically* that the value has *moved* in the mechanical //! sense of being located at a new place in memory. //! //! All values in Rust are trivially *moveable*. This means that the address at which a value is @@ -184,12 +185,38 @@ //! will not be *moved* or [otherwise invalidated][subtle-details]. //! //! We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning reference, or pinning -//! `Box`, etc.) because its existince is the thing that is pinning the underlying pointee in place: -//! it is the metaphorical "pin" securing the data in place on the pinboard (in memory). +//! `Box`, etc.) because its existince is the thing that is *symbolically* pinning the underlying +//! pointee in place: it is the metaphorical "pin" securing the data in place on the pinboard +//! (in memory). +//! +//! Notice that the thing wrapped by [`Pin`] is not the value which we want to pin itself, but +//! rather a pointer to that value! A [`Pin`] does not pin the `Ptr`; instead, it pins the +//! pointer's ***pointee** value*. +//! +//! ### Pinning as a library contract +//! +//! Pinning does not require nor make use of any compiler "magic"[^noalias], only a specific +//! contract between the [`unsafe`] parts of a library API and its users. +//! +//! It is important to stress this point as a user of the [`unsafe`] parts of the [`Pin`] API. +//! Practically, this means that performing the mechanics of "pinning" a value by creating a +//! [`Pin`] to it *does not* actually change the way the compiler behaves towards the +//! inner value! It is possible to use incorrect [`unsafe`] code to create a [`Pin`] to a +//! value which does not actually satisfy the invariants that a pinned value must satisfy, and in +//! this way lead undefined behavior even in (from that point) fully safe code. Similarly, using +//! [`unsafe`], one may get access to a bare [`&mut T`] from a [`Pin`] and +//! juse that to invalidly *move* pinned the value out. It is the job of the user of the +//! [`unsafe`] parts of the [`Pin`] API to ensure these invariants are not violated. +//! +//! This differs from e.g. [`UnsafeCell`] which changes the semantics of a program's compiled +//! output. A [`Pin`] is a handle to a value which we have promised we will not move out of, +//! but Rust still considers all values themselves to be fundamentally moveable through, *e.g.* +//! assignment or [`mem::replace`]. //! -//! It is important to stress that the thing in the [`Pin`] is not the value which we want to pin -//! itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather -//! the pointer's ***pointee** value*. +//! [^noalias]: There is a bit of nuance here that is still being decided about what the aliasing +//! semantics of `Pin<&mut T>` should be, but this is true as of today. +//! +//! ### How [`Pin`] prevents misuse in safe code //! //! In order to accomplish the goal of pinning the pointee value, [`Pin`] restricts access to //! the wrapped `Ptr` type in safe code. Specifically, [`Pin`] disallows the ability to access @@ -199,23 +226,25 @@ //! through that [&mut] T it would be possible to *move* the underlying value out of //! the pointer with [`mem::replace`], etc. //! -//! This promise must be upheld manually by [`unsafe`] code which interacts with the [`Pin`] -//! so that other [`unsafe`] code can rely on the pointee value being *un-moved* and valid. -//! Interfaces that operate on values which are in an address-sensitive state accept an argument -//! like [Pin]<[&mut] T> or [Pin]<[Box]\> to indicate this contract to -//! the caller. +//! As discussed above, this promise must be upheld manually by [`unsafe`] code which interacts +//! with the [`Pin`] so that other [`unsafe`] code can rely on the pointee value being +//! *un-moved* and valid. Interfaces that operate on values which are in an address-sensitive state +//! accept an argument like [Pin]<[&mut] T> or [Pin]<[Box]\> to +//! indicate this contract to the caller. //! -//! [As discussed below][drop-guarantee], this has consequences for running -//! destructors of pinned memory, too. +//! [As discussed below][drop-guarantee], opting in to using pinning guarantees in the interface +//! of an address-sensitive type has consequences for the implementation of some safe traits on +//! that type as well. //! //! ## Interaction between [`Deref`] and [`Pin`] //! //! Since [`Pin`] can wrap any pointer type, it uses [`Deref`] and [`DerefMut`] in //! order to identify the type of the pinned pointee data and provide (restricted) access to it. //! -//! A [`Pin`] where [`Ptr: Deref`][Deref] is a "`Ptr`-style pointer" to a pinned -//! [`P::Target`][Target] – so, a [Pin]<[Box]\> is an owned pointer to a pinned `T`, -//! and a [Pin]<[Rc]\> is a reference-counted pointer to a pinned `T`. +//! A [`Pin`] where [`Ptr: Deref`][Deref] is a "`Ptr`-style pinning pointer" to a pinned +//! [`P::Target`][Target] – so, a [Pin]<[Box]\> is an owned, pinning pointer to a +//! pinned `T`, and a [Pin]<[Rc]\> is a reference-counted, pinning pointer to a +//! pinned `T`. //! //! [`Pin`] also uses the [`::Target`][Target] type information to modify the //! interface it is allowed to provide for interacting with that data (for example, when a @@ -228,17 +257,6 @@ //! types with such "malicious" implementations of [`Deref`]; see [`Pin::new_unchecked`] for //! details. //! -//! ## Pinning as a library contract -//! -//! Pinning does not require any compiler "magic"[^noalias], only a specific contract between the -//! library API and its users. This differs from e.g. [`UnsafeCell`] which changes the semantics of -//! a program's compiled output. A [`Pin`] is a handle to a value which does not allow moving -//! the value out, but Rust still considers all values themselves to be moveable with e.g. -//! [`mem::replace`]. -//! -//! [^noalias]: There is a bit of nuance here that is still being decided about what the aliasing -//! semantics of `Pin<&mut T>` should be, but this is true as of today. -//! //! ## Fixing `AddrTracker` //! //! The guarantee of a stable address is necessary to make our `AddrTracker` example work. When @@ -850,6 +868,7 @@ //! [`std::alloc`]: ../../std/alloc/index.html //! [`Box`]: ../../std/boxed/struct.Box.html //! [Box]: ../../std/boxed/struct.Box.html "Box" +//! [`Box`]: ../../std/boxed/struct.Box.html "Box" //! [`Rc`]: ../../std/rc/struct.Rc.html //! [Rc]: ../../std/rc/struct.Rc.html "rc::Rc" //! [`Vec`]: ../../std/vec/struct.Vec.html @@ -861,6 +880,7 @@ //! [`Vec::set_len`]: ../../std/vec/struct.Vec.html#method.set_len //! [`VecDeque`]: ../../std/collections/struct.VecDeque.html //! [VecDeque]: ../../std/collections/struct.VecDeque.html "collections::VecDeque" +//! [`String`]: ../../std/string/struct.String.html "String" #![stable(feature = "pin", since = "1.33.0")] From bebbe24a6331ac2ea9f4d77466bedebb99f05709 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 26 Sep 2023 20:14:05 +0200 Subject: [PATCH 11/40] improve `Pin` struct docs and add examples --- library/core/src/pin.rs | 97 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 8 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 40dfb38948770..c1f82bf6f8b84 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -900,14 +900,17 @@ use crate::{ /// A pointer which pins its pointee in place. /// /// [`Pin`] is a wrapper around some kind of pointer `Ptr` which makes that pointer "pin" its -/// pointee value in place, thus preventing the value referenced by that pointer from being moved or -/// otherwise invalidated at that place in memory unless it implements [`Unpin`]. +/// pointee value in place, thus preventing the value referenced by that pointer from being moved +/// or otherwise invalidated at that place in memory unless it implements [`Unpin`]. +/// +/// *See the [`pin` module] documentation for a more thorough exploration of pinning.* /// /// ## Pinning values with [`Pin`] /// /// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a /// [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** -/// will not be *moved* or [otherwise invalidated][subtle-details]. +/// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is impossible +/// to create or misuse a [`Pin`] which can violate this promise without using [`unsafe`]. /// /// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning /// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in @@ -917,13 +920,91 @@ use crate::{ /// itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather /// the pointer's ***pointee** value*. /// -/// `Pin

` is guaranteed to have the same memory layout and ABI as `P`. +/// For the vast majoriy of Rust types, pinning a value of that type will actually have no effect. +/// This is because the vast majority of types implement the [`Unpin`] trait, which entirely opts +/// all values of that type out of pinning-related guarantees. The most common exception +/// to this is the compiler-generated types that implement [`Future`] for the return value +/// of `async fn`s. These compiler-generated [`Future`]s do not implement [`Unpin`] for reasons +/// explained more in the [`pin` module] docs, but suffice it to say they require the guarantees +/// provided by pinning to be implemented soundly. /// -/// *See the [`pin` module] documentation for a more thorough exploration of pinning.* +/// This requirement in the implementation of `async fn`s means that the [`Future`] trait requires +/// any [`Future`] to be pinned in order to call [`poll`] on it. Therefore, when manually polling +/// a future, you will need to pin it first. +/// +/// ### Pinning a value inside a [`Box`] +/// +/// The simplest and most flexible way to pin a value is to put that value inside a [`Box`] and +/// then turn that [`Box`] into a "pinning [`Box`]" by wrapping it in a [`Pin`]. +/// You can do both of these in a single step using [`Box::pin`]. Let's see an example of using +/// this flow to pin a [`Future`] returned from calling an `async fn`, a common use case +/// as described above. +/// +/// ``` +/// use std::pin::Pin; +/// +/// async fn add_one(x: u32) -> u32 { +/// x + 1 +/// } +/// +/// // Call the async function to get a future back +/// let fut = add_one(42); +/// +/// // Pin the future inside a pinning box +/// let pinned_fut: Pin> = Box::pin(fut); +/// ``` +/// +/// If you have a value which is already boxed, for example a [`Box`][Box], you can pin +/// that value in-place at its current memory address using [`Box::into_pin`]. +/// +/// ``` +/// use std::pin::Pin; +/// use std::future::Future; +/// +/// async fn add_one(x: u32) -> u32 { +/// x + 1 +/// } +/// +/// fn boxed_add_one(x: u32) -> Box> { +/// Box::new(add_one(x)) +/// } +/// +/// let boxed_fut = boxed_add_one(42); +/// +/// // Pin the future inside the existing box +/// let pinned_fut: Pin> = Box::into_pin(boxed_fut); +/// ``` +/// +/// There are similar pinning methods offered on the other standard library smart pointer types +/// as well, like [`Rc`] and [`Arc`]. +/// +/// ### Pinning a value on the stack using [`pin!`] +/// +/// There are some situations where it is desirable or even required (for example, in a `#[no_std]` +/// context where you don't have access to the standard library or allocation in general) to +/// pin a value to its location on the stack. Doing so is possible using the [`pin!`] macro. See +/// its documentation for more. +/// +/// ## Layout and ABI +/// +/// [`Pin`] is guaranteed to have the same memory layout and ABI[^noalias] as `Ptr`. +/// +/// [^noalias]: There is a bit of nuance here that is still being decided about whether the +/// aliasing semantics of `Pin<&mut T>` should be different than `&mut T`, but this is true as of +/// today. /// -/// [`pin` module]: self -/// [`Box`]: ../../std/boxed/struct.Box.html -/// [subtle-details]: self#subtle-details-and-the-drop-guarantee +/// [`pin!`]: crate::pin::pin "pin!" +/// [`Future`]: crate::future::Future "Future" +/// [`poll`]: crate::future::Future::poll "Future::poll" +/// [`pin` module]: self "pin module" +/// [`Rc`]: ../../std/rc/struct.Rc.html "Rc" +/// [`Arc`]: ../../std/sync/struct.Arc.html "Arc" +/// [Box]: ../../std/boxed/struct.Box.html "Box" +/// [`Box`]: ../../std/boxed/struct.Box.html "Box" +/// [`Box::pin`]: ../../std/boxed/struct.Box.html#method.pin "Box::pin" +/// [`Box::into_pin`]: ../../std/boxed/struct.Box.html#method.into_pin "Box::into_pin" +/// [subtle-details]: self#subtle-details-and-the-drop-guarantee "pin subtle details" +/// [`unsafe`]: ../../std/keyword.unsafe.html "keyword unsafe" // // Note: the `Clone` derive below causes unsoundness as it's possible to implement // `Clone` for mutable references. From 82a68171d397804604378a1d0341a965172afe04 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Wed, 27 Sep 2023 11:38:46 +0200 Subject: [PATCH 12/40] fix link in footnote --- library/core/src/pin.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index c1f82bf6f8b84..785044e689d88 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -31,9 +31,9 @@ //! [^guarantees]: Pinning on its own does not provide *all* the invariants necessary here. However, //! in order to validly pin a value in the first place, it must already satisfy the other invariants //! for it to be valid to dereference a pointer to that value while it is pinned, and using the -//! [`Drop` guarantee][drop-guarantee], we can ensure that any interested parties are notified -//! before the value becomes no longer pinned, i.e. when the value goes out of scope and is -//! invalidated. +//! [`Drop` guarantee][self#subtle-details-and-the-drop-guarantee], we can ensure that any +//! interested parties are notified before the value becomes no longer pinned, i.e. when the value +//! goes out of scope and is invalidated. //! //! Note that as long as you don't use [`unsafe`], it's impossible to create or misuse a pinned //! value in a way that will produce unsoundness. See the documentation of [`Pin`] for more From e2e8746bb634a4b58fd6e424985b59b4f6aba089 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Wed, 27 Sep 2023 11:39:08 +0200 Subject: [PATCH 13/40] reword unpin auto impl section --- library/core/src/marker.rs | 14 +++++++------- library/core/src/pin.rs | 32 +++++++++++++------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index bd19d3cc0e290..5b527dfc9e7e9 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -938,13 +938,13 @@ marker_impls! { /// mem::replace(&mut *pinned_string, "other".to_string()); /// ``` /// -/// This trait is automatically implemented for almost every type. The compiler (and you!) is free -/// to take the conservative stance of marking types as [`Unpin`] by default. This is because if a -/// type implements [`Unpin`], then it is unsound for [`unsafe`] code to assume that type is truly -/// pinned, *even* when viewed through a "pinning" pointer! It is the responsibility of the -/// implementor of [`unsafe`] code that relies upon pinning for soundness to ensure that all the -/// types it expects to be truly pinned do not implement [`Unpin`]. For more details, see the -/// [`pin` module] docs! +/// This trait is automatically implemented for almost every type. The compiler is free +/// to take the conservative stance of marking types as [`Unpin`] so long as all of the types that +/// compose its fields are also [`Unpin`]. This is because if a type implements [`Unpin`], then it +/// is unsound for that type's implementation to rely on pinning-related guarantees for soundness, +/// *even* when viewed through a "pinning" pointer! It is the responsibility of the implementor of +/// a type that relies upon pinning for soundness to ensure that type is *not* marked as [`Unpin`] +/// by adding [`PhantomPinned`] field. For more details, see the [`pin` module] docs. /// /// [`mem::replace`]: crate::mem::replace "mem replace" /// [`Pin`]: crate::pin::Pin "Pin" diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 785044e689d88..eb9c0818f1767 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -205,7 +205,7 @@ //! value which does not actually satisfy the invariants that a pinned value must satisfy, and in //! this way lead undefined behavior even in (from that point) fully safe code. Similarly, using //! [`unsafe`], one may get access to a bare [`&mut T`] from a [`Pin`] and -//! juse that to invalidly *move* pinned the value out. It is the job of the user of the +//! use that to invalidly *move* pinned the value out. It is the job of the user of the //! [`unsafe`] parts of the [`Pin`] API to ensure these invariants are not violated. //! //! This differs from e.g. [`UnsafeCell`] which changes the semantics of a program's compiled @@ -336,24 +336,18 @@ //! unsound without being expressed through pinning, and they would then need to not //! implement [`Unpin`]. //! -//! The compiler (and users!) is free to take the conservative stance of marking types as [`Unpin`] -//! by default. This is because if a type implements [`Unpin`], then it is unsound for [`unsafe`] -//! code to assume that type is truly pinned, *even* when viewed through a "pinning" pointer! It is -//! the responsibility of *the implementor of [`unsafe`] code that relies upon pinning for -//! soundness* (you, in this case!) to ensure that all the types which that code expects to be truly -//! pinned do not implement [`Unpin`]. -//! -//! Like other auto-traits, the compiler will automatically determine that a type implements -//! [`Unpin`] if all its fields also implement [`Unpin`]. If you are building a type which consists -//! of only [`Unpin`] types but has an address-sensistive state and thus should not itself -//! implement [`Unpin`], you must opt out of [`Unpin`] via adding a field with the -//! [`PhantomPinned`] marker type, as we did with our latest `AddrTracker` example above. Without -//! doing this, you must not rely on the pinning guarantees to apply to your type! -//! -//! If you have reason to pin a value of a type that implements [`Unpin`] such that pinning-related -//! guarantees actually are respected, you'll need to create your own wrapper type which itself -//! opts out of implementing [`Unpin`] and contains a sub-field with the [`Unpin`] type that you -//! want to pin. +//! The compiler is free to take the conservative stance of marking types as [`Unpin`] so long as +//! all of the types that compose its fields are also [`Unpin`]. This is because if a type +//! implements [`Unpin`], then it is unsound for that type's implementation to rely on +//! pinning-related guarantees for soundness, *even* when viewed through a "pinning" pointer! It is +//! the responsibility of the implementor of a type that relies upon pinning for soundness to +//! ensure that type is *not* marked as [`Unpin`] by adding [`PhantomPinned`] field. This is +//! exactly what we did with our `AddrTracker` example above. Without doing this, you *must not* +//! rely on pinning-related guarantees to apply to your type! +//! +//! If need to truly pin a value of a foreign or built-in type that implements [`Unpin`], you'll +//! need to create your own wrapper type around the [`Unpin`] type you want to pin and then +//! opts-out of [`Unpin`] using [`PhantomPinned`]. //! //! Exposing access to the inner field which you want to remain pinned must then be carefully //! considered as well! Remember, exposing a method that gives access to a From 7c9c71260e8fded5cb52ca9418b6d99398be2b8c Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Wed, 27 Sep 2023 11:45:54 +0200 Subject: [PATCH 14/40] improve structural Unpin + formatting --- library/core/src/pin.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index eb9c0818f1767..b4c2e65ad7056 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -753,9 +753,11 @@ //! 1. *Structural [`Unpin`].* A struct can be [`Unpin`] if, and only if, all of its //! structurally-pinned fields are, too. This is [`Unpin`]'s behavior by default. //! However, as a libray author, it is your responsibility not to write something like -//! unsafe impl\ [Unpin] for Struct\ {}. (Adding *any* projection -//! operation requires unsafe code, so the fact that [`Unpin`] is a safe trait does not break -//! the principle that you only have to worry about any of this if you use [`unsafe`].) +//! impl\ [Unpin] for Struct\ {} and then offer a method that provides +//! structural pinning to an inner field of `T`, which may not be [`Unpin`]! (Adding *any* +//! projection operation requires unsafe code, so the fact that [`Unpin`] is a safe trait does +//! not break the principle that you only have to worry about any of this if you use +//! [`unsafe`].) //! //! 2. *Pinned Destruction.* As discussed [above][drop-impl], [`drop`] takes //! [`&mut self`], but the struct (and hence its fields) might have been pinned @@ -764,14 +766,14 @@ //! //! As a consequence, the struct *must not* be [`#[repr(packed)]`][packed]. //! -//! 3. *Structural Notice of Destruction.* You must uphold the the [`Drop` guarantee][drop-guarantee]: -//! once your struct is pinned, the struct's storage cannot be re-used without calling the -//! structurally-pinned fields' destructors, as well. +//! 3. *Structural Notice of Destruction.* You must uphold the the +//! [`Drop` guarantee][drop-guarantee]: once your struct is pinned, the struct's storage cannot +//! be re-used without calling the structurally-pinned fields' destructors, as well. //! //! This can be tricky, as witnessed by [`VecDeque`]: the destructor of [`VecDeque`] //! can fail to call [`drop`] on all elements if one of the destructors panics. This violates -//! the [`Drop` guarantee][drop-guarantee], because it can lead to elements being deallocated without -//! their destructor being called. +//! the [`Drop` guarantee][drop-guarantee], because it can lead to elements being deallocated +//! without their destructor being called. //! //! [`VecDeque`] has no pinning projections, so its destructor is sound. If it wanted //! to provide such structural pinning, its destructor would need to abort the process if any From 252a83b63f91688ce230db3ed713ed5e5e90e15d Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Thu, 28 Sep 2023 15:27:05 +0200 Subject: [PATCH 15/40] add section on manual owning ptr managed solution via @kpreid --- library/core/src/pin.rs | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index b4c2e65ad7056..7417fa2dd53ac 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -163,20 +163,47 @@ //! first option is ruled out. //! //! In order to implement the second option, we must in some way enforce its key invariant, -//! *i.e.* prevent the value from being *moved* or otherwise invalidated. (You may notice this -//! sounds an awful lot like the definition of *pinning* a value). There are two ways by -//! which we might enforce this validity invariant in Rust: +//! *i.e.* prevent the value from being *moved* or otherwise invalidated (you may notice this +//! sounds an awful lot like the definition of *pinning* a value). There a few ways one might be +//! able to enforce this invariant in Rust: //! //! 1. Offer a wholly `unsafe` API to interact with the object, thus requiring every caller to -//! uphold the invariant themselves; or, -//! 2. Leverage the type system to encode and enforce this invariant by presenting a restricted -//! API surface to interact with the object +//! uphold the invariant themselves +//! 2. Store the value that must not be moved behind a carefully managed pointer internal to +//! the object +//! 3. Leverage the type system to encode and enforce this invariant by presenting a restricted +//! API surface to interact with *any* object that requires these invariants //! -//! The first option is quite obviously undesirable, as the `unsafe`ty of the interface will +//! The first option is quite obviously undesirable, as the [`unsafe`]ty of the interface will //! become viral throughout all code that interacts with the object. //! -//! [`Pin`] is an implementation of the second option, allowing us to pin a value in place -//! until its [`drop`] runs in a way that we can depend on it staying valid in `unsafe` code. +//! The second option is a viable solution to the problem for some use cases, in particular +//! for self-referrential types. Under this model, any type that has an address sensitive state +//! would ultimately store its data in something like a [`Box`] and then carefully manage +//! the access to that data internally to ensure no *moves* or other invalidation occur, then +//! provide a safe interface on top. +//! +//! There are a couple of linked disadvantages to using this model. The core issue is a lack +//! of generality. This is an issue first because it means hat each individual type that +//! implements such an interface does so on its own. Each individual developer must themselves +//! think through all the guarantees needed to ensure the API they present is sound. This puts +//! a greater burden on each developer, rather than allowing building a shared understanding of the +//! problem space, encoded into a shared interface to solve it. In addition, and the key issue that +//! drove Rust towards another solution, is that each individual object must assume it is on its +//! own in ensuring that its data does not become *moved* or otherwise invalidated. Since there is +//! no shared contract between values of different types, an object cannot assume that others +//! interacting with it will be a good citizen with its data. Because of this, *composition* of +//! address-sensitive types requires at least a level of pointer indirection (and, practically, a +//! heap allocation) each time a new object is added to the mix. This is particularly a problem +//! when one considers the implications of composing together the [`Future`]s which will eventaully +//! make up an asynchronous task (including address-sensitive `async fn` state machines). +//! It is plausible that there could be many layers of [`Future`]s composed together, including +//! multiple layers of `async fn`s handling different parts of a task, and it was deemed +//! unacceptable to force indirection and allocation for each layer of composition in this case. +//! +//! [`Pin`] is an implementation of the third option. It allows us to solve the issues +//! discussed with the second option by building a *shared contractual language* around the +//! guarantees of "pinning" data. //! //! ## Using [`Pin`] to pin values //! From f2447a6a70149dbef12c41e22d163b0be92588a3 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Thu, 28 Sep 2023 15:34:53 +0200 Subject: [PATCH 16/40] fix typos --- library/core/src/pin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 7417fa2dd53ac..5ddd0c29adf3c 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -230,7 +230,7 @@ //! [`Pin`] to it *does not* actually change the way the compiler behaves towards the //! inner value! It is possible to use incorrect [`unsafe`] code to create a [`Pin`] to a //! value which does not actually satisfy the invariants that a pinned value must satisfy, and in -//! this way lead undefined behavior even in (from that point) fully safe code. Similarly, using +//! this way lead to undefined behavior even in (from that point) fully safe code. Similarly, using //! [`unsafe`], one may get access to a bare [`&mut T`] from a [`Pin`] and //! use that to invalidly *move* pinned the value out. It is the job of the user of the //! [`unsafe`] parts of the [`Pin`] API to ensure these invariants are not violated. @@ -423,9 +423,9 @@ //! /// Create a new `Unmovable`. //! /// //! /// To ensure the data doesn't move we place it on the heap behind a pinning Box. -//! /// Note that the data is pinned, but the which is pinning it can itself still be moved. -//! /// This is important because it means we can return the pointer from the function, which -//! /// is itself a kind of move! +//! /// Note that the data is pinned, but the `Pin>` which is pinning it can +//! /// itself still be moved. This is important because it means we can return the pinning +//! /// pointer from the function, which is itself a kind of move! //! fn new() -> Pin> { //! let res = Unmovable { //! data: [0; 64], From 921d37dbd57541e1b6fe97c439cef307d47da7f4 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Thu, 28 Sep 2023 15:37:29 +0200 Subject: [PATCH 17/40] fix imports --- library/core/src/pin.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 5ddd0c29adf3c..19beaf2a6745c 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -292,9 +292,9 @@ //! to it *cannot* panic. //! //! ``` -//! use core::marker::PhantomPinned; -//! use core::pin::Pin; -//! use core::pin::pin; +//! use std::marker::PhantomPinned; +//! use std::pin::Pin; +//! use std::pin::pin; //! //! #[derive(Default)] //! struct AddrTracker { @@ -327,9 +327,8 @@ //! let mut ptr_to_pinned_tracker: Pin<&mut AddrTracker> = pin!(tracker); //! ptr_to_pinned_tracker.as_mut().check_for_move(); //! -//! // Trying to access `tracker` or pass `ptr_to_pinned_tracker` to anything -//! // that requires mutable access to a non-pinned version of it will no longer -//! // compile +//! // Trying to access `tracker` or pass `ptr_to_pinned_tracker` to anything that requires +//! // mutable access to a non-pinned version of it will no longer compile //! //! // 3. We can now assume that the tracker value will never be moved, thus //! // this will never panic! From 6d5f43d77d2209741fc9103096a554366f38993d Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Thu, 28 Sep 2023 16:11:43 +0200 Subject: [PATCH 18/40] edit new section for typos and better wording --- library/core/src/pin.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 19beaf2a6745c..9eea37c62f9a1 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -184,22 +184,27 @@ //! provide a safe interface on top. //! //! There are a couple of linked disadvantages to using this model. The core issue is a lack -//! of generality. This is an issue first because it means hat each individual type that -//! implements such an interface does so on its own. Each individual developer must themselves -//! think through all the guarantees needed to ensure the API they present is sound. This puts -//! a greater burden on each developer, rather than allowing building a shared understanding of the -//! problem space, encoded into a shared interface to solve it. In addition, and the key issue that -//! drove Rust towards another solution, is that each individual object must assume it is on its -//! own in ensuring that its data does not become *moved* or otherwise invalidated. Since there is -//! no shared contract between values of different types, an object cannot assume that others -//! interacting with it will be a good citizen with its data. Because of this, *composition* of -//! address-sensitive types requires at least a level of pointer indirection (and, practically, a -//! heap allocation) each time a new object is added to the mix. This is particularly a problem -//! when one considers the implications of composing together the [`Future`]s which will eventaully -//! make up an asynchronous task (including address-sensitive `async fn` state machines). -//! It is plausible that there could be many layers of [`Future`]s composed together, including -//! multiple layers of `async fn`s handling different parts of a task, and it was deemed -//! unacceptable to force indirection and allocation for each layer of composition in this case. +//! of generality. This is an issue because it means that each individual type that implements +//! such an interface does so on its own. Each developer implementing such a type must themselves +//! think through all the guarantees needed to ensure the API they present is sound. We would +//! rather build a shared understanding of the problem space and encode that understanding into a +//! shared interface to solve it which everyone helps validate. +//! +//! In addition, and the key issue that drove Rust towards developing another solution, is that +//! in this model, each individual object must assume it is *on its own* to ensure that its data +//! does not become *moved* or otherwise invalidated. Since there is no shared contract between +//! values of different types, an object cannot assume that others interacting with it will +//! properly respect the invariants around interacting with its data and must therefore protect +//! it from everyone. Because of this, *composition* of address-sensitive types requires at least +//! a level of pointer indirection each time a new object is added to the mix (and, practically, a +//! heap allocation). +//! +//! This is particularly a problem when one considers, for exapmle, the implications of composing +//! together the [`Future`]s which will eventaully make up an asynchronous task (including +//! address-sensitive `async fn` state machines). It is plausible that there could be many layers +//! of [`Future`]s composed together, including multiple layers of `async fn`s handling different +//! parts of a task. It was deemed unacceptable to force indirection and allocation for each layer +//! of composition in this case. //! //! [`Pin`] is an implementation of the third option. It allows us to solve the issues //! discussed with the second option by building a *shared contractual language* around the From de2e748a40c66998397c85216538f89942575dc3 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Thu, 28 Sep 2023 17:22:04 +0200 Subject: [PATCH 19/40] fix typos and edit prose --- library/core/src/pin.rs | 161 +++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 76 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 9eea37c62f9a1..bb315949d8bcc 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -36,7 +36,7 @@ //! goes out of scope and is invalidated. //! //! Note that as long as you don't use [`unsafe`], it's impossible to create or misuse a pinned -//! value in a way that will produce unsoundness. See the documentation of [`Pin`] for more +//! value in a way that is unsound. See the documentation of [`Pin`] for more //! information on the practicalities of how to pin a value and how to use that pinned value from a //! user's perspective without using [`unsafe`]. //! @@ -179,8 +179,8 @@ //! //! The second option is a viable solution to the problem for some use cases, in particular //! for self-referrential types. Under this model, any type that has an address sensitive state -//! would ultimately store its data in something like a [`Box`] and then carefully manage -//! the access to that data internally to ensure no *moves* or other invalidation occur, then +//! would ultimately store its data in something like a [`Box`], carefully manage internal +//! access to that data to ensure no *moves* or other invalidation occurs, and finally //! provide a safe interface on top. //! //! There are a couple of linked disadvantages to using this model. The core issue is a lack @@ -190,21 +190,20 @@ //! rather build a shared understanding of the problem space and encode that understanding into a //! shared interface to solve it which everyone helps validate. //! -//! In addition, and the key issue that drove Rust towards developing another solution, is that -//! in this model, each individual object must assume it is *on its own* to ensure that its data -//! does not become *moved* or otherwise invalidated. Since there is no shared contract between -//! values of different types, an object cannot assume that others interacting with it will -//! properly respect the invariants around interacting with its data and must therefore protect -//! it from everyone. Because of this, *composition* of address-sensitive types requires at least -//! a level of pointer indirection each time a new object is added to the mix (and, practically, a -//! heap allocation). -//! -//! This is particularly a problem when one considers, for exapmle, the implications of composing -//! together the [`Future`]s which will eventaully make up an asynchronous task (including -//! address-sensitive `async fn` state machines). It is plausible that there could be many layers -//! of [`Future`]s composed together, including multiple layers of `async fn`s handling different -//! parts of a task. It was deemed unacceptable to force indirection and allocation for each layer -//! of composition in this case. +//! In addition, in this model, each individual object must assume it is *on its own* to ensure +//! that its data does not become *moved* or otherwise invalidated. Since there is no shared +//! contract between values of different types, an object cannot assume that others interacting +//! with it will properly respect the invariants around interacting with its data and must +//! therefore protect it from everyone. Because of this, *composition* of address-sensitive types +//! requires at least a level of pointer indirection each time a new object is added to the mix +//! (and, practically, a heap allocation). +//! +//! This is the key thing that drove Rust towards a different model. It is particularly a problem +//! when one considers, for exapmle, the implications of composing together the [`Future`]s which +//! will eventaully make up an asynchronous task (including address-sensitive `async fn` state +//! machines). It is plausible that there could be many layers of [`Future`]s composed together, +//! including multiple layers of `async fn`s handling different parts of a task. It was deemed +//! unacceptable to force indirection and allocation for each layer of composition in this case. //! //! [`Pin`] is an implementation of the third option. It allows us to solve the issues //! discussed with the second option by building a *shared contractual language* around the @@ -237,7 +236,7 @@ //! value which does not actually satisfy the invariants that a pinned value must satisfy, and in //! this way lead to undefined behavior even in (from that point) fully safe code. Similarly, using //! [`unsafe`], one may get access to a bare [`&mut T`] from a [`Pin`] and -//! use that to invalidly *move* pinned the value out. It is the job of the user of the +//! use that to invalidly *move* the pinned value out. It is the job of the user of the //! [`unsafe`] parts of the [`Pin`] API to ensure these invariants are not violated. //! //! This differs from e.g. [`UnsafeCell`] which changes the semantics of a program's compiled @@ -496,7 +495,7 @@ //! *Moving* or otherwise invalidating an element's data would invalidate the pointers back to it //! which are stored in the elements ahead and behind it. Thus, in order to soundly dereference //! the pointers stored to the next and previous elements, we must satisfy the guarantee that -//! nothing has invalidated those pointers (which point to data which we do not own). +//! nothing has invalidated those pointers (which point to data that we do not own). //! //! Moreover, the [`Drop`][Drop] implementation of each element must in some way notify its //! predecessor and successor elements that it should be removed from the list before it is fully @@ -504,7 +503,7 @@ //! //! Crucially, this means we have to be able to rely on [`drop`] always being called before an //! element is invalidated. If an element could be deallocated or otherwise invalidated without -//! calling [`drop`], the pointers to it which are stored in its neighboring elements would +//! calling [`drop`], the pointers to it stored in its neighboring elements would //! become invalid, which would break the data structure. //! //! Therefore, pinning data also comes with [the "`Drop` guarantee"][drop-guarantee]. @@ -513,20 +512,20 @@ //! [subtle-details]: self#subtle-details-and-the-drop-guarantee //! [drop-guarantee]: self#subtle-details-and-the-drop-guarantee //! -//! The purpose of pinning is not *just* to prevent a value from being *moved*, but rather more +//! The purpose of pinning is not *just* to prevent a value from being *moved*, but more //! generally to be able to rely on the pinned value *remaining valid **at a specific place*** in //! memory. //! //! To do so, pinning a value adds an *additional* invariant that must be upheld in order for use -//! of the pinned data to be valid, on top of the ones that must be upheld for a non-pinned value of -//! the same type: +//! of the pinned data to be valid, on top of the ones that must be upheld for a non-pinned value +//! of the same type to be valid: //! //! From the moment a value is pinned by constructing a [`Pin`]ning pointer to it, that value //! must *remain, **valid***, at that same address in memory, *until its [`drop`] handler is //! called.* //! -//! There is some subtlety to this which we have not yet talked about in detail. The invariant above -//! means that, yes, +//! There is some subtlety to this which we have not yet talked about in detail. The invariant +//! described above means that, yes, //! //! 1. The value must not be moved out of its location in memory //! @@ -540,31 +539,31 @@ //! ## `Drop` guarantee //! //! There needs to be a way for a pinned value to notify any code that is relying on its pinned -//! status that it is about to be destroyed, so that such code can remove its address from their -//! data structures or otherwise change their behavior with the knowledge that they can no longer -//! rely on that value existing at the same location. +//! status that it is about to be destroyed. In this way, the dependent code can remove the +//! pinned value's address from its data structures or otherwise change its behavior with the +//! knowledge that it can no longer rely on that value existing at the location it was pinned to. //! //! Thus, in any situation where we may want to overwrite a pinned value, that value's [`drop`] must //! be called beforehand (unless the pinned value implements [`Unpin`], in which case we can ignore //! all of [`Pin`]'s guarantees, as usual). //! -//! The most common storage-reuse situation is when a value on the stack is destroyed as part of a -//! function return, or when heap storage is freed. In both cases, [`drop`] gets run for us -//! by Rust when using standard safe code. However, for heap or otherwise custom-allocated storage, -//! [`unsafe`] code must make sure to call [`ptr::drop_in_place`] before deallocating and re-using -//! said storage. +//! The most common storage-reuse situations occur when a value on the stack is destroyed as part +//! of a function return and when heap storage is freed. In both cases, [`drop`] gets run for us +//! by Rust when using standard safe code. However, for manual heap allocations or otherwise +//! custom-allocated storage, [`unsafe`] code must make sure to call [`ptr::drop_in_place`] before +//! deallocating and re-using said storage. //! -//! However, reuse can happen even if no storage is (de-)allocated. For example, if we had an -//! [`Option`] which contained a `Some(v)` where `v` is a pinned value, then `v` would be -//! invalidated by setting that option to `None`. +//! In addition, storage "re-use"/invalidation can happen even if no storage is (de-)allocated. +//! For example, if we had an [`Option`] which contained a `Some(v)` where `v` is pinned, then `v` +//! would be invalidated by setting that option to `None`. //! -//! Similarly, if a [`Vec`] was used to store pinned values and [`Vec::set_len`] is used to manually -//! "kill" some elements of a vector, all value "killed" would become invalidated. +//! Similarly, if a [`Vec`] was used to store pinned values and [`Vec::set_len`] was used to +//! manually "kill" some elements of a vector, all of the items "killed" would become invalidated, +//! which would be *undefined behavior* if those items were pinned. //! -//! Both of these cases are somewhat contrived, but it is crucial -//! to remember that [`Pin`]ned data *must* be [`drop`]ped before it is invalidated, as a matter of -//! soundness, not just to prevent memory leaks. As a corollary, the following code can *never* be -//! made safe: +//! Both of these cases are somewhat contrived, but it is crucial to remember that [`Pin`]ned data +//! *must* be [`drop`]ped before it is invalidated; not just to prevent memory leaks, but as a +//! matter of soundness. As a corollary, the following code can *never* be made safe: //! //! ```rust //! # use std::mem::ManuallyDrop; @@ -637,20 +636,21 @@ //! //! ### Implementing [`Drop`] for pointer types which will be used as [`Pin`]ning pointers //! -//! It should further be noted that creating a pinning pointer of some type `Ptr` to some underlying -//! `T` which is not `Unpin` *also* carries with it implications on the way that `Ptr` type must -//! implement [`Drop`] (as well as [`Deref`] and [`DerefMut`])! When implementing a pointer type -//! that may be used as a pinning pointer, you must also take the same care described above not to -//! *move* out of or otherwise invalidate the pointee during [`Drop`], [`Deref`], or [`DerefMut`] +//! It should further be noted that creating a pinning pointer of some type `Ptr` *also* carries +//! with it implications on the way that `Ptr` type must implement [`Drop`] +//! (as well as [`Deref`] and [`DerefMut`])! When implementing a pointer type that may be used as +//! a pinning pointer, you must also take the same care described above not to *move* out of or +//! otherwise invalidate the pointee during [`Drop`], [`Deref`], or [`DerefMut`] //! implementations. //! //! ## "Assigning" pinned data //! -//! Although in general it is not valid to swap data through a [`Pin`], or assign from -//! a [`Pin`], for the same reason that a *move* is invalid, there is no particular reason -//! to disallow doing it with specialized functions, as long as they know how to update all -//! uses of the pinned address (and any other `unsafe`-assumed invariants). For [`Unmovable`] ( -//! from the example above) we could write +//! Although in general it is not valid to swap data or assign through a [`Pin`] for the same +//! reason that reusing a pinned object's memory is invalid, it is possible to do validly when +//! implemented with special care for the needs of the exact data structure which is being +//! modified. For example, the assigning function must know how to update all uses of the pinned +//! address (and any other invariants necessary to satisfy validity for that type). For +//! [`Unmovable`] (from the example above), we could write an assignment function like so: //! //! ``` //! # use std::pin::Pin; @@ -689,15 +689,18 @@ //! Even though we can't have the compiler do the assignment for us, it's possible to write //! such specialized functions for types that might need it. //! -//! Note that it _is_ possible to assign through a [`Pin

`] by way of [`Pin::set()`]. This does -//! not violate any guarantees, since it will run [`drop`] on the pointee value before assigning -//! the new value. +//! Note that it _is_ possible to assign generically through a [`Pin

`] by way of [`Pin::set()`]. +//! This does not violate any guarantees, since it will run [`drop`] on the pointee value before +//! assigning the new value. Thus, the [`drop`] implementation still has a chance to perform the +//! necessary notifications to dependent values before the memory location of the original pinned +//! value is overwritten. //! //! ## Projections and Structural Pinning //! [structural-pinning]: self#projections-and-structural-pinning //! -//! With ordinary structs, it is natural that we want to add *projection* methods -//! that select one of the fields: +//! With ordinary structs, it is natural that we want to add *projection* methods that allow +//! borrowing one or more of the inner fields of a struct when the caller has access to a +//! borrow of the whole struct: //! //! ``` //! # struct Field; @@ -712,33 +715,35 @@ //! ``` //! //! When working with address-sensitive types, it's not obvious what the signature of these -//! functions should be. If `field` takes self: [Pin]<[&mut Struct][&mut]>, should it return -//! [`&mut Field`] or [Pin]<[`&mut Field`]>? This question also arises with `enum`s and -//! wrapper types like [`Vec`], [`Box`], and [`RefCell`]. (This question -//! applies just as well to shared references, but we'll examine the more common case -//! of mutable references for illustration). +//! functions should be. If `field` takes self: [Pin]<[&mut Struct][&mut]>, should it +//! return [`&mut Field`] or [Pin]<[`&mut Field`]>? This question also arises with +//! `enum`s and wrapper types like [`Vec`], [`Box`], and [`RefCell`]. (This question +//! applies just as well to shared references, but we'll examine the more common case of mutable +//! references for illustration). //! -//! It turns out that it's up to the author of `Struct` to decide which type the projection -//! should produce. The choice must be *consistent* though: each field should only ever -//! be projected as pinned or unpinned; both together will likely be unsound! +//! It turns out that it's up to the author of `Struct` to decide which type the "projection" +//! should produce. The choice must be *consistent* though: if a pin is projected to a field +//! in one place, then it should very likely not be exposed elsewhere without projecting the +//! pin. //! //! As the author of a data structure, you get to decide for each field whether pinning //! "propagates" to this field or not. Pinning that propagates is also called "structural", //! because it follows the structure of the type. //! -//! The choice of whether to pin depends on how the type is being used. If unsafe code +//! The choice of whether to pin depends on how the type is being used. If [`unsafe`] code //! that consumes [Pin]\<[&mut Struct][&mut]> also needs to take note of //! the address of the field itself, it may be evidence that that field is structurally //! pinned. Unfortunately, there are no hard-and-fast rules. //! //! ### Choosing pinning *not to be* structural for `field`... //! -//! While counter-intuitive, it's actually the easier choice: if a -//! [Pin]<[&mut] Field> is never created, nothing can go wrong (well, so long as no -//! unsound `unsafe` code is written which expects the invariants of such a [`Pin`] to be upheld -//! without actually using pinning to guarantee them)! So, if you decide that some field does not +//! While counter-intuitive, it's actually the easier choice: if you do not expose a +//! [Pin]<[&mut] Field>, then no code must be written assuming that the field is +//! pinned and so nothing can go wrong. So, if you decide that some field does not //! have structural pinning, all you have to ensure is that you never create pinning -//! reference to that field. +//! reference to that field. This does of course also mean that if you decide a field does not +//! have structural pinning, you must not write [`unsafe`] code that assumes (invalidly) that the +//! field *is* structurally pinned! //! //! Fields without structural pinning may have a projection method that turns //! [Pin]<[&mut] Struct> into [`&mut Field`]: @@ -749,15 +754,19 @@ //! # struct Struct { field: Field } //! impl Struct { //! fn field(self: Pin<&mut Self>) -> &mut Field { -//! // This is okay because `field` is never considered pinned. +//! // This is okay because `field` is never considered pinned, therefore we do not +//! // need to uphold any pinning guarantees for this field in particular. Of course, +//! // we must not elsewhere assume this field *is* pinned if we choose to expose +//! // such a method! //! unsafe { &mut self.get_unchecked_mut().field } //! } //! } //! ``` //! -//! You may also impl [Unpin] for Struct {} *even if* the type of `field` -//! is not [`Unpin`]. What that type thinks about pinning is not relevant -//! when no [Pin]<[&mut] Field> is ever created. +//! You may also in this situation impl [Unpin] for Struct {} *even if* the type of +//! `field` is not [`Unpin`]. Since we have explicitly chosen not to care about pinning guarantees +//! for `field`, the way `field`'s type interacts with pinning is no longer relevant in the +//! context of its use in `Struct`. //! //! ### Choosing pinning *to be* structural for `field`... //! From 6e882790b89080e337fa8ff8541023f3b1984c0f Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 09:29:02 +0200 Subject: [PATCH 20/40] `Pin

` -> `Pin` --- library/core/src/pin.rs | 156 +++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 75 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index bb315949d8bcc..70c6ddc2a9887 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -273,7 +273,7 @@ //! order to identify the type of the pinned pointee data and provide (restricted) access to it. //! //! A [`Pin`] where [`Ptr: Deref`][Deref] is a "`Ptr`-style pinning pointer" to a pinned -//! [`P::Target`][Target] – so, a [Pin]<[Box]\> is an owned, pinning pointer to a +//! [`Ptr::Target`][Target] – so, a [Pin]<[Box]\> is an owned, pinning pointer to a //! pinned `T`, and a [Pin]<[Rc]\> is a reference-counted, pinning pointer to a //! pinned `T`. //! @@ -590,7 +590,7 @@ //! # Implementing an address-sensitive type. //! //! This section goes into detail on important considerations for implementing your own -//! address-sensitive types, which are different from merely using [`Pin

`] in a generic +//! address-sensitive types, which are different from merely using [`Pin`] in a generic //! way. //! //! ## Implementing [`Drop`] for types with address-sensitive states @@ -689,7 +689,7 @@ //! Even though we can't have the compiler do the assignment for us, it's possible to write //! such specialized functions for types that might need it. //! -//! Note that it _is_ possible to assign generically through a [`Pin

`] by way of [`Pin::set()`]. +//! Note that it _is_ possible to assign generically through a [`Pin`] by way of [`Pin::set()`]. //! This does not violate any guarantees, since it will run [`drop`] on the pointee value before //! assigning the new value. Thus, the [`drop`] implementation still has a chance to perform the //! necessary notifications to dependent values before the memory location of the original pinned @@ -1050,7 +1050,7 @@ use crate::{ #[fundamental] #[repr(transparent)] #[derive(Copy, Clone)] -pub struct Pin

{ +pub struct Pin { // FIXME(#93176): this field is made `#[unstable] #[doc(hidden)] pub` to: // - deter downstream users from accessing it (which would be unsound!), // - let the `pin!` macro access it (such a macro requires using struct @@ -1058,7 +1058,7 @@ pub struct Pin

{ // Long-term, `unsafe` fields or macro hygiene are expected to offer more robust alternatives. #[unstable(feature = "unsafe_pin_internals", issue = "none")] #[doc(hidden)] - pub pointer: P, + pub pointer: Ptr, } // The following implementations aren't derived in order to avoid soundness @@ -1068,68 +1068,68 @@ pub struct Pin

{ // See for more details. #[stable(feature = "pin_trait_impls", since = "1.41.0")] -impl PartialEq> for Pin

+impl PartialEq> for Pin where - P::Target: PartialEq, + Ptr::Target: PartialEq, { fn eq(&self, other: &Pin) -> bool { - P::Target::eq(self, other) + Ptr::Target::eq(self, other) } fn ne(&self, other: &Pin) -> bool { - P::Target::ne(self, other) + Ptr::Target::ne(self, other) } } #[stable(feature = "pin_trait_impls", since = "1.41.0")] -impl> Eq for Pin

{} +impl> Eq for Pin {} #[stable(feature = "pin_trait_impls", since = "1.41.0")] -impl PartialOrd> for Pin

+impl PartialOrd> for Pin where - P::Target: PartialOrd, + Ptr::Target: PartialOrd, { fn partial_cmp(&self, other: &Pin) -> Option { - P::Target::partial_cmp(self, other) + Ptr::Target::partial_cmp(self, other) } fn lt(&self, other: &Pin) -> bool { - P::Target::lt(self, other) + Ptr::Target::lt(self, other) } fn le(&self, other: &Pin) -> bool { - P::Target::le(self, other) + Ptr::Target::le(self, other) } fn gt(&self, other: &Pin) -> bool { - P::Target::gt(self, other) + Ptr::Target::gt(self, other) } fn ge(&self, other: &Pin) -> bool { - P::Target::ge(self, other) + Ptr::Target::ge(self, other) } } #[stable(feature = "pin_trait_impls", since = "1.41.0")] -impl> Ord for Pin

{ +impl> Ord for Pin { fn cmp(&self, other: &Self) -> cmp::Ordering { - P::Target::cmp(self, other) + Ptr::Target::cmp(self, other) } } #[stable(feature = "pin_trait_impls", since = "1.41.0")] -impl> Hash for Pin

{ +impl> Hash for Pin { fn hash(&self, state: &mut H) { - P::Target::hash(self, state); + Ptr::Target::hash(self, state); } } -impl> Pin

{ - /// Construct a new `Pin

` around a pointer to some data of a type that +impl> Pin { + /// Construct a new `Pin` around a pointer to some data of a type that /// implements [`Unpin`]. /// /// Unlike `Pin::new_unchecked`, this method is safe because the pointer - /// `P` dereferences to an [`Unpin`] type, which cancels the pinning guarantees. + /// `Ptr` dereferences to an [`Unpin`] type, which cancels the pinning guarantees. /// /// # Examples /// @@ -1143,16 +1143,16 @@ impl> Pin

{ #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin", since = "1.33.0")] - pub const fn new(pointer: P) -> Pin

{ + pub const fn new(pointer: Ptr) -> Pin { // SAFETY: the value pointed to is `Unpin`, and so has no requirements // around pinning. unsafe { Pin::new_unchecked(pointer) } } - /// Unwraps this `Pin

` returning the underlying pointer. + /// Unwraps this `Pin`, returning the underlying pointer. /// - /// This requires that the data inside this `Pin` implements [`Unpin`] so that we - /// can ignore the pinning invariants when unwrapping it. + /// Doing this operation safely requires that the data pointed at by this pinning pointer + /// implemts [`Unpin`] so that we can ignore the pinning invariants when unwrapping it. /// /// # Examples /// @@ -1168,13 +1168,13 @@ impl> Pin

{ #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin_into_inner", since = "1.39.0")] - pub const fn into_inner(pin: Pin

) -> P { + pub const fn into_inner(pin: Pin) -> Ptr { pin.pointer } } -impl Pin

{ - /// Construct a new `Pin

` around a reference to some data of a type that +impl Pin { + /// Construct a new `Pin` around a reference to some data of a type that /// may or may not implement `Unpin`. /// /// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used @@ -1184,18 +1184,18 @@ impl Pin

{ /// /// This constructor is unsafe because we cannot guarantee that the data /// pointed to by `pointer` is pinned, meaning that the data will not be moved or - /// its storage invalidated until it gets dropped. If the constructed `Pin

` does - /// not guarantee that the data `P` points to is pinned, that is a violation of + /// its storage invalidated until it gets dropped. If the constructed `Pin` does + /// not guarantee that the data `Ptr` points to is pinned, that is a violation of /// the API contract and may lead to undefined behavior in later (safe) operations. /// - /// By using this method, you are making a promise about the `P::Deref` and - /// `P::DerefMut` implementations, if they exist. Most importantly, they + /// By using this method, you are making a promise about the `Ptr::Deref` and + /// `Ptr::DerefMut` implementations, if they exist. Most importantly, they /// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref` - /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type P* + /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type `Ptr`* /// and expect these methods to uphold the pinning invariants. - /// Moreover, by calling this method you promise that the reference `P` + /// Moreover, by calling this method you promise that the reference `Ptr` /// dereferences to will not be moved out of again; in particular, it - /// must not be possible to obtain a `&mut P::Target` and then + /// must not be possible to obtain a `&mut Ptr::Target` and then /// move out of that reference (using, for example [`mem::swap`]). /// /// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because @@ -1299,7 +1299,7 @@ impl Pin

{ #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin", since = "1.33.0")] - pub const unsafe fn new_unchecked(pointer: P) -> Pin

{ + pub const unsafe fn new_unchecked(pointer: Ptr) -> Pin { Pin { pointer } } @@ -1312,34 +1312,39 @@ impl Pin

{ /// ruled out by the contract of `Pin::new_unchecked`. #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] - pub fn as_ref(&self) -> Pin<&P::Target> { + pub fn as_ref(&self) -> Pin<&Ptr::Target> { // SAFETY: see documentation on this function unsafe { Pin::new_unchecked(&*self.pointer) } } - /// Unwraps this `Pin

` returning the underlying pointer. + /// Unwraps this `Pin` returning the underlying pointer. /// /// # Safety /// /// This function is unsafe. You must guarantee that you will continue to - /// treat the pointer `P` as pinned after you call this function, so that + /// treat the pointer `Ptr` as pinned after you call this function, so that /// the invariants on the `Pin` type can be upheld. If the code using the - /// resulting `P` does not continue to maintain the pinning invariants that + /// resulting `Ptr` does not continue to maintain the pinning invariants that /// is a violation of the API contract and may lead to undefined behavior in /// later (safe) operations. /// + /// Note that you must be able to guarantee that the data pointed to by `Ptr` + /// will be treated as pinned all the way until its `drop` handler is complete! + /// + /// *For more information, see the [`pin` module docs][self]* + /// /// If the underlying data is [`Unpin`], [`Pin::into_inner`] should be used /// instead. #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] #[stable(feature = "pin_into_inner", since = "1.39.0")] - pub const unsafe fn into_inner_unchecked(pin: Pin

) -> P { + pub const unsafe fn into_inner_unchecked(pin: Pin) -> Ptr { pin.pointer } } -impl Pin

{ - /// Gets a mutable reference to the pinned value this `Pin

` points to. +impl Pin { + /// Gets a mutable reference to the pinned value this `Pin` points to. /// /// This is a generic method to go from `&mut Pin>` to `Pin<&mut T>`. /// It is safe because, as part of the contract of `Pin::new_unchecked`, @@ -1370,12 +1375,12 @@ impl Pin

{ /// ``` #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] - pub fn as_mut(&mut self) -> Pin<&mut P::Target> { + pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> { // SAFETY: see documentation on this function unsafe { Pin::new_unchecked(&mut *self.pointer) } } - /// Assigns a new value to the memory location pointed to by the `Pin

`. + /// Assigns a new value to the memory location pointed to by the `Pin`. /// /// This overwrites pinned data, but that is okay: the original pinned value's destructor gets /// run before being overwritten and the new value is also a valid value of the same type, so @@ -1397,9 +1402,9 @@ impl Pin

{ /// [subtle-details]: self#subtle-details-and-the-drop-guarantee #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] - pub fn set(&mut self, value: P::Target) + pub fn set(&mut self, value: Ptr::Target) where - P::Target: Sized, + Ptr::Target: Sized, { *(self.pointer) = value; } @@ -1555,41 +1560,42 @@ impl Pin<&'static T> { } } -impl<'a, P: DerefMut> Pin<&'a mut Pin

> { +impl<'a, Ptr: DerefMut> Pin<&'a mut Pin> { /// Gets `Pin<&mut T>` to the underlying pinned value from this nested `Pin`-pointer. /// /// This is a generic method to go from `Pin<&mut Pin>>` to `Pin<&mut T>`. It is /// safe because the existence of a `Pin>` ensures that the pointee, `T`, cannot /// move in the future, and this method does not enable the pointee to move. "Malicious" - /// implementations of `P::DerefMut` are likewise ruled out by the contract of + /// implementations of `Ptr::DerefMut` are likewise ruled out by the contract of /// `Pin::new_unchecked`. #[unstable(feature = "pin_deref_mut", issue = "86918")] #[must_use = "`self` will be dropped if the result is not used"] #[inline(always)] - pub fn as_deref_mut(self) -> Pin<&'a mut P::Target> { + pub fn as_deref_mut(self) -> Pin<&'a mut Ptr::Target> { // SAFETY: What we're asserting here is that going from // - // Pin<&mut Pin

> + // Pin<&mut Pin> // // to // - // Pin<&mut P::Target> + // Pin<&mut Ptr::Target> // // is safe. // // We need to ensure that two things hold for that to be the case: // - // 1) Once we give out a `Pin<&mut P::Target>`, an `&mut P::Target` will not be given out. - // 2) By giving out a `Pin<&mut P::Target>`, we do not risk of violating `Pin<&mut Pin

>` + // 1) Once we give out a `Pin<&mut Ptr::Target>`, an `&mut Ptr::Target` will not be given out. + // 2) By giving out a `Pin<&mut Ptr::Target>`, we do not risk of violating + // `Pin<&mut Pin>` // - // The existence of `Pin

` is sufficient to guarantee #1: since we already have a - // `Pin

`, it must already uphold the pinning guarantees, which must mean that - // `Pin<&mut P::Target>` does as well, since `Pin::as_mut` is safe. We do not have to rely - // on the fact that P is _also_ pinned. + // The existence of `Pin` is sufficient to guarantee #1: since we already have a + // `Pin`, it must already uphold the pinning guarantees, which must mean that + // `Pin<&mut Ptr::Target>` does as well, since `Pin::as_mut` is safe. We do not have to rely + // on the fact that `Ptr` is _also_ pinned. // - // For #2, we need to ensure that code given a `Pin<&mut P::Target>` cannot cause the - // `Pin

` to move? That is not possible, since `Pin<&mut P::Target>` no longer retains - // any access to the `P` itself, much less the `Pin

`. + // For #2, we need to ensure that code given a `Pin<&mut Ptr::Target>` cannot cause the + // `Pin` to move? That is not possible, since `Pin<&mut Ptr::Target>` no longer retains + // any access to the `Ptr` itself, much less the `Pin`. unsafe { self.get_unchecked_mut() }.as_mut() } } @@ -1609,39 +1615,39 @@ impl Pin<&'static mut T> { } #[stable(feature = "pin", since = "1.33.0")] -impl Deref for Pin

{ - type Target = P::Target; - fn deref(&self) -> &P::Target { +impl Deref for Pin { + type Target = Ptr::Target; + fn deref(&self) -> &Ptr::Target { Pin::get_ref(Pin::as_ref(self)) } } #[stable(feature = "pin", since = "1.33.0")] -impl> DerefMut for Pin

{ - fn deref_mut(&mut self) -> &mut P::Target { +impl> DerefMut for Pin { + fn deref_mut(&mut self) -> &mut Ptr::Target { Pin::get_mut(Pin::as_mut(self)) } } #[unstable(feature = "receiver_trait", issue = "none")] -impl Receiver for Pin

{} +impl Receiver for Pin {} #[stable(feature = "pin", since = "1.33.0")] -impl fmt::Debug for Pin

{ +impl fmt::Debug for Pin { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.pointer, f) } } #[stable(feature = "pin", since = "1.33.0")] -impl fmt::Display for Pin

{ +impl fmt::Display for Pin { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.pointer, f) } } #[stable(feature = "pin", since = "1.33.0")] -impl fmt::Pointer for Pin

{ +impl fmt::Pointer for Pin { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Pointer::fmt(&self.pointer, f) } @@ -1653,10 +1659,10 @@ impl fmt::Pointer for Pin

{ // for other reasons, though, so we just need to take care not to allow such // impls to land in std. #[stable(feature = "pin", since = "1.33.0")] -impl CoerceUnsized> for Pin

where P: CoerceUnsized {} +impl CoerceUnsized> for Pin where Ptr: CoerceUnsized {} #[stable(feature = "pin", since = "1.33.0")] -impl DispatchFromDyn> for Pin

where P: DispatchFromDyn {} +impl DispatchFromDyn> for Pin where Ptr: DispatchFromDyn {} /// Constructs a [Pin]<[&mut] T>, by pinning a `value: T` locally. /// From 469c78bcfda4e00cfcdad2bae4793395af444ec2 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 10:36:11 +0200 Subject: [PATCH 21/40] improve `Pin` and `Pin::new` docs --- library/core/src/pin.rs | 90 ++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 70c6ddc2a9887..17ab48dfd9afd 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -945,8 +945,13 @@ use crate::{ /// /// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a /// [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** -/// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is impossible -/// to create or misuse a [`Pin`] which can violate this promise without using [`unsafe`]. +/// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is +/// impossible to create or misuse a [`Pin`] to violate this promise without using [`unsafe`]. +/// If the pointee value's type implements [`Unpin`], we are free to disregard these requirements +/// entirely and can wrap any pointer to that value in [`Pin`] directly via [`Pin::new`]. +/// If the pointee value's type does not implement [`Unpin`], then Rust will not let us use the +/// [`Pin::new`] function directly and we'll need to construct a [`Pin`]-wrapped pointer in one of +/// the more specialized manners discussed below. /// /// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning /// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in @@ -956,25 +961,57 @@ use crate::{ /// itself, but rather a pointer to that value! A [`Pin`] does not pin the `Ptr` but rather /// the pointer's ***pointee** value*. /// -/// For the vast majoriy of Rust types, pinning a value of that type will actually have no effect. -/// This is because the vast majority of types implement the [`Unpin`] trait, which entirely opts -/// all values of that type out of pinning-related guarantees. The most common exception -/// to this is the compiler-generated types that implement [`Future`] for the return value -/// of `async fn`s. These compiler-generated [`Future`]s do not implement [`Unpin`] for reasons -/// explained more in the [`pin` module] docs, but suffice it to say they require the guarantees -/// provided by pinning to be implemented soundly. +/// The most common set of types which require pinning related guarantees for soundness are the +/// state machines that implement [`Future`] for the return value of `async fn`s under the +/// hood. These compiler-generated [`Future`]s may contain self-referrential pointers, one of the +/// most common use cases for [`Pin`]. More details on this point are provided in the +/// [`pin` module] docs, but suffice it to say they require the guarantees provided by pinning to +/// be implemented soundly. /// -/// This requirement in the implementation of `async fn`s means that the [`Future`] trait requires -/// any [`Future`] to be pinned in order to call [`poll`] on it. Therefore, when manually polling -/// a future, you will need to pin it first. +/// This requirement from the implementation of `async fn`s means that the [`Future`] trait +/// requires all calls to [`poll`] to use a self: [Pin]\<&mut Self> parameter instead +/// of the usual `&mut self`. Therefore, when manually polling a future, you will need to pin it +/// first. +/// +/// You may notice that `async fn`-generated [`Future`]s are only a small percentage of all +/// [`Future`]s that exist, yet we had to modify the signature of [`poll`] for all [`Future`]s +/// to accommodate them. This is unfortunate, but there is a way that the language attempts to +/// alleviate the extra friction that this API choice incurs: the [`Unpin`] trait. +/// +/// The vast majority of Rust types have no reason to ever care about being pinned. These +/// types implement the [`Unpin`] trait, which entirely opts all values of that type out of +/// pinning-related guarantees. For values of these types, pinning a value by pointing to it with a +/// [`Pin`] will have no actual effect. +/// +/// The reason this distinction exists is exactly to allow APIs like [`Future::poll`] to take a +/// [`Pin`] as an argument for all types while only forcing [`Future`] types that actually +/// care about pinning guarantees pay the ergonomics cost. For the majority of [`Future`] types +/// that don't have a reason to care about being pinned and therefore implement [`Unpin`], the +/// [Pin]\<&mut Self> will act exactly like a regular `&mut Self`, allowing direct +/// access to the underlying value. Only types that *don't* implement [`Unpin`] will be restricted. +/// +/// ### Pinning a value of a type that implements [`Unpin`] +/// +/// If the type of the value you need to "pin" implements [`Unpin`], you can trivially wrap any +/// pointer to that value in a [`Pin`] by calling [`Pin::new`]. +/// +/// ``` +/// use std::pin::Pin; +/// +/// // Create a value of a type that implements `Unpin` +/// let mut unpin_future = std::future::ready(5); +/// +/// // Pin it by creating a pinning mutable reference to it (ready to be `poll`ed!) +/// let my_pinned_unpin_future: Pin<&mut _> = Pin::new(&mut unpin_future); +/// ``` /// /// ### Pinning a value inside a [`Box`] /// -/// The simplest and most flexible way to pin a value is to put that value inside a [`Box`] and -/// then turn that [`Box`] into a "pinning [`Box`]" by wrapping it in a [`Pin`]. -/// You can do both of these in a single step using [`Box::pin`]. Let's see an example of using -/// this flow to pin a [`Future`] returned from calling an `async fn`, a common use case -/// as described above. +/// The simplest and most flexible way to pin a value that does not implement [`Unpin`] is to put +/// that value inside a [`Box`] and then turn that [`Box`] into a "pinning [`Box`]" by wrapping it +/// in a [`Pin`]. You can do both of these in a single step using [`Box::pin`]. Let's see an +/// example of using this flow to pin a [`Future`] returned from calling an `async fn`, a common +/// use case as described above. /// /// ``` /// use std::pin::Pin; @@ -1018,8 +1055,8 @@ use crate::{ /// /// There are some situations where it is desirable or even required (for example, in a `#[no_std]` /// context where you don't have access to the standard library or allocation in general) to -/// pin a value to its location on the stack. Doing so is possible using the [`pin!`] macro. See -/// its documentation for more. +/// pin a value which does not implement [`Unpin`] to its location on the stack. Doing so is +/// possible using the [`pin!`] macro. See its documentation for more. /// /// ## Layout and ABI /// @@ -1032,6 +1069,7 @@ use crate::{ /// [`pin!`]: crate::pin::pin "pin!" /// [`Future`]: crate::future::Future "Future" /// [`poll`]: crate::future::Future::poll "Future::poll" +/// [`Future::poll`]: crate::future::Future::poll "Future::poll" /// [`pin` module]: self "pin module" /// [`Rc`]: ../../std/rc/struct.Rc.html "Rc" /// [`Arc`]: ../../std/sync/struct.Arc.html "Arc" @@ -1137,7 +1175,10 @@ impl> Pin { /// use std::pin::Pin; /// /// let mut val: u8 = 5; - /// // We can pin the value, since it doesn't care about being moved + /// + /// // Since `val` doesn't care about being moved, we can safely create a "facade" `Pin` + /// // which will allow `val` to participate in `Pin`-bound apis without checking that + /// // pinning guarantees are actually upheld. /// let mut pinned: Pin<&mut u8> = Pin::new(&mut val); /// ``` #[inline(always)] @@ -1161,7 +1202,10 @@ impl> Pin { /// /// let mut val: u8 = 5; /// let pinned: Pin<&mut u8> = Pin::new(&mut val); - /// // Unwrap the pin to get a reference to the value + /// + /// // Unwrap the pin to get the underlying mutable reference to the value. We can do + /// // this because `val` doesn't care about being moved, so the `Pin` was just + /// // a "facade" anyway. /// let r = Pin::into_inner(pinned); /// assert_eq!(*r, 5); /// ``` @@ -1317,7 +1361,7 @@ impl Pin { unsafe { Pin::new_unchecked(&*self.pointer) } } - /// Unwraps this `Pin` returning the underlying pointer. + /// Unwraps this `Pin`, returning the underlying `Ptr`. /// /// # Safety /// @@ -1330,7 +1374,7 @@ impl Pin { /// /// Note that you must be able to guarantee that the data pointed to by `Ptr` /// will be treated as pinned all the way until its `drop` handler is complete! - /// + /// /// *For more information, see the [`pin` module docs][self]* /// /// If the underlying data is [`Unpin`], [`Pin::into_inner`] should be used From e0210e6e1d18f748db7d84255b84539f67fd7dc0 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 11:42:46 +0200 Subject: [PATCH 22/40] justify motivation of `Unpin` better --- library/core/src/marker.rs | 9 +++++++++ library/core/src/pin.rs | 35 +++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 5b527dfc9e7e9..cf14b8dee3700 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -910,6 +910,13 @@ marker_impls! { /// to the pointee value like it normally would, thus allowing the user to do anything that they /// normally could with a non-[`Pin`]-wrapped `Ptr` to that value. /// +/// The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use +/// of [`Pin`] for soundness for some types, but which also want to be used by other types that +/// don't care about pinning. The prime example of such an API is [`Future::poll`]. There are many +/// [`Future`] types that don't care about pinning. These futures can implement `Unpin` and +/// therefore get around the pinning related restrictions in the API, while still allowing the +/// subset of [`Future`]s which *do* require pinning to be implemented soundly. +/// /// For more discussion on the consequences of [`Unpin`] within the wider scope of the pinning /// system, see [the section about `Unpin`] in the [`pin` module]. /// @@ -947,6 +954,8 @@ marker_impls! { /// by adding [`PhantomPinned`] field. For more details, see the [`pin` module] docs. /// /// [`mem::replace`]: crate::mem::replace "mem replace" +/// [`Future`]: crate::future::Future "Future" +/// [`Future::poll`]: crate::future::Future::poll "Future poll" /// [`Pin`]: crate::pin::Pin "Pin" /// [`Pin`]: crate::pin::Pin "Pin" /// [`pin` module]: crate::pin "pin module" diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 17ab48dfd9afd..5d05f4bf901b3 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -349,7 +349,15 @@ //! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of //! [`Pin`] when the *pointee* type `T` is [`Unpin`]. When [`T: Unpin`][Unpin], //! [Pin]<[Box]\> functions identically to a non-pinning [`Box`]; similarly, -//! [Pin]<[&mut] T> would impose no additional restrictions above a regular [`&mut T`]. +//! [Pin]<[&mut] T> would impose no additional restrictions above a regular +//! [`&mut T`]. +//! +//! The idea of this trait is to alleviate the reduced ergonomics of APIs that require the use +//! of [`Pin`] for soundness for some types, but which also want to be used by other types that +//! don't care about pinning. The prime example of such an API is [`Future::poll`]. There are many +//! [`Future`] types that don't care about pinning. These futures can implement [`Unpin`] and +//! therefore get around the pinning related restrictions in the API, while still allowing the +//! subset of [`Future`]s which *do* require pinning to be implemented soundly. //! //! Note that the interaction between a [`Pin`] and [`Unpin`] is through the type of the //! **pointee** value, [`::Target`][Target]. Whether the `Ptr` type itself @@ -945,15 +953,14 @@ use crate::{ /// /// In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a /// [`Pin`]. [`Pin`] can wrap any pointer type, forming a promise that the **pointee** -/// will not be *moved* or [otherwise invalidated][subtle-details]. Note that it is -/// impossible to create or misuse a [`Pin`] to violate this promise without using [`unsafe`]. -/// If the pointee value's type implements [`Unpin`], we are free to disregard these requirements -/// entirely and can wrap any pointer to that value in [`Pin`] directly via [`Pin::new`]. -/// If the pointee value's type does not implement [`Unpin`], then Rust will not let us use the -/// [`Pin::new`] function directly and we'll need to construct a [`Pin`]-wrapped pointer in one of -/// the more specialized manners discussed below. +/// will not be *moved* or [otherwise invalidated][subtle-details]. If the pointee value's type +/// implements [`Unpin`], we are free to disregard these requirements entirely and can wrap any +/// pointer to that value in [`Pin`] directly via [`Pin::new`]. If the pointee value's type does +/// not implement [`Unpin`], then Rust will not let us use the [`Pin::new`] function directly and +/// we'll need to construct a [`Pin`]-wrapped pointer in one of the more specialized manners +/// discussed below. /// -/// We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning ref, or pinning +/// We call such a [`Pin`]-wrapped pointer a **pinning pointer** (or pinning ref, or pinning /// [`Box`], etc.) because its existince is the thing that is pinning the underlying pointee in /// place: it is the metaphorical "pin" securing the data in place on the pinboard (in memory). /// @@ -962,18 +969,18 @@ use crate::{ /// the pointer's ***pointee** value*. /// /// The most common set of types which require pinning related guarantees for soundness are the -/// state machines that implement [`Future`] for the return value of `async fn`s under the -/// hood. These compiler-generated [`Future`]s may contain self-referrential pointers, one of the -/// most common use cases for [`Pin`]. More details on this point are provided in the +/// compiler-generated state machines that implement [`Future`] for the return value of +/// `async fn`s. These compiler-generated [`Future`]s may contain self-referrential pointers, one +/// of the most common use cases for [`Pin`]. More details on this point are provided in the /// [`pin` module] docs, but suffice it to say they require the guarantees provided by pinning to /// be implemented soundly. /// -/// This requirement from the implementation of `async fn`s means that the [`Future`] trait +/// This requirement for the implementation of `async fn`s means that the [`Future`] trait /// requires all calls to [`poll`] to use a self: [Pin]\<&mut Self> parameter instead /// of the usual `&mut self`. Therefore, when manually polling a future, you will need to pin it /// first. /// -/// You may notice that `async fn`-generated [`Future`]s are only a small percentage of all +/// You may notice that `async fn`-sourced [`Future`]s are only a small percentage of all /// [`Future`]s that exist, yet we had to modify the signature of [`poll`] for all [`Future`]s /// to accommodate them. This is unfortunate, but there is a way that the language attempts to /// alleviate the extra friction that this API choice incurs: the [`Unpin`] trait. From f0827b3055eb05e68c680a341043e94b2f7fa502 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 12:03:13 +0200 Subject: [PATCH 23/40] fix broken link --- library/core/src/marker.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index cf14b8dee3700..edca9003890d5 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -918,7 +918,7 @@ marker_impls! { /// subset of [`Future`]s which *do* require pinning to be implemented soundly. /// /// For more discussion on the consequences of [`Unpin`] within the wider scope of the pinning -/// system, see [the section about `Unpin`] in the [`pin` module]. +/// system, see the [section about `Unpin`] in the [`pin` module]. /// /// `Unpin` has no consequence at all for non-pinned data. In particular, [`mem::replace`] happily /// moves `!Unpin` data, which would be immovable when pinned ([`mem::replace`] works for any From d7a886a807a25d097404a6be355bca176632a146 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 13:01:27 +0200 Subject: [PATCH 24/40] update ui tests --- tests/ui/async-await/pin-needed-to-poll-2.stderr | 2 +- .../ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.stderr | 2 +- tests/ui/self/arbitrary_self_types_pin_needing_borrow.stderr | 2 +- tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ui/async-await/pin-needed-to-poll-2.stderr b/tests/ui/async-await/pin-needed-to-poll-2.stderr index 9c1ad32cc2ce9..8eb671531e792 100644 --- a/tests/ui/async-await/pin-needed-to-poll-2.stderr +++ b/tests/ui/async-await/pin-needed-to-poll-2.stderr @@ -13,7 +13,7 @@ note: required because it appears within the type `Sleep` | LL | struct Sleep(std::marker::PhantomPinned); | ^^^^^ -note: required by a bound in `Pin::

::new` +note: required by a bound in `Pin::::new` --> $SRC_DIR/core/src/pin.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.stderr b/tests/ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.stderr index f5cb3e2b5f807..48fc84618823e 100644 --- a/tests/ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.stderr +++ b/tests/ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.stderr @@ -1,4 +1,4 @@ -error[E0133]: call to unsafe function `Pin::

::new_unchecked` is unsafe and requires unsafe function or block +error[E0133]: call to unsafe function `Pin::::new_unchecked` is unsafe and requires unsafe function or block --> $DIR/coerce-unsafe-closure-to-unsafe-fn-ptr.rs:2:31 | LL | let _: unsafe fn() = || { ::std::pin::Pin::new_unchecked(&0_u8); }; diff --git a/tests/ui/self/arbitrary_self_types_pin_needing_borrow.stderr b/tests/ui/self/arbitrary_self_types_pin_needing_borrow.stderr index ec985b254b340..1811cd6753ffe 100644 --- a/tests/ui/self/arbitrary_self_types_pin_needing_borrow.stderr +++ b/tests/ui/self/arbitrary_self_types_pin_needing_borrow.stderr @@ -6,7 +6,7 @@ LL | Pin::new(S).x(); | | | required by a bound introduced by this call | -note: required by a bound in `Pin::

::new` +note: required by a bound in `Pin::::new` --> $SRC_DIR/core/src/pin.rs:LL:COL help: consider borrowing here | diff --git a/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr b/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr index 7c81825e57649..60ab392f55de8 100644 --- a/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr +++ b/tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr @@ -52,7 +52,7 @@ LL | Pin::new(x) | = note: consider using the `pin!` macro consider using `Box::pin` if you need to access the pinned value outside of the current scope -note: required by a bound in `Pin::

::new` +note: required by a bound in `Pin::::new` --> $SRC_DIR/core/src/pin.rs:LL:COL error[E0277]: `dyn Future + Send` cannot be unpinned @@ -65,7 +65,7 @@ LL | Pin::new(Box::new(x)) | = note: consider using the `pin!` macro consider using `Box::pin` if you need to access the pinned value outside of the current scope -note: required by a bound in `Pin::

::new` +note: required by a bound in `Pin::::new` --> $SRC_DIR/core/src/pin.rs:LL:COL error[E0308]: mismatched types From 9997114e14df45df370507887d737a3b38c63117 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 13:12:43 +0200 Subject: [PATCH 25/40] improve `Pin::new_unchecked` docs --- library/core/src/pin.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 5d05f4bf901b3..b0d777332f444 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1226,21 +1226,24 @@ impl> Pin { impl Pin { /// Construct a new `Pin` around a reference to some data of a type that - /// may or may not implement `Unpin`. + /// may or may not implement [`Unpin`]. /// - /// If `pointer` dereferences to an `Unpin` type, `Pin::new` should be used + /// If `pointer` dereferences to an [`Unpin`] type, [`Pin::new`] should be used /// instead. /// /// # Safety /// /// This constructor is unsafe because we cannot guarantee that the data - /// pointed to by `pointer` is pinned, meaning that the data will not be moved or - /// its storage invalidated until it gets dropped. If the constructed `Pin` does - /// not guarantee that the data `Ptr` points to is pinned, that is a violation of - /// the API contract and may lead to undefined behavior in later (safe) operations. + /// pointed to by `pointer` is pinned. At its core, pinning a value means making the + /// guarantee that the value's data will not be moved nor have its storage invalidated until + /// it gets dropped. For a more thorough explanation of pinning, see the [`pin` module docs]. /// - /// By using this method, you are making a promise about the `Ptr::Deref` and - /// `Ptr::DerefMut` implementations, if they exist. Most importantly, they + /// If the caller that is constructing this `Pin` does not ensure that the data `Ptr` + /// points to is pinned, that is a violation of the API contract and may lead to undefined + /// behavior in later (even safe) operations. + /// + /// By using this method, you are also making a promise about the [`Deref`] and + /// [`DerefMut`] implementations of `Ptr`, if they exist. Most importantly, they /// must not move out of their `self` arguments: `Pin::as_mut` and `Pin::as_ref` /// will call `DerefMut::deref_mut` and `Deref::deref` *on the pointer type `Ptr`* /// and expect these methods to uphold the pinning invariants. @@ -1251,7 +1254,9 @@ impl Pin { /// /// For example, calling `Pin::new_unchecked` on an `&'a mut T` is unsafe because /// while you are able to pin it for the given lifetime `'a`, you have no control - /// over whether it is kept pinned once `'a` ends: + /// over whether it is kept pinned once `'a` ends, and therefore cannot uphold the + /// guarantee that a value, once pinned, remains pinned until it is dropped: + /// /// ``` /// use std::mem; /// use std::pin::Pin; @@ -1285,7 +1290,7 @@ impl Pin { /// // ... /// } /// drop(pin); - + /// /// let content = Rc::get_mut(&mut x).unwrap(); // Potential UB down the road ⚠️ /// // Now, if `x` was the only reference, we have a mutable reference to /// // data that we pinned above, which we could use to move it as we have @@ -1346,6 +1351,7 @@ impl Pin { /// ``` /// /// [`mem::swap`]: crate::mem::swap + /// [`pin` module docs]: self #[lang = "new_unchecked"] #[inline(always)] #[rustc_const_unstable(feature = "const_pin", issue = "76654")] From 058fb50ecdb19cb36b009285e811b87fd7fe254d Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 3 Oct 2023 13:17:26 +0200 Subject: [PATCH 26/40] trim section on managed-box model --- library/core/src/pin.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index b0d777332f444..e106433b44e76 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -183,14 +183,8 @@ //! access to that data to ensure no *moves* or other invalidation occurs, and finally //! provide a safe interface on top. //! -//! There are a couple of linked disadvantages to using this model. The core issue is a lack -//! of generality. This is an issue because it means that each individual type that implements -//! such an interface does so on its own. Each developer implementing such a type must themselves -//! think through all the guarantees needed to ensure the API they present is sound. We would -//! rather build a shared understanding of the problem space and encode that understanding into a -//! shared interface to solve it which everyone helps validate. -//! -//! In addition, in this model, each individual object must assume it is *on its own* to ensure +//! There are a couple of linked disadvantages to using this model. The most significant is that +//! each individual object must assume it is *on its own* to ensure //! that its data does not become *moved* or otherwise invalidated. Since there is no shared //! contract between values of different types, an object cannot assume that others interacting //! with it will properly respect the invariants around interacting with its data and must @@ -198,7 +192,8 @@ //! requires at least a level of pointer indirection each time a new object is added to the mix //! (and, practically, a heap allocation). //! -//! This is the key thing that drove Rust towards a different model. It is particularly a problem +//! Although there were other reason as well, this issue of expensive composition is the key thing +//! that drove Rust towards adopting a different model. It is particularly a problem //! when one considers, for exapmle, the implications of composing together the [`Future`]s which //! will eventaully make up an asynchronous task (including address-sensitive `async fn` state //! machines). It is plausible that there could be many layers of [`Future`]s composed together, From 00506764408132e80658b1312fdd2b72e919119d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:10:03 -0800 Subject: [PATCH 27/40] Rephrase unpin docs in terms of pinning-agnosticness --- library/core/src/marker.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index edca9003890d5..561f8ef36ffde 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -899,13 +899,15 @@ marker_impls! { {T: ?Sized} &mut T, } -/// Types that do not need to follow the rules of pinning. +/// Types that do not require any pinning guarantees. /// /// For information on what "pinning" is, see the [`pin` module] documentation. /// -/// Implementing the `Unpin` trait for `T` lifts the restrictions of pinning off that type. -/// This means that, if `T: Unpin`, it cannot be assumed that a value of type `T` will be bound -/// by the invariants that pinning infers, *even* when "pinned" by a [`Pin`] pointing at it. +/// Implementing the `Unpin` trait for `T` expresses the fact that `T` is pinning-agnostic: +/// it shall not expose nor rely on any pinning guarantees. This, in turn, means that a +/// `Pin`-wrapped pointer to such a type can feature a *fully unrestricted* API. +/// In other words, if `T: Unpin`, a value of type `T` will *not* be bound by the invariants +/// which pinning otherwise offers, even when "pinned" by a [`Pin`] pointing at it. /// When a value of type `T` is pointed at by a [`Pin`], [`Pin`] will not restrict access /// to the pointee value like it normally would, thus allowing the user to do anything that they /// normally could with a non-[`Pin`]-wrapped `Ptr` to that value. From 936ceb20f57ebfa4ea2f583ae1d0be488b404895 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:15:22 -0800 Subject: [PATCH 28/40] lifetime -> lifespan where relevant. improve docs on as_ref() --- library/core/src/pin.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index e106433b44e76..c47f097b8a31e 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -9,12 +9,12 @@ //! 2. More generally, remain *valid* at that same memory location //! //! is called "pinning." We would say that a value which satisfies these guarantees has been -//! "pinned," in that it has been permanently (until the end of its lifetime) attached to its +//! "pinned," in that it has been permanently (until the end of its lifespan) attached to its //! location in memory, as though pinned to a pinboard. Pinning a value is incredibly useful in //! that it provides the necessary guarantees[^guarantees] for [`unsafe`] code to be able to //! dereference raw pointers to the pinned value for the duration it is pinned (which, //! [as we'll see later][drop-guarantee], is necessarily from the time the value is first pinned -//! until the end of its lifetime). This concept of "pinning" is necessary to implement safe +//! until the end of its lifespan). This concept of "pinning" is necessary to implement safe //! interfaces on top of things like self-referential types and intrusive data structures which //! cannot currently be modeled in fully safe Rust using only borrow-checked //! [references][reference]. @@ -126,7 +126,7 @@ //! [`Pin`] is specifically targeted at allowing the implementation of *safe interfaces* around //! types which have some state during which they become "address-sensitive." A value in such an //! "address-sensitive" state is *not* okay with being *moved* around at-will. Such a value must -//! stay *un-moved* and valid during the address-sensitive portion of its lifetime because some +//! stay *un-moved* and valid during the address-sensitive portion of its lifespan because some //! interface is relying on those invariants to be true in order for its implementation to be sound. //! //! As a motivating example of a type which may become address-sensitive, consider a type which @@ -535,7 +535,7 @@ //! but it also implies that, //! //! 2. The memory location that stores the value must not get invalidated or otherwise repurposed -//! during the lifetime of the pinned value until its [`drop`] returns or panics +//! during the lifespan of the pinned value until its [`drop`] returns or panics //! //! This point is subtle but required for intrusive data structures to be implemented soundly. //! @@ -1505,8 +1505,8 @@ impl<'a, T: ?Sized> Pin<&'a T> { /// Note: `Pin` also implements `Deref` to the target, which can be used /// to access the inner value. However, `Deref` only provides a reference /// that lives for as long as the borrow of the `Pin`, not the lifetime of - /// the `Pin` itself. This method allows turning the `Pin` into a reference - /// with the same lifetime as the original `Pin`. + /// the reference contained in the `Pin`. This method allows turning the `Pin` into a reference + /// with the same lifetime as the reference it wraps. /// /// ["pinning projections"]: self#projections-and-structural-pinning #[inline(always)] From 4c25246f3e94002795e7652feac6ef6fa0415de2 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:24:03 -0800 Subject: [PATCH 29/40] Clean up guarantees wording We don't need to go into that much depth at this stage --- library/core/src/pin.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index c47f097b8a31e..3926a661dce61 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -10,14 +10,13 @@ //! //! is called "pinning." We would say that a value which satisfies these guarantees has been //! "pinned," in that it has been permanently (until the end of its lifespan) attached to its -//! location in memory, as though pinned to a pinboard. Pinning a value is incredibly useful in -//! that it provides the necessary guarantees[^guarantees] for [`unsafe`] code to be able to -//! dereference raw pointers to the pinned value for the duration it is pinned (which, -//! [as we'll see later][drop-guarantee], is necessarily from the time the value is first pinned -//! until the end of its lifespan). This concept of "pinning" is necessary to implement safe -//! interfaces on top of things like self-referential types and intrusive data structures which -//! cannot currently be modeled in fully safe Rust using only borrow-checked -//! [references][reference]. +//! location in memory, as though pinned to a pinboard. Pinning a value is an incredibly useful +//! building block for [unsafe] code to be able to reason about whether a raw pointer to the +//! pinned value is still valid. [As we'll see later][drop-guarantee], this is necessarily from the +//! time the value is first pinned until the end of its lifespan. This concept of "pinning" is +//! necessary to implement safe interfaces on top of things like self-referential types and +//! intrusive data structures which cannot currently be modeled in fully safe Rust using only +//! borrow-checked [references][reference]. //! //! "Pinning" allows us to put a *value* which exists at some location in memory into a state where //! safe code cannot *move* that value to a different location in memory or otherwise invalidate it @@ -28,13 +27,6 @@ //! and not the compiler. In this way, we can allow other [`unsafe`] code to rely on any pointers //! that point to the pinned value to be valid to dereference while it is pinned. //! -//! [^guarantees]: Pinning on its own does not provide *all* the invariants necessary here. However, -//! in order to validly pin a value in the first place, it must already satisfy the other invariants -//! for it to be valid to dereference a pointer to that value while it is pinned, and using the -//! [`Drop` guarantee][self#subtle-details-and-the-drop-guarantee], we can ensure that any -//! interested parties are notified before the value becomes no longer pinned, i.e. when the value -//! goes out of scope and is invalidated. -//! //! Note that as long as you don't use [`unsafe`], it's impossible to create or misuse a pinned //! value in a way that is unsound. See the documentation of [`Pin`] for more //! information on the practicalities of how to pin a value and how to use that pinned value from a From 68bdeddb5cc6ec3ee787a94d7373928048e5b81b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Jan 2024 00:27:01 +0000 Subject: [PATCH 30/40] Apply suggestions from code review Co-authored-by: Ralf Jung Co-authored-by: Daniel Henry-Mantilla --- library/core/src/pin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 3926a661dce61..a8ef5949e7a89 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -146,10 +146,10 @@ //! There are two possible ways to ensure the invariants required for 2. and 3. above (which //! apply to any address-sensitive type, not just self-referrential types) do not get broken. //! -//! 1. Have the value detect when it is moved and update all the pointers that point to itself +//! 1. Have the value detect when it is moved and update all the pointers that point to itself. //! 2. Guarantee that the address of the value does not change (and that memory is not re-used //! for anything else) during the time that the pointers to it are expected to be valid to -//! dereference +//! dereference. //! //! Since, as we discussed, Rust can move values without notifying them that they have moved, the //! first option is ruled out. @@ -203,7 +203,7 @@ //! will not be *moved* or [otherwise invalidated][subtle-details]. //! //! We call such a [`Pin`]-wrapped pointer a **pinning pointer,** (or pinning reference, or pinning -//! `Box`, etc.) because its existince is the thing that is *symbolically* pinning the underlying +//! `Box`, etc.) because its existence is the thing that is conceptually pinning the underlying //! pointee in place: it is the metaphorical "pin" securing the data in place on the pinboard //! (in memory). //! @@ -332,7 +332,7 @@ //! //! ## [`Unpin`] //! -//! The vast majority of Rust types have no address-sensitive states; these types +//! The vast majority of Rust types have no address-sensitive states. These types //! implement the [`Unpin`] auto-trait, which cancels the restrictive effects of //! [`Pin`] when the *pointee* type `T` is [`Unpin`]. When [`T: Unpin`][Unpin], //! [Pin]<[Box]\> functions identically to a non-pinning [`Box`]; similarly, From 6553d0d55119512369f3636c93ae86c2e609bd7b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:28:07 -0800 Subject: [PATCH 31/40] punctuation in parens --- library/core/src/pin.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index a8ef5949e7a89..600208132ba8c 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -714,7 +714,7 @@ //! return [`&mut Field`] or [Pin]<[`&mut Field`]>? This question also arises with //! `enum`s and wrapper types like [`Vec`], [`Box`], and [`RefCell`]. (This question //! applies just as well to shared references, but we'll examine the more common case of mutable -//! references for illustration). +//! references for illustration) //! //! It turns out that it's up to the author of `Struct` to decide which type the "projection" //! should produce. The choice must be *consistent* though: if a pin is projected to a field @@ -792,7 +792,7 @@ //! structural pinning to an inner field of `T`, which may not be [`Unpin`]! (Adding *any* //! projection operation requires unsafe code, so the fact that [`Unpin`] is a safe trait does //! not break the principle that you only have to worry about any of this if you use -//! [`unsafe`].) +//! [`unsafe`]) //! //! 2. *Pinned Destruction.* As discussed [above][drop-impl], [`drop`] takes //! [`&mut self`], but the struct (and hence its fields) might have been pinned From 6a54ed71c04c1c79fa3060040742f44d5b04ea16 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:33:07 -0800 Subject: [PATCH 32/40] valid --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 600208132ba8c..18113c6def86c 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -105,7 +105,7 @@ //! [what-is-pinning]: self#what-is-pinning //! //! We say that a value has been *pinned* when it has been put into a state where it is guaranteed -//! to remain *valid* and *located at the same place in memory* from the time it is pinned until its +//! to remain *located at the same place in memory* from the time it is pinned until its //! [`drop`] is called. //! //! ## Address-sensitive values, AKA "when we need pinning" From a573c7c409ca335aaef1f54bbff0c5816e38cad6 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:37:58 -0800 Subject: [PATCH 33/40] footnote on dropping futures --- library/core/src/pin.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 18113c6def86c..3af0234330765 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -126,7 +126,7 @@ //! for such a type to be implemented soundly, the pointer which points into `self`'s data must be //! proven valid whenever it is accessed. But if that value is *moved*, the pointer will still //! point to the old address where the value was located and not into the new location of `self`, -//! thus becoming invalid. A key example of such self-referrential types are the state machines +//! thus becoming invalid. A key example of such self-referential types are the state machines //! generated by the compiler to implement [`Future`] for `async fn`s. //! //! Such types that have an *address-sensitive* state usually follow a lifecycle @@ -141,7 +141,7 @@ //! * e.g. subsequent calls to [`poll`] //! 4. Before the value is invalidated (e.g. deallocated), it is *dropped*, giving it a chance to //! notify anything with pointers to itself that those pointers will be invalidated -//! * e.g. [`drop`]ping the [`Future`] +//! * e.g. [`drop`]ping the [`Future`] [^pin-drop-future] //! //! There are two possible ways to ensure the invariants required for 2. and 3. above (which //! apply to any address-sensitive type, not just self-referrential types) do not get broken. @@ -196,6 +196,9 @@ //! discussed with the second option by building a *shared contractual language* around the //! guarantees of "pinning" data. //! +//! [^pin-drop-future]: Futures themselves do not ever need to notify other bits of code that +//! they are being dropped, however data structures like stack-based intrusive linked lists do. +//! //! ## Using [`Pin`] to pin values //! //! In order to pin a value, we wrap a *pointer to that value* (of some type `Ptr`) in a From b1830f130a0ccd06cd43ac5f2c09ed669e5b1d3b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 6 Jan 2024 16:54:01 -0800 Subject: [PATCH 34/40] clean up structural pinning --- library/core/src/pin.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 3af0234330765..15f5d7ce19c79 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -728,17 +728,19 @@ //! "propagates" to this field or not. Pinning that propagates is also called "structural", //! because it follows the structure of the type. //! -//! The choice of whether to pin depends on how the type is being used. If [`unsafe`] code -//! that consumes [Pin]\<[&mut Struct][&mut]> also needs to take note of -//! the address of the field itself, it may be evidence that that field is structurally -//! pinned. Unfortunately, there are no hard-and-fast rules. +//! This choice depends on what guarantees you need from the field for your [`unsafe`] code to work. +//! If the field is itself address-sensitive, or participates in the parent struct's address +//! sensitivity, it will need to be structurally pinned. +//! +//! A useful test is if [`unsafe`] code that consumes [Pin]\<[&mut Struct][&mut]> +//! also needs to take note of the address of the field itself, it may be evidence that that field +//! is structurally pinned. Unfortunately, there are no hard-and-fast rules. //! //! ### Choosing pinning *not to be* structural for `field`... //! -//! While counter-intuitive, it's actually the easier choice: if you do not expose a -//! [Pin]<[&mut] Field>, then no code must be written assuming that the field is -//! pinned and so nothing can go wrong. So, if you decide that some field does not -//! have structural pinning, all you have to ensure is that you never create pinning +//! While counter-intuitive, it's often the easier choice: if you do not expose a +//! [Pin]<[&mut] Field>, you do not need to be careful about other code +//! moving out of that field, you just have to ensure is that you never create pinning //! reference to that field. This does of course also mean that if you decide a field does not //! have structural pinning, you must not write [`unsafe`] code that assumes (invalidly) that the //! field *is* structurally pinned! From df6d44961d6f05b7c51189dcce3b05c4fb9ced63 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Jan 2024 08:33:14 -0800 Subject: [PATCH 35/40] Update library/core/src/pin.rs Co-authored-by: Ralf Jung --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 15f5d7ce19c79..ef29e3f373ab4 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -790,7 +790,7 @@ //! //! Structural pinning comes with a few extra requirements: //! -//! 1. *Structural [`Unpin`].* A struct can be [`Unpin`] if, and only if, all of its +//! 1. *Structural [`Unpin`].* A struct can be [`Unpin`] only if all of its //! structurally-pinned fields are, too. This is [`Unpin`]'s behavior by default. //! However, as a libray author, it is your responsibility not to write something like //! impl\ [Unpin] for Struct\ {} and then offer a method that provides From 7fd841c098e58a69808610e4e89366c03c5621fc Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 7 Jan 2024 08:57:23 -0800 Subject: [PATCH 36/40] link --- library/core/src/pin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index ef29e3f373ab4..bb6c81a486a59 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -11,7 +11,7 @@ //! is called "pinning." We would say that a value which satisfies these guarantees has been //! "pinned," in that it has been permanently (until the end of its lifespan) attached to its //! location in memory, as though pinned to a pinboard. Pinning a value is an incredibly useful -//! building block for [unsafe] code to be able to reason about whether a raw pointer to the +//! building block for [`unsafe`] code to be able to reason about whether a raw pointer to the //! pinned value is still valid. [As we'll see later][drop-guarantee], this is necessarily from the //! time the value is first pinned until the end of its lifespan. This concept of "pinning" is //! necessary to implement safe interfaces on top of things like self-referential types and From 3acc5a0da327777b2e4a39a500a17546545dc569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Sun, 7 Jan 2024 18:22:47 +0100 Subject: [PATCH 37/40] effects: support ~const in assoc fns in trait impls --- compiler/rustc_ast_lowering/src/item.rs | 29 ++++++++------- .../const-impl-trait.stderr | 37 ++++++++++--------- .../tilde-const-assoc-fn-in-trait-impl.rs | 29 +++++++++++++++ ...=> tilde-const-inherent-assoc-const-fn.rs} | 0 4 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-assoc-fn-in-trait-impl.rs rename tests/ui/rfcs/rfc-2632-const-trait-impl/{tilde_const_on_impl_bound.rs => tilde-const-inherent-assoc-const-fn.rs} (100%) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index c618953461cf6..a2950a5011f57 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -12,6 +12,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::PredicateOrigin; use rustc_index::{Idx, IndexSlice, IndexVec}; +use rustc_middle::span_bug; use rustc_middle::ty::{ResolverAstLowering, TyCtxt}; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::symbol::{kw, sym, Ident}; @@ -572,23 +573,25 @@ impl<'hir> LoweringContext<'_, 'hir> { // This is used to track which lifetimes have already been defined, // and which need to be replicated when lowering an async fn. - match parent_hir.node().expect_item().kind { + let generics = match parent_hir.node().expect_item().kind { hir::ItemKind::Impl(impl_) => { self.is_in_trait_impl = impl_.of_trait.is_some(); + &impl_.generics } - hir::ItemKind::Trait(_, _, generics, _, _) if self.tcx.features().effects => { - self.host_param_id = generics - .params - .iter() - .find(|param| { - matches!( - param.kind, - hir::GenericParamKind::Const { is_host_effect: true, .. } - ) - }) - .map(|param| param.def_id); + hir::ItemKind::Trait(_, _, generics, _, _) => generics, + kind => { + span_bug!(item.span, "assoc item has unexpected kind of parent: {}", kind.descr()) } - _ => {} + }; + + if self.tcx.features().effects { + self.host_param_id = generics + .params + .iter() + .find(|param| { + matches!(param.kind, hir::GenericParamKind::Const { is_host_effect: true, .. }) + }) + .map(|param| param.def_id); } match ctxt { diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-trait.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-trait.stderr index ddedf8f1d8d27..d0ca1b19ad17d 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-trait.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/const-impl-trait.stderr @@ -1,27 +1,30 @@ -error[E0277]: can't compare `impl PartialEq + Destruct + Copy` with `impl PartialEq + Destruct + Copy` - --> $DIR/const-impl-trait.rs:28:17 +error[E0277]: can't compare `()` with `()` + --> $DIR/const-impl-trait.rs:35:17 | -LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `impl PartialEq + Destruct + Copy == impl PartialEq + Destruct + Copy` +LL | assert!(cmp(&())); + | --- ^^^ no implementation for `() == ()` + | | + | required by a bound introduced by this call | - = help: the trait `~const PartialEq` is not implemented for `impl PartialEq + Destruct + Copy` -note: required by a bound in `Foo::{opaque#0}` - --> $DIR/const-impl-trait.rs:24:22 + = help: the trait `const PartialEq` is not implemented for `()` + = help: the trait `PartialEq` is implemented for `()` +note: required by a bound in `cmp` + --> $DIR/const-impl-trait.rs:12:23 | -LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy; - | ^^^^^^^^^^^^^^^^ required by this bound in `Foo::{opaque#0}` +LL | const fn cmp(a: &impl ~const PartialEq) -> bool { + | ^^^^^^^^^^^^^^^^ required by this bound in `cmp` -error[E0277]: can't drop `impl PartialEq + Destruct + Copy` - --> $DIR/const-impl-trait.rs:28:17 +error[E0277]: can't compare `&impl ~const PartialEq` with `&impl ~const PartialEq` + --> $DIR/const-impl-trait.rs:13:7 | -LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `~const Destruct` is not implemented for `impl PartialEq + Destruct + Copy` +LL | a == a + | ^^ no implementation for `&impl ~const PartialEq == &impl ~const PartialEq` | -note: required by a bound in `Foo::{opaque#0}` - --> $DIR/const-impl-trait.rs:24:41 + = help: the trait `~const PartialEq<&impl ~const PartialEq>` is not implemented for `&impl ~const PartialEq` +help: consider dereferencing both sides of the expression | -LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy; - | ^^^^^^^^^^^^^^^ required by this bound in `Foo::{opaque#0}` +LL | *a == *a + | + + error: aborting due to 2 previous errors diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-assoc-fn-in-trait-impl.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-assoc-fn-in-trait-impl.rs new file mode 100644 index 0000000000000..a848b6d2fc9f2 --- /dev/null +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-assoc-fn-in-trait-impl.rs @@ -0,0 +1,29 @@ +// Regression test for issue #119700. +// check-pass + +#![feature(const_trait_impl, effects)] + +#[const_trait] +trait Main { + fn compute() -> u32; +} + +impl const Main for () { + fn compute() -> u32 { + T::generate() + } +} + +#[const_trait] +trait Aux { + fn generate() -> u32; +} + +impl const Aux for () { + fn generate() -> u32 { 1024 } +} + +fn main() { + const _: u32 = <()>::compute::<()>(); + let _ = <()>::compute::<()>(); +} diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-inherent-assoc-const-fn.rs similarity index 100% rename from tests/ui/rfcs/rfc-2632-const-trait-impl/tilde_const_on_impl_bound.rs rename to tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-inherent-assoc-const-fn.rs From 7e38b70cc020a0a472c8eafb77cd3fec45f79ae1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 7 Jan 2024 17:11:48 +0000 Subject: [PATCH 38/40] Split note, fix const/static impl trait error --- compiler/rustc_ast_lowering/messages.ftl | 3 +- compiler/rustc_ast_lowering/src/errors.rs | 1 + compiler/rustc_ast_lowering/src/item.rs | 10 +- compiler/rustc_ast_lowering/src/lib.rs | 4 +- .../ui/associated-consts/issue-105330.stderr | 4 +- .../feature-gate-associated_type_bounds.rs | 6 +- ...feature-gate-associated_type_bounds.stderr | 12 +- ...ture-gate-impl_trait_in_fn_trait_return.rs | 4 +- ...-gate-impl_trait_in_fn_trait_return.stderr | 6 +- tests/ui/impl-trait/issues/issue-54600.rs | 2 +- tests/ui/impl-trait/issues/issue-54600.stderr | 4 +- tests/ui/impl-trait/issues/issue-54840.rs | 2 +- tests/ui/impl-trait/issues/issue-54840.stderr | 4 +- tests/ui/impl-trait/issues/issue-58504.rs | 2 +- tests/ui/impl-trait/issues/issue-58504.stderr | 4 +- tests/ui/impl-trait/issues/issue-58956.rs | 4 +- tests/ui/impl-trait/issues/issue-58956.stderr | 8 +- tests/ui/impl-trait/issues/issue-70971.rs | 2 +- tests/ui/impl-trait/issues/issue-70971.stderr | 4 +- tests/ui/impl-trait/issues/issue-79099.rs | 2 +- tests/ui/impl-trait/issues/issue-79099.stderr | 4 +- ...sue-83929-impl-trait-in-generic-default.rs | 4 +- ...83929-impl-trait-in-generic-default.stderr | 8 +- tests/ui/impl-trait/issues/issue-84919.rs | 2 +- tests/ui/impl-trait/issues/issue-84919.stderr | 4 +- tests/ui/impl-trait/issues/issue-86642.rs | 2 +- tests/ui/impl-trait/issues/issue-86642.stderr | 4 +- tests/ui/impl-trait/issues/issue-87295.rs | 2 +- tests/ui/impl-trait/issues/issue-87295.stderr | 4 +- tests/ui/impl-trait/nested_impl_trait.rs | 2 +- tests/ui/impl-trait/nested_impl_trait.stderr | 4 +- tests/ui/impl-trait/where-allowed.rs | 74 ++++----- tests/ui/impl-trait/where-allowed.stderr | 148 +++++++++++++----- tests/ui/issues/issue-47715.rs | 8 +- tests/ui/issues/issue-47715.stderr | 16 +- .../type-alias-impl-trait-fn-type.rs | 2 +- .../type-alias-impl-trait-fn-type.stderr | 4 +- tests/ui/typeck/issue-104513-ice.rs | 2 +- tests/ui/typeck/issue-104513-ice.stderr | 4 +- 39 files changed, 257 insertions(+), 129 deletions(-) diff --git a/compiler/rustc_ast_lowering/messages.ftl b/compiler/rustc_ast_lowering/messages.ftl index fd94e7e9341d4..f4e3086f2b589 100644 --- a/compiler/rustc_ast_lowering/messages.ftl +++ b/compiler/rustc_ast_lowering/messages.ftl @@ -106,7 +106,8 @@ ast_lowering_misplaced_double_dot = .note = only allowed in tuple, tuple struct, and slice patterns ast_lowering_misplaced_impl_trait = - `impl Trait` only allowed in function and inherent method argument and return types, not in {$position} + `impl Trait` is not allowed in {$position} + .note = `impl Trait` is only allowed in arguments and return types of functions and methods ast_lowering_misplaced_relax_trait_bound = `?Trait` bounds are only permitted at the point where a type parameter is declared diff --git a/compiler/rustc_ast_lowering/src/errors.rs b/compiler/rustc_ast_lowering/src/errors.rs index 710690d0d86a4..faa22eece380b 100644 --- a/compiler/rustc_ast_lowering/src/errors.rs +++ b/compiler/rustc_ast_lowering/src/errors.rs @@ -90,6 +90,7 @@ pub enum AssocTyParenthesesSub { #[derive(Diagnostic)] #[diag(ast_lowering_misplaced_impl_trait, code = "E0562")] +#[note] pub struct MisplacedImplTrait<'a> { #[primary_span] pub span: Span, diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index c618953461cf6..7fd7d9489b068 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -182,7 +182,8 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => { - let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); + let (ty, body_id) = + self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy); hir::ItemKind::Static(ty, *m, body_id) } ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => { @@ -191,7 +192,9 @@ impl<'hir> LoweringContext<'_, 'hir> { Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), - |this| this.lower_const_item(ty, span, expr.as_deref()), + |this| { + this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy) + }, ); hir::ItemKind::Const(ty, generics, body_id) } @@ -448,8 +451,9 @@ impl<'hir> LoweringContext<'_, 'hir> { ty: &Ty, span: Span, body: Option<&Expr>, + impl_trait_position: ImplTraitPosition, ) -> (&'hir hir::Ty<'hir>, hir::BodyId) { - let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy)); + let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(impl_trait_position)); (ty, self.lower_const_body(span, body)) } diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 998a7322fa014..dc23b1dce7bf5 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -322,7 +322,7 @@ impl std::fmt::Display for ImplTraitPosition { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let name = match self { ImplTraitPosition::Path => "paths", - ImplTraitPosition::Variable => "variable bindings", + ImplTraitPosition::Variable => "the type of variable bindings", ImplTraitPosition::Trait => "traits", ImplTraitPosition::AsyncBlock => "async blocks", ImplTraitPosition::Bound => "bounds", @@ -334,7 +334,7 @@ impl std::fmt::Display for ImplTraitPosition { ImplTraitPosition::ExternFnReturn => "`extern fn` return types", ImplTraitPosition::ClosureReturn => "closure return types", ImplTraitPosition::PointerReturn => "`fn` pointer return types", - ImplTraitPosition::FnTraitReturn => "the return types of `Fn` trait bounds", + ImplTraitPosition::FnTraitReturn => "the return type of `Fn` trait bounds", ImplTraitPosition::GenericDefault => "generic parameter defaults", ImplTraitPosition::ConstTy => "const types", ImplTraitPosition::StaticTy => "static types", diff --git a/tests/ui/associated-consts/issue-105330.stderr b/tests/ui/associated-consts/issue-105330.stderr index aeedf6b194978..e1461fec296e0 100644 --- a/tests/ui/associated-consts/issue-105330.stderr +++ b/tests/ui/associated-consts/issue-105330.stderr @@ -33,11 +33,13 @@ LL | fn main>() { = note: see issue #92827 for more information = help: add `#![feature(associated_const_equality)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in impl headers +error[E0562]: `impl Trait` is not allowed in impl headers --> $DIR/issue-105330.rs:6:27 | LL | impl TraitWAssocConst for impl Demo { | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error[E0131]: `main` function is not allowed to have generic parameters --> $DIR/issue-105330.rs:15:8 diff --git a/tests/ui/feature-gates/feature-gate-associated_type_bounds.rs b/tests/ui/feature-gates/feature-gate-associated_type_bounds.rs index 152c7a8de66a1..073599edad7b9 100644 --- a/tests/ui/feature-gates/feature-gate-associated_type_bounds.rs +++ b/tests/ui/feature-gates/feature-gate-associated_type_bounds.rs @@ -54,20 +54,20 @@ fn _rpit_dyn() -> Box> { Box::new(S1) } const _cdef: impl Tr1 = S1; //~^ ERROR associated type bounds are unstable -//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~| ERROR `impl Trait` is not allowed in const types // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // const _cdef_dyn: &dyn Tr1 = &S1; static _sdef: impl Tr1 = S1; //~^ ERROR associated type bounds are unstable -//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~| ERROR `impl Trait` is not allowed in static types // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // static _sdef_dyn: &dyn Tr1 = &S1; fn main() { let _: impl Tr1 = S1; //~^ ERROR associated type bounds are unstable - //~| ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~| ERROR `impl Trait` is not allowed in the type of variable bindings // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // let _: &dyn Tr1 = &S1; } diff --git a/tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr b/tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr index f2bceda9bea71..4a643d31259f2 100644 --- a/tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr +++ b/tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr @@ -115,23 +115,29 @@ LL | let _: impl Tr1 = S1; = note: see issue #52662 for more information = help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types +error[E0562]: `impl Trait` is not allowed in const types --> $DIR/feature-gate-associated_type_bounds.rs:55:14 | LL | const _cdef: impl Tr1 = S1; | ^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types +error[E0562]: `impl Trait` is not allowed in static types --> $DIR/feature-gate-associated_type_bounds.rs:61:15 | LL | static _sdef: impl Tr1 = S1; | ^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/feature-gate-associated_type_bounds.rs:68:12 | LL | let _: impl Tr1 = S1; | ^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error[E0277]: the trait bound `<::A as Iterator>::Item: Copy` is not satisfied --> $DIR/feature-gate-associated_type_bounds.rs:12:28 diff --git a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs index f07abb9d04967..c75eabd6ac83e 100644 --- a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs +++ b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.rs @@ -1,6 +1,6 @@ fn f() -> impl Fn() -> impl Sized { || () } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds fn g() -> &'static dyn Fn() -> impl Sized { &|| () } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr index af56e2bd9ef3e..dacf1ca4c37c5 100644 --- a/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr +++ b/tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr @@ -1,18 +1,20 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:1:24 | LL | fn f() -> impl Fn() -> impl Sized { || () } | ^^^^^^^^^^ | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods = note: see issue #99697 for more information = help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32 | LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () } | ^^^^^^^^^^ | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods = note: see issue #99697 for more information = help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable diff --git a/tests/ui/impl-trait/issues/issue-54600.rs b/tests/ui/impl-trait/issues/issue-54600.rs index ccf2767012e6d..62bfd7cd96881 100644 --- a/tests/ui/impl-trait/issues/issue-54600.rs +++ b/tests/ui/impl-trait/issues/issue-54600.rs @@ -2,6 +2,6 @@ use std::fmt::Debug; fn main() { let x: Option = Some(44_u32); - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings println!("{:?}", x); } diff --git a/tests/ui/impl-trait/issues/issue-54600.stderr b/tests/ui/impl-trait/issues/issue-54600.stderr index 946ad74b87228..c75c0fa0f055d 100644 --- a/tests/ui/impl-trait/issues/issue-54600.stderr +++ b/tests/ui/impl-trait/issues/issue-54600.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-54600.rs:4:19 | LL | let x: Option = Some(44_u32); | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-54840.rs b/tests/ui/impl-trait/issues/issue-54840.rs index 910d23f1d9386..65257d2f7f1db 100644 --- a/tests/ui/impl-trait/issues/issue-54840.rs +++ b/tests/ui/impl-trait/issues/issue-54840.rs @@ -3,5 +3,5 @@ use std::ops::Add; fn main() { let i: i32 = 0; let j: &impl Add = &i; - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings } diff --git a/tests/ui/impl-trait/issues/issue-54840.stderr b/tests/ui/impl-trait/issues/issue-54840.stderr index c4ab79f110dde..de75256d5a939 100644 --- a/tests/ui/impl-trait/issues/issue-54840.stderr +++ b/tests/ui/impl-trait/issues/issue-54840.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-54840.rs:5:13 | LL | let j: &impl Add = &i; | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-58504.rs b/tests/ui/impl-trait/issues/issue-58504.rs index 03b51ae92d189..4f7a35e81b80b 100644 --- a/tests/ui/impl-trait/issues/issue-58504.rs +++ b/tests/ui/impl-trait/issues/issue-58504.rs @@ -8,5 +8,5 @@ fn mk_gen() -> impl Coroutine { fn main() { let gens: [impl Coroutine;2] = [ mk_gen(), mk_gen() ]; - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings } diff --git a/tests/ui/impl-trait/issues/issue-58504.stderr b/tests/ui/impl-trait/issues/issue-58504.stderr index e67e48728ed09..8231732bba1c2 100644 --- a/tests/ui/impl-trait/issues/issue-58504.stderr +++ b/tests/ui/impl-trait/issues/issue-58504.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-58504.rs:10:16 | LL | let gens: [impl Coroutine;2] = [ mk_gen(), mk_gen() ]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-58956.rs b/tests/ui/impl-trait/issues/issue-58956.rs index 5d5566860c0fc..a59de2379d8fa 100644 --- a/tests/ui/impl-trait/issues/issue-58956.rs +++ b/tests/ui/impl-trait/issues/issue-58956.rs @@ -5,9 +5,9 @@ impl Lam for B {} pub struct Wrap(T); const _A: impl Lam = { - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in const types let x: Wrap = Wrap(B); - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings x.0 }; diff --git a/tests/ui/impl-trait/issues/issue-58956.stderr b/tests/ui/impl-trait/issues/issue-58956.stderr index 5ee33352afa2a..0c81c69def3aa 100644 --- a/tests/ui/impl-trait/issues/issue-58956.stderr +++ b/tests/ui/impl-trait/issues/issue-58956.stderr @@ -1,14 +1,18 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types +error[E0562]: `impl Trait` is not allowed in const types --> $DIR/issue-58956.rs:7:11 | LL | const _A: impl Lam = { | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-58956.rs:9:17 | LL | let x: Wrap = Wrap(B); | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 2 previous errors diff --git a/tests/ui/impl-trait/issues/issue-70971.rs b/tests/ui/impl-trait/issues/issue-70971.rs index c24259a71eb22..2f2c2e8f441a8 100644 --- a/tests/ui/impl-trait/issues/issue-70971.rs +++ b/tests/ui/impl-trait/issues/issue-70971.rs @@ -1,4 +1,4 @@ fn main() { let x : (impl Copy,) = (true,); - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings } diff --git a/tests/ui/impl-trait/issues/issue-70971.stderr b/tests/ui/impl-trait/issues/issue-70971.stderr index fcc67291e4d2b..28c463cea8504 100644 --- a/tests/ui/impl-trait/issues/issue-70971.stderr +++ b/tests/ui/impl-trait/issues/issue-70971.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-70971.rs:2:14 | LL | let x : (impl Copy,) = (true,); | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-79099.rs b/tests/ui/impl-trait/issues/issue-79099.rs index 22c66491ce988..757e61fb631ea 100644 --- a/tests/ui/impl-trait/issues/issue-79099.rs +++ b/tests/ui/impl-trait/issues/issue-79099.rs @@ -1,7 +1,7 @@ struct Bug { V1: [(); { let f: impl core::future::Future = async { 1 }; - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings //~| expected identifier 1 }], diff --git a/tests/ui/impl-trait/issues/issue-79099.stderr b/tests/ui/impl-trait/issues/issue-79099.stderr index 82fc03c61b3b3..6c26d5bd1b742 100644 --- a/tests/ui/impl-trait/issues/issue-79099.stderr +++ b/tests/ui/impl-trait/issues/issue-79099.stderr @@ -9,11 +9,13 @@ LL | let f: impl core::future::Future = async { 1 }; = help: pass `--edition 2021` to `rustc` = note: for more on editions, read https://doc.rust-lang.org/edition-guide -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-79099.rs:3:16 | LL | let f: impl core::future::Future = async { 1 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 2 previous errors diff --git a/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.rs b/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.rs index 3224145bffeb4..771b29f3c7e18 100644 --- a/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.rs +++ b/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.rs @@ -1,8 +1,8 @@ struct Foo(T); -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults type Result = std::result::Result; -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults // should not cause ICE fn x() -> Foo { diff --git a/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.stderr b/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.stderr index 56be4577d51a0..07d6c5b6b1c88 100644 --- a/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.stderr +++ b/tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.stderr @@ -1,14 +1,18 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16 | LL | struct Foo(T); | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/issue-83929-impl-trait-in-generic-default.rs:4:20 | LL | type Result = std::result::Result; | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 2 previous errors diff --git a/tests/ui/impl-trait/issues/issue-84919.rs b/tests/ui/impl-trait/issues/issue-84919.rs index 77d27d7c06b6c..0f911ba23ae20 100644 --- a/tests/ui/impl-trait/issues/issue-84919.rs +++ b/tests/ui/impl-trait/issues/issue-84919.rs @@ -3,7 +3,7 @@ impl Trait for () {} fn foo<'a: 'a>() { let _x: impl Trait = (); - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings } fn main() {} diff --git a/tests/ui/impl-trait/issues/issue-84919.stderr b/tests/ui/impl-trait/issues/issue-84919.stderr index 963865efa69f7..02d2ce28fb3ed 100644 --- a/tests/ui/impl-trait/issues/issue-84919.stderr +++ b/tests/ui/impl-trait/issues/issue-84919.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-84919.rs:5:13 | LL | let _x: impl Trait = (); | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-86642.rs b/tests/ui/impl-trait/issues/issue-86642.rs index 49f8944ac4af0..74be8779d4458 100644 --- a/tests/ui/impl-trait/issues/issue-86642.rs +++ b/tests/ui/impl-trait/issues/issue-86642.rs @@ -1,5 +1,5 @@ static x: impl Fn(&str) -> Result<&str, ()> = move |source| { - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in static types let res = (move |source| Ok(source))(source); let res = res.or((move |source| Ok(source))(source)); res diff --git a/tests/ui/impl-trait/issues/issue-86642.stderr b/tests/ui/impl-trait/issues/issue-86642.stderr index 3ad18a13290a2..19fd5bc0c1ca4 100644 --- a/tests/ui/impl-trait/issues/issue-86642.stderr +++ b/tests/ui/impl-trait/issues/issue-86642.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types +error[E0562]: `impl Trait` is not allowed in static types --> $DIR/issue-86642.rs:1:11 | LL | static x: impl Fn(&str) -> Result<&str, ()> = move |source| { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/issues/issue-87295.rs b/tests/ui/impl-trait/issues/issue-87295.rs index eb44020ac0e72..a765e14884b09 100644 --- a/tests/ui/impl-trait/issues/issue-87295.rs +++ b/tests/ui/impl-trait/issues/issue-87295.rs @@ -14,5 +14,5 @@ impl Struct { fn main() { let _do_not_waste: Struct> = Struct::new(()); - //~^ `impl Trait` only allowed in function and inherent method argument and return types + //~^ `impl Trait` is not allowed in the type of variable bindings } diff --git a/tests/ui/impl-trait/issues/issue-87295.stderr b/tests/ui/impl-trait/issues/issue-87295.stderr index e9a635f244bcb..78274a056ec68 100644 --- a/tests/ui/impl-trait/issues/issue-87295.stderr +++ b/tests/ui/impl-trait/issues/issue-87295.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-87295.rs:16:31 | LL | let _do_not_waste: Struct> = Struct::new(()); | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/nested_impl_trait.rs b/tests/ui/impl-trait/nested_impl_trait.rs index c036b9e367a17..760102794c34e 100644 --- a/tests/ui/impl-trait/nested_impl_trait.rs +++ b/tests/ui/impl-trait/nested_impl_trait.rs @@ -9,7 +9,7 @@ fn bad_in_ret_position(x: impl Into) -> impl Into { x } fn bad_in_fn_syntax(x: fn() -> impl Into) {} //~^ ERROR nested `impl Trait` is not allowed -//~| `impl Trait` only allowed in function and inherent method argument and return types +//~| `impl Trait` is not allowed in `fn` pointer fn bad_in_arg_position(_: impl Into) { } //~^ ERROR nested `impl Trait` is not allowed diff --git a/tests/ui/impl-trait/nested_impl_trait.stderr b/tests/ui/impl-trait/nested_impl_trait.stderr index f1cafd958b05e..31c3e0c901365 100644 --- a/tests/ui/impl-trait/nested_impl_trait.stderr +++ b/tests/ui/impl-trait/nested_impl_trait.stderr @@ -34,11 +34,13 @@ LL | fn bad(x: impl Into) -> impl Into { x } | | nested `impl Trait` here | outer `impl Trait` -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer return types +error[E0562]: `impl Trait` is not allowed in `fn` pointer return types --> $DIR/nested_impl_trait.rs:10:32 | LL | fn bad_in_fn_syntax(x: fn() -> impl Into) {} | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error[E0277]: the trait bound `impl Debug: From>` is not satisfied --> $DIR/nested_impl_trait.rs:6:46 diff --git a/tests/ui/impl-trait/where-allowed.rs b/tests/ui/impl-trait/where-allowed.rs index 158dc5ab9745d..5ce63db684f3e 100644 --- a/tests/ui/impl-trait/where-allowed.rs +++ b/tests/ui/impl-trait/where-allowed.rs @@ -16,47 +16,47 @@ fn in_adt_in_parameters(_: Vec) { panic!() } // Disallowed fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in `fn` pointer // Disallowed fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in `fn` pointer // Disallowed fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in `fn` pointer // Disallowed fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in `fn` pointer // Disallowed fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds // Disallowed fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds // Disallowed fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds // Allowed fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() } // Disallowed fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds //~^^ ERROR nested `impl Trait` is not allowed // Disallowed fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds // Disallowed fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds //~| ERROR nested `impl Trait` is not allowed // Allowed @@ -64,11 +64,11 @@ fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() // Disallowed fn in_Fn_parameter_in_generics (_: F) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds // Disallowed fn in_Fn_return_in_generics impl Debug> (_: F) { panic!() } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds // Allowed @@ -81,22 +81,22 @@ fn in_impl_Trait_in_return() -> impl IntoIterator { // Disallowed struct InBraceStructField { x: impl Debug } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in field types // Disallowed struct InAdtInBraceStructField { x: Vec } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in field types // Disallowed struct InTupleStructField(impl Debug); -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in field types // Disallowed enum InEnum { InBraceVariant { x: impl Debug }, - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in field types InTupleVariant(impl Debug), - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in field types } // Allowed @@ -136,10 +136,10 @@ impl DummyType { // Disallowed extern "C" { fn in_foreign_parameters(_: impl Debug); - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in `extern fn` fn in_foreign_return() -> impl Debug; - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in `extern fn` } // Allowed @@ -155,97 +155,97 @@ type InTypeAlias = impl Debug; //~^ ERROR `impl Trait` in type aliases is unstable type InReturnInTypeAlias = fn() -> impl Debug; -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in `fn` pointer //~| ERROR `impl Trait` in type aliases is unstable // Disallowed in impl headers impl PartialEq for () { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in traits } // Disallowed in impl headers impl PartialEq<()> for impl Debug { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in impl headers } // Disallowed in inherent impls impl impl Debug { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in impl headers } // Disallowed in inherent impls struct InInherentImplAdt { t: T } impl InInherentImplAdt { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in impl headers } // Disallowed in where clauses fn in_fn_where_clause() where impl Debug: Debug -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in bounds { } // Disallowed in where clauses fn in_adt_in_fn_where_clause() where Vec: Debug -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in bounds { } // Disallowed fn in_trait_parameter_in_fn_where_clause() where T: PartialEq -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in bounds { } // Disallowed fn in_Fn_parameter_in_fn_where_clause() where T: Fn(impl Debug) -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the parameters of `Fn` trait bounds { } // Disallowed fn in_Fn_return_in_fn_where_clause() where T: Fn() -> impl Debug -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds { } // Disallowed struct InStructGenericParamDefault(T); -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults // Disallowed enum InEnumGenericParamDefault { Variant(T) } -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults // Disallowed trait InTraitGenericParamDefault {} -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults // Disallowed type InTypeAliasGenericParamDefault = T; -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generic parameter defaults // Disallowed impl T {} //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions //~| WARNING this was previously accepted by the compiler but is being phased out -//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~| ERROR `impl Trait` is not allowed in generic parameter defaults //~| ERROR no nominal type found // Disallowed fn in_method_generic_param_default(_: T) {} //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions //~| WARNING this was previously accepted by the compiler but is being phased out -//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~| ERROR `impl Trait` is not allowed in generic parameter defaults fn main() { let _in_local_variable: impl Fn() = || {}; - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in the type of variable bindings let _in_return_in_local_variable = || -> impl Fn() { || {} }; - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in closure return types } diff --git a/tests/ui/impl-trait/where-allowed.stderr b/tests/ui/impl-trait/where-allowed.stderr index 9c841342ed384..5e8a8637d0480 100644 --- a/tests/ui/impl-trait/where-allowed.stderr +++ b/tests/ui/impl-trait/where-allowed.stderr @@ -43,227 +43,301 @@ LL | type InReturnInTypeAlias = fn() -> impl Debug; = note: see issue #63063 for more information = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer parameters +error[E0562]: `impl Trait` is not allowed in `fn` pointer parameters --> $DIR/where-allowed.rs:18:40 | LL | fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer return types +error[E0562]: `impl Trait` is not allowed in `fn` pointer return types --> $DIR/where-allowed.rs:22:42 | LL | fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer parameters +error[E0562]: `impl Trait` is not allowed in `fn` pointer parameters --> $DIR/where-allowed.rs:26:38 | LL | fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer return types +error[E0562]: `impl Trait` is not allowed in `fn` pointer return types --> $DIR/where-allowed.rs:30:40 | LL | fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:34:49 | LL | fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/where-allowed.rs:38:51 | LL | fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:42:55 | LL | fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:49:51 | LL | fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/where-allowed.rs:54:53 | LL | fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:58:57 | LL | fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:66:38 | LL | fn in_Fn_parameter_in_generics (_: F) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/where-allowed.rs:70:40 | LL | fn in_Fn_return_in_generics impl Debug> (_: F) { panic!() } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in field types +error[E0562]: `impl Trait` is not allowed in field types --> $DIR/where-allowed.rs:83:32 | LL | struct InBraceStructField { x: impl Debug } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in field types +error[E0562]: `impl Trait` is not allowed in field types --> $DIR/where-allowed.rs:87:41 | LL | struct InAdtInBraceStructField { x: Vec } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in field types +error[E0562]: `impl Trait` is not allowed in field types --> $DIR/where-allowed.rs:91:27 | LL | struct InTupleStructField(impl Debug); | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in field types +error[E0562]: `impl Trait` is not allowed in field types --> $DIR/where-allowed.rs:96:25 | LL | InBraceVariant { x: impl Debug }, | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in field types +error[E0562]: `impl Trait` is not allowed in field types --> $DIR/where-allowed.rs:98:20 | LL | InTupleVariant(impl Debug), | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `extern fn` parameters +error[E0562]: `impl Trait` is not allowed in `extern fn` parameters --> $DIR/where-allowed.rs:138:33 | LL | fn in_foreign_parameters(_: impl Debug); | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `extern fn` return types +error[E0562]: `impl Trait` is not allowed in `extern fn` return types --> $DIR/where-allowed.rs:141:31 | LL | fn in_foreign_return() -> impl Debug; | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer return types +error[E0562]: `impl Trait` is not allowed in `fn` pointer return types --> $DIR/where-allowed.rs:157:39 | LL | type InReturnInTypeAlias = fn() -> impl Debug; | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in traits +error[E0562]: `impl Trait` is not allowed in traits --> $DIR/where-allowed.rs:162:16 | LL | impl PartialEq for () { | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in impl headers +error[E0562]: `impl Trait` is not allowed in impl headers --> $DIR/where-allowed.rs:167:24 | LL | impl PartialEq<()> for impl Debug { | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in impl headers +error[E0562]: `impl Trait` is not allowed in impl headers --> $DIR/where-allowed.rs:172:6 | LL | impl impl Debug { | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in impl headers +error[E0562]: `impl Trait` is not allowed in impl headers --> $DIR/where-allowed.rs:178:24 | LL | impl InInherentImplAdt { | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in bounds +error[E0562]: `impl Trait` is not allowed in bounds --> $DIR/where-allowed.rs:184:11 | LL | where impl Debug: Debug | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in bounds +error[E0562]: `impl Trait` is not allowed in bounds --> $DIR/where-allowed.rs:191:15 | LL | where Vec: Debug | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in bounds +error[E0562]: `impl Trait` is not allowed in bounds --> $DIR/where-allowed.rs:198:24 | LL | where T: PartialEq | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the parameters of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the parameters of `Fn` trait bounds --> $DIR/where-allowed.rs:205:17 | LL | where T: Fn(impl Debug) | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds +error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds --> $DIR/where-allowed.rs:212:22 | LL | where T: Fn() -> impl Debug | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:218:40 | LL | struct InStructGenericParamDefault(T); | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:222:36 | LL | enum InEnumGenericParamDefault { Variant(T) } | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:226:38 | LL | trait InTraitGenericParamDefault {} | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:230:41 | LL | type InTypeAliasGenericParamDefault = T; | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:234:11 | LL | impl T {} | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults +error[E0562]: `impl Trait` is not allowed in generic parameter defaults --> $DIR/where-allowed.rs:241:40 | LL | fn in_method_generic_param_default(_: T) {} | ^^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/where-allowed.rs:247:29 | LL | let _in_local_variable: impl Fn() = || {}; | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in closure return types +error[E0562]: `impl Trait` is not allowed in closure return types --> $DIR/where-allowed.rs:249:46 | LL | let _in_return_in_local_variable = || -> impl Fn() { || {} }; | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions --> $DIR/where-allowed.rs:234:7 diff --git a/tests/ui/issues/issue-47715.rs b/tests/ui/issues/issue-47715.rs index 0a770593bc9ce..bf2b03351b29a 100644 --- a/tests/ui/issues/issue-47715.rs +++ b/tests/ui/issues/issue-47715.rs @@ -7,22 +7,22 @@ trait Iterable { } struct Container> { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in generics field: T } enum Enum> { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in generics A(T), } union Union + Copy> { - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in generics x: T, } type Type> = T; -//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR `impl Trait` is not allowed in generics fn main() { } diff --git a/tests/ui/issues/issue-47715.stderr b/tests/ui/issues/issue-47715.stderr index 2ded98781c643..8ed9ff4395216 100644 --- a/tests/ui/issues/issue-47715.stderr +++ b/tests/ui/issues/issue-47715.stderr @@ -1,26 +1,34 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generics +error[E0562]: `impl Trait` is not allowed in generics --> $DIR/issue-47715.rs:9:37 | LL | struct Container> { | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generics +error[E0562]: `impl Trait` is not allowed in generics --> $DIR/issue-47715.rs:14:30 | LL | enum Enum> { | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generics +error[E0562]: `impl Trait` is not allowed in generics --> $DIR/issue-47715.rs:19:32 | LL | union Union + Copy> { | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generics +error[E0562]: `impl Trait` is not allowed in generics --> $DIR/issue-47715.rs:24:30 | LL | type Type> = T; | ^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 4 previous errors diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.rs b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.rs index 9796823a72440..46c2c22cac1c9 100644 --- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.rs +++ b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.rs @@ -4,7 +4,7 @@ // FIXME: this is ruled out for now but should work type Foo = fn() -> impl Send; -//~^ ERROR: `impl Trait` only allowed in function and inherent method argument and return types +//~^ ERROR: `impl Trait` is not allowed in `fn` pointer return types fn make_foo() -> Foo { || 15 diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.stderr b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.stderr index 5641ff301649f..0f73c14247046 100644 --- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.stderr +++ b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-fn-type.stderr @@ -1,8 +1,10 @@ -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in `fn` pointer return types +error[E0562]: `impl Trait` is not allowed in `fn` pointer return types --> $DIR/type-alias-impl-trait-fn-type.rs:6:20 | LL | type Foo = fn() -> impl Send; | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 1 previous error diff --git a/tests/ui/typeck/issue-104513-ice.rs b/tests/ui/typeck/issue-104513-ice.rs index 4968d3f51fe80..aaeee9cef48ac 100644 --- a/tests/ui/typeck/issue-104513-ice.rs +++ b/tests/ui/typeck/issue-104513-ice.rs @@ -1,6 +1,6 @@ struct S; fn f() { let _: S = S; //~ ERROR cannot find trait `Oops` in this scope - //~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types + //~^ ERROR `impl Trait` is not allowed in the type of variable bindings } fn main() {} diff --git a/tests/ui/typeck/issue-104513-ice.stderr b/tests/ui/typeck/issue-104513-ice.stderr index 56c6b33615462..37d38a76a4034 100644 --- a/tests/ui/typeck/issue-104513-ice.stderr +++ b/tests/ui/typeck/issue-104513-ice.stderr @@ -4,11 +4,13 @@ error[E0405]: cannot find trait `Oops` in this scope LL | let _: S = S; | ^^^^ not found in this scope -error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings +error[E0562]: `impl Trait` is not allowed in the type of variable bindings --> $DIR/issue-104513-ice.rs:3:14 | LL | let _: S = S; | ^^^^^^^^^ + | + = note: `impl Trait` is only allowed in arguments and return types of functions and methods error: aborting due to 2 previous errors From 68bb76634d916a5ea692f0a6bfae359e7ff556ab Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 7 Jan 2024 18:59:20 +0000 Subject: [PATCH 39/40] Unions are not PointerLike --- compiler/rustc_target/src/abi/mod.rs | 4 ++-- tests/ui/dyn-star/union.rs | 16 ++++++++++++++++ tests/ui/dyn-star/union.stderr | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/ui/dyn-star/union.rs create mode 100644 tests/ui/dyn-star/union.stderr diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index a274790bffcfd..24e49ff648f2f 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -120,11 +120,11 @@ impl<'a> Layout<'a> { /// Whether the layout is from a type that implements [`std::marker::PointerLike`]. /// /// Currently, that means that the type is pointer-sized, pointer-aligned, - /// and has a scalar ABI. + /// and has a initialized (non-union), scalar ABI. pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool { self.size() == data_layout.pointer_size && self.align().abi == data_layout.pointer_align.abi - && matches!(self.abi(), Abi::Scalar(..)) + && matches!(self.abi(), Abi::Scalar(Scalar::Initialized { .. })) } } diff --git a/tests/ui/dyn-star/union.rs b/tests/ui/dyn-star/union.rs new file mode 100644 index 0000000000000..ad3a85a937ae7 --- /dev/null +++ b/tests/ui/dyn-star/union.rs @@ -0,0 +1,16 @@ +#![feature(dyn_star)] +//~^ WARN the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes + +union Union { + x: usize, +} + +trait Trait {} +impl Trait for Union {} + +fn bar(_: dyn* Trait) {} + +fn main() { + bar(Union { x: 0usize }); + //~^ ERROR `Union` needs to have the same ABI as a pointer +} diff --git a/tests/ui/dyn-star/union.stderr b/tests/ui/dyn-star/union.stderr new file mode 100644 index 0000000000000..906eb4f5163e5 --- /dev/null +++ b/tests/ui/dyn-star/union.stderr @@ -0,0 +1,20 @@ +warning: the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/union.rs:1:12 + | +LL | #![feature(dyn_star)] + | ^^^^^^^^ + | + = note: see issue #102425 for more information + = note: `#[warn(incomplete_features)]` on by default + +error[E0277]: `Union` needs to have the same ABI as a pointer + --> $DIR/union.rs:14:9 + | +LL | bar(Union { x: 0usize }); + | ^^^^^^^^^^^^^^^^^^^ `Union` needs to be a pointer-like type + | + = help: the trait `PointerLike` is not implemented for `Union` + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0277`. From 5be2a85351fcf2a71831938022f5afc96c76f284 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:48:31 +0100 Subject: [PATCH 40/40] Delete unused makefile in tests/ui ?????????? --- src/tools/tidy/src/ui_tests.rs | 1 - tests/ui/unused-crate-deps/test.mk | 7 ------- 2 files changed, 8 deletions(-) delete mode 100644 tests/ui/unused-crate-deps/test.mk diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index b4745d4883c55..99c5dce347a07 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -33,7 +33,6 @@ const EXTENSION_EXCEPTION_PATHS: &[&str] = &[ "tests/ui/macros/macro-expanded-include/file.txt", // testing including data with the include macros "tests/ui/macros/not-utf8.bin", // testing including data with the include macros "tests/ui/macros/syntax-extension-source-utils-files/includeme.fragment", // more include - "tests/ui/unused-crate-deps/test.mk", // why would you use make "tests/ui/proc-macro/auxiliary/included-file.txt", // more include "tests/ui/invalid/foo.natvis.xml", // sample debugger visualizer ]; diff --git a/tests/ui/unused-crate-deps/test.mk b/tests/ui/unused-crate-deps/test.mk deleted file mode 100644 index 0b98b4e44fb2a..0000000000000 --- a/tests/ui/unused-crate-deps/test.mk +++ /dev/null @@ -1,7 +0,0 @@ -# Everyone uses make for building Rust - -foo: bar.rlib - $(RUSTC) --crate-type bin --extern bar=bar.rlib - -%.rlib: %.rs - $(RUSTC) --crate-type lib $<