From 7b7992fbcf5b8d9af945e32eac241252baa46fab Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Wed, 4 Sep 2024 16:03:47 -0700 Subject: [PATCH 1/5] Begin experimental support for pin reborrowing This commit adds basic support for reborrowing `Pin` types in argument position. At the moment it only supports reborrowing `Pin<&mut T>` as `Pin<&mut T>` by inserting a call to `Pin::as_mut()`, and only in argument position (not as the receiver in a method call). --- compiler/rustc_borrowck/messages.ftl | 2 +- .../rustc_borrowck/src/session_diagnostics.rs | 2 +- compiler/rustc_feature/src/unstable.rs | 2 + compiler/rustc_hir/src/lang_items.rs | 1 + compiler/rustc_hir_typeck/src/coercion.rs | 59 +++++++++++++++++++ .../rustc_hir_typeck/src/expr_use_visitor.rs | 15 +++++ compiler/rustc_middle/src/ty/adjustment.rs | 3 + compiler/rustc_mir_build/src/thir/cx/expr.rs | 45 ++++++++++++++ compiler/rustc_span/src/symbol.rs | 2 + library/core/src/pin.rs | 1 + tests/ui/async-await/pin-reborrow-arg.rs | 27 +++++++++ tests/ui/async-await/pin-reborrow-self.rs | 24 ++++++++ .../feature-gate-pin_ergonomics.rs | 15 +++++ .../feature-gate-pin_ergonomics.stderr | 21 +++++++ 14 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 tests/ui/async-await/pin-reborrow-arg.rs create mode 100644 tests/ui/async-await/pin-reborrow-self.rs create mode 100644 tests/ui/feature-gates/feature-gate-pin_ergonomics.rs create mode 100644 tests/ui/feature-gates/feature-gate-pin_ergonomics.stderr diff --git a/compiler/rustc_borrowck/messages.ftl b/compiler/rustc_borrowck/messages.ftl index edb25e12864bc..ee4b2f95cb151 100644 --- a/compiler/rustc_borrowck/messages.ftl +++ b/compiler/rustc_borrowck/messages.ftl @@ -207,7 +207,7 @@ borrowck_simd_intrinsic_arg_const = *[other] {$arg}th } argument of `{$intrinsic}` is required to be a `const` item -borrowck_suggest_create_freash_reborrow = +borrowck_suggest_create_fresh_reborrow = consider reborrowing the `Pin` instead of moving it borrowck_suggest_iterate_over_slice = diff --git a/compiler/rustc_borrowck/src/session_diagnostics.rs b/compiler/rustc_borrowck/src/session_diagnostics.rs index 4a50b0f070408..b6c6960d4ca00 100644 --- a/compiler/rustc_borrowck/src/session_diagnostics.rs +++ b/compiler/rustc_borrowck/src/session_diagnostics.rs @@ -415,7 +415,7 @@ pub(crate) enum CaptureReasonSuggest<'tcx> { span: Span, }, #[suggestion( - borrowck_suggest_create_freash_reborrow, + borrowck_suggest_create_fresh_reborrow, applicability = "maybe-incorrect", code = ".as_mut()", style = "verbose" diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index d0c0460ddfe32..cf20b79dfa945 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -558,6 +558,8 @@ declare_features! ( (unstable, optimize_attribute, "1.34.0", Some(54882)), /// Allows specifying nop padding on functions for dynamic patching. (unstable, patchable_function_entry, "1.81.0", Some(123115)), + /// Experimental features that make `Pin` more ergonomic. + (incomplete, pin_ergonomics, "CURRENT_RUSTC_VERSION", Some(130494)), /// Allows postfix match `expr.match { ... }` (unstable, postfix_match, "1.79.0", Some(121618)), /// Allows macro attributes on expressions, statements and non-inline modules. diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index e7398fd222636..60e024068e916 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -395,6 +395,7 @@ language_item_table! { IteratorNext, sym::next, next_fn, Target::Method(MethodKind::Trait { body: false}), GenericRequirement::None; PinNewUnchecked, sym::new_unchecked, new_unchecked_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None; + PinAsMut, sym::pin_as_mut, as_mut_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None; RangeFrom, sym::RangeFrom, range_from_struct, Target::Struct, GenericRequirement::None; RangeFull, sym::RangeFull, range_full_struct, Target::Struct, GenericRequirement::None; diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 3bada1de148b1..3da4b2893d9c3 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -214,6 +214,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { ty::Dynamic(predicates, region, ty::DynStar) if self.tcx.features().dyn_star => { return self.coerce_dyn_star(a, b, predicates, region); } + ty::Adt(pin, _) + if self.tcx.features().pin_ergonomics + && pin.did() == self.tcx.lang_items().pin_type().unwrap() => + { + return self.coerce_pin(a, b); + } _ => {} } @@ -774,6 +780,59 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { }) } + /// Applies reborrowing for `Pin` + /// + /// We currently only support reborrowing `Pin<&mut T>` as `Pin<&mut T>`. This is accomplished + /// by inserting a call to `Pin::as_mut` during MIR building. + /// + /// In the future we might want to support other reborrowing coercions, such as: + /// - `Pin<&mut T>` as `Pin<&T>` + /// - `Pin<&T>` as `Pin<&T>` + /// - `Pin>` as `Pin<&T>` + /// - `Pin>` as `Pin<&mut T>` + #[instrument(skip(self), level = "trace")] + fn coerce_pin(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> { + // We need to make sure the two types are compatible for coercion. + // Then we will build a ReborrowPin adjustment and return that as an InferOk. + + // Right now we can only reborrow if this is a `Pin<&mut T>`. + let can_reborrow = |ty: Ty<'tcx>| { + // Get the T out of Pin + let ty = match ty.kind() { + ty::Adt(pin, args) if pin.did() == self.tcx.lang_items().pin_type().unwrap() => { + args[0].expect_ty() + } + _ => { + debug!("can't reborrow {:?} as pinned", ty); + return None; + } + }; + // Make sure the T is something we understand (just `&mut U` for now) + match ty.kind() { + ty::Ref(region, ty, ty::Mutability::Mut) => Some((*region, *ty)), + _ => { + debug!("can't reborrow pin of inner type {:?}", ty); + None + } + } + }; + + let (_, _a_ty) = can_reborrow(a).ok_or(TypeError::Mismatch)?; + let (b_region, _b_ty) = can_reborrow(b).ok_or(TypeError::Mismatch)?; + + // To complete the reborrow, we need to make sure we can unify the inner types, and if so we + // add the adjustments. + self.unify_and(a, b, |_inner_ty| { + vec![Adjustment { + kind: Adjust::ReborrowPin(AutoBorrow::Ref( + b_region, + AutoBorrowMutability::Mut { allow_two_phase_borrow: AllowTwoPhase::No }, + )), + target: b, + }] + }) + } + fn coerce_from_safe_fn( &self, a: Ty<'tcx>, diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index da8c0ad3a30e2..103a4c72ea858 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -780,6 +780,20 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx adjustment::Adjust::Borrow(ref autoref) => { self.walk_autoref(expr, &place_with_id, autoref); } + + adjustment::Adjust::ReborrowPin(ref autoref) => { + // Reborrowing a Pin is like a combinations of a deref and a borrow, so we do + // both. + let bk = match autoref { + adjustment::AutoBorrow::Ref(_, m) => { + ty::BorrowKind::from_mutbl((*m).into()) + } + adjustment::AutoBorrow::RawPtr(m) => ty::BorrowKind::from_mutbl(*m), + }; + self.delegate.borrow_mut().borrow(&place_with_id, place_with_id.hir_id, bk); + + self.walk_autoref(expr, &place_with_id, autoref); + } } place_with_id = self.cat_expr_adjusted(expr, place_with_id, adjustment)?; } @@ -1284,6 +1298,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) | adjustment::Adjust::Borrow(_) + | adjustment::Adjust::ReborrowPin(_) | adjustment::Adjust::DynStar => { // Result is an rvalue. Ok(self.cat_rvalue(expr.hir_id, target)) diff --git a/compiler/rustc_middle/src/ty/adjustment.rs b/compiler/rustc_middle/src/ty/adjustment.rs index 1236c9efb41cf..de3619f5b6248 100644 --- a/compiler/rustc_middle/src/ty/adjustment.rs +++ b/compiler/rustc_middle/src/ty/adjustment.rs @@ -104,6 +104,9 @@ pub enum Adjust<'tcx> { /// Cast into a dyn* object. DynStar, + + /// Take a Pin and call either as_mut() or as_ref() to get a Pin<&mut T> or Pin<&T>. + ReborrowPin(AutoBorrow<'tcx>), } /// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)` diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index aa8ccc8b7ddd6..8e6e13f94e0e1 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -74,6 +74,7 @@ impl<'tcx> Cx<'tcx> { self.thir.exprs.push(expr) } + #[instrument(level = "trace", skip(self, expr, span))] fn apply_adjustment( &mut self, hir_expr: &'tcx hir::Expr<'tcx>, @@ -146,6 +147,50 @@ impl<'tcx> Cx<'tcx> { ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) } } Adjust::DynStar => ExprKind::Cast { source: self.thir.exprs.push(expr) }, + Adjust::ReborrowPin(AutoBorrow::Ref(region, m)) => { + debug!("apply ReborrowPin adjustment"); + match m { + AutoBorrowMutability::Mut { .. } => { + // Rewrite `$expr` as `Pin::as_mut(&mut $expr)` + let as_mut_method = + self.tcx().require_lang_item(rustc_hir::LangItem::PinAsMut, Some(span)); + let pin_ty_args = match expr.ty.kind() { + ty::Adt(_, args) => args, + _ => bug!("ReborrowPin with non-Pin type"), + }; + let as_mut_ty = + Ty::new_fn_def(self.tcx, as_mut_method, pin_ty_args.into_iter()); + + let ty = Ty::new_ref(self.tcx, region, expr.ty, ty::Mutability::Mut); + let arg = ExprKind::Borrow { + borrow_kind: BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, + arg: self.thir.exprs.push(expr), + }; + debug!(?arg, "borrow arg"); + let arg = self.thir.exprs.push(Expr { temp_lifetime, ty, span, kind: arg }); + + let kind = ExprKind::Call { + ty: as_mut_ty, + fun: self.thir.exprs.push(Expr { + temp_lifetime, + ty: as_mut_ty, + span, + kind: ExprKind::ZstLiteral { user_ty: None }, + }), + args: Box::new([arg]), + from_hir_call: true, + fn_span: span, + }; + debug!(?kind); + kind + } + AutoBorrowMutability::Not => { + // FIXME: We need to call Pin::as_ref on the expression + bug!("ReborrowPin with shared reference is not implemented yet") + } + } + } + Adjust::ReborrowPin(AutoBorrow::RawPtr(_)) => bug!("ReborrowPin with raw pointer"), }; Expr { temp_lifetime, ty: adjustment.target, span, kind } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index cbe2bafef21fd..979294563d735 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1418,6 +1418,8 @@ symbols! { pic, pie, pin, + pin_as_mut, + pin_ergonomics, platform_intrinsics, plugin, plugin_registrar, diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index 9c13662e08e8f..bb1196e82de78 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1408,6 +1408,7 @@ impl Pin { /// } /// } /// ``` + #[cfg_attr(not(bootstrap), lang = "pin_as_mut")] #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> { diff --git a/tests/ui/async-await/pin-reborrow-arg.rs b/tests/ui/async-await/pin-reborrow-arg.rs new file mode 100644 index 0000000000000..34f23533b65b2 --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-arg.rs @@ -0,0 +1,27 @@ +//@ check-pass + +#![feature(pin_ergonomics)] +#![allow(dead_code, incomplete_features)] + +use std::pin::Pin; + +struct Foo; + +impl Foo { + fn baz(self: Pin<&mut Self>) { + } +} + +fn foo(_: Pin<&mut Foo>) { +} + +fn bar(mut x: Pin<&mut Foo>) { + foo(x); + foo(x); // for this to work we need to automatically reborrow, + // as if the user had written `foo(x.as_mut())`. + + Foo::baz(x); + Foo::baz(x); +} + +fn main() {} diff --git a/tests/ui/async-await/pin-reborrow-self.rs b/tests/ui/async-await/pin-reborrow-self.rs new file mode 100644 index 0000000000000..b60b6982bb879 --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-self.rs @@ -0,0 +1,24 @@ +//@ check-pass +//@ignore-test + +// Currently ignored due to self reborrowing not being implemented for Pin + +#![feature(pin_ergonomics)] +#![allow(incomplete_features)] + +use std::pin::Pin; + +struct Foo; + +impl Foo { + fn foo(self: Pin<&mut Self>) { + } +} + +fn bar(x: Pin<&mut Foo>) { + x.foo(); + x.foo(); // for this to work we need to automatically reborrow, + // as if the user had written `x.as_mut().foo()`. +} + +fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-pin_ergonomics.rs b/tests/ui/feature-gates/feature-gate-pin_ergonomics.rs new file mode 100644 index 0000000000000..d694531d53abe --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-pin_ergonomics.rs @@ -0,0 +1,15 @@ +#![allow(dead_code, incomplete_features)] + +use std::pin::Pin; + +struct Foo; + +fn foo(_: Pin<&mut Foo>) { +} + +fn bar(mut x: Pin<&mut Foo>) { + foo(x); + foo(x); //~ ERROR use of moved value: `x` +} + +fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-pin_ergonomics.stderr b/tests/ui/feature-gates/feature-gate-pin_ergonomics.stderr new file mode 100644 index 0000000000000..6c9029d817674 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-pin_ergonomics.stderr @@ -0,0 +1,21 @@ +error[E0382]: use of moved value: `x` + --> $DIR/feature-gate-pin_ergonomics.rs:12:9 + | +LL | fn bar(mut x: Pin<&mut Foo>) { + | ----- move occurs because `x` has type `Pin<&mut Foo>`, which does not implement the `Copy` trait +LL | foo(x); + | - value moved here +LL | foo(x); + | ^ value used here after move + | +note: consider changing this parameter type in function `foo` to borrow instead if owning the value isn't necessary + --> $DIR/feature-gate-pin_ergonomics.rs:7:11 + | +LL | fn foo(_: Pin<&mut Foo>) { + | --- ^^^^^^^^^^^^^ this parameter takes ownership of the value + | | + | in this function + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0382`. From a73c8b1171be7af6c2e56d6a8c01b4d5b4eb61cb Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Wed, 18 Sep 2024 15:37:50 -0700 Subject: [PATCH 2/5] Apply code review suggestions --- compiler/rustc_hir_typeck/src/coercion.rs | 16 ++++++++-------- .../rustc_hir_typeck/src/expr_use_visitor.rs | 2 -- compiler/rustc_middle/src/ty/adjustment.rs | 3 ++- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 3da4b2893d9c3..d548df98bf99c 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -216,7 +216,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { } ty::Adt(pin, _) if self.tcx.features().pin_ergonomics - && pin.did() == self.tcx.lang_items().pin_type().unwrap() => + && self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => { return self.coerce_pin(a, b); } @@ -796,29 +796,29 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // Then we will build a ReborrowPin adjustment and return that as an InferOk. // Right now we can only reborrow if this is a `Pin<&mut T>`. - let can_reborrow = |ty: Ty<'tcx>| { + let extract_pin_mut = |ty: Ty<'tcx>| { // Get the T out of Pin let ty = match ty.kind() { - ty::Adt(pin, args) if pin.did() == self.tcx.lang_items().pin_type().unwrap() => { + ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => { args[0].expect_ty() } _ => { debug!("can't reborrow {:?} as pinned", ty); - return None; + return Err(TypeError::Mismatch); } }; // Make sure the T is something we understand (just `&mut U` for now) match ty.kind() { - ty::Ref(region, ty, ty::Mutability::Mut) => Some((*region, *ty)), + ty::Ref(region, ty, ty::Mutability::Mut) => Ok((*region, *ty)), _ => { debug!("can't reborrow pin of inner type {:?}", ty); - None + Err(TypeError::Mismatch) } } }; - let (_, _a_ty) = can_reborrow(a).ok_or(TypeError::Mismatch)?; - let (b_region, _b_ty) = can_reborrow(b).ok_or(TypeError::Mismatch)?; + let (_, _a_ty) = extract_pin_mut(a)?; + let (b_region, _b_ty) = extract_pin_mut(b)?; // To complete the reborrow, we need to make sure we can unify the inner types, and if so we // add the adjustments. diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 103a4c72ea858..95a3cf7b96bcb 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -791,8 +791,6 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx adjustment::AutoBorrow::RawPtr(m) => ty::BorrowKind::from_mutbl(*m), }; self.delegate.borrow_mut().borrow(&place_with_id, place_with_id.hir_id, bk); - - self.walk_autoref(expr, &place_with_id, autoref); } } place_with_id = self.cat_expr_adjusted(expr, place_with_id, adjustment)?; diff --git a/compiler/rustc_middle/src/ty/adjustment.rs b/compiler/rustc_middle/src/ty/adjustment.rs index de3619f5b6248..7f56e05901c5f 100644 --- a/compiler/rustc_middle/src/ty/adjustment.rs +++ b/compiler/rustc_middle/src/ty/adjustment.rs @@ -105,7 +105,8 @@ pub enum Adjust<'tcx> { /// Cast into a dyn* object. DynStar, - /// Take a Pin and call either as_mut() or as_ref() to get a Pin<&mut T> or Pin<&T>. + /// Take a `Pin` and call either `as_mut()` or `as_ref()` to get a `Pin<&mut T>` or + /// `Pin<&T>`. ReborrowPin(AutoBorrow<'tcx>), } From b2b76fb70659f55d5e1842b28c7d6c451ae916d9 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 19 Sep 2024 11:18:00 -0700 Subject: [PATCH 3/5] Allow shortening reborrows Generating a call to `as_mut()` let to more restrictive borrows than what reborrowing usually gives us. Instead, we change the desugaring to reborrow the pin internals directly which makes things more expressive. --- compiler/rustc_hir/src/lang_items.rs | 1 - compiler/rustc_hir_typeck/src/coercion.rs | 5 +- .../rustc_hir_typeck/src/expr_use_visitor.rs | 12 +-- compiler/rustc_middle/src/ty/adjustment.rs | 5 +- compiler/rustc_mir_build/src/thir/cx/expr.rs | 100 ++++++++++-------- compiler/rustc_span/src/symbol.rs | 1 - library/core/src/pin.rs | 1 - tests/ui/async-await/pin-reborrow-shorter.rs | 14 +++ 8 files changed, 79 insertions(+), 60 deletions(-) create mode 100644 tests/ui/async-await/pin-reborrow-shorter.rs diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 60e024068e916..e7398fd222636 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -395,7 +395,6 @@ language_item_table! { IteratorNext, sym::next, next_fn, Target::Method(MethodKind::Trait { body: false}), GenericRequirement::None; PinNewUnchecked, sym::new_unchecked, new_unchecked_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None; - PinAsMut, sym::pin_as_mut, as_mut_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None; RangeFrom, sym::RangeFrom, range_from_struct, Target::Struct, GenericRequirement::None; RangeFull, sym::RangeFull, range_full_struct, Target::Struct, GenericRequirement::None; diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index d548df98bf99c..d45d7f54ed4a9 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -824,10 +824,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // add the adjustments. self.unify_and(a, b, |_inner_ty| { vec![Adjustment { - kind: Adjust::ReborrowPin(AutoBorrow::Ref( - b_region, - AutoBorrowMutability::Mut { allow_two_phase_borrow: AllowTwoPhase::No }, - )), + kind: Adjust::ReborrowPin(b_region, hir::Mutability::Mut), target: b, }] }) diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 95a3cf7b96bcb..3b2ddf659a141 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -781,14 +781,12 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx self.walk_autoref(expr, &place_with_id, autoref); } - adjustment::Adjust::ReborrowPin(ref autoref) => { + adjustment::Adjust::ReborrowPin(_, mutbl) => { // Reborrowing a Pin is like a combinations of a deref and a borrow, so we do // both. - let bk = match autoref { - adjustment::AutoBorrow::Ref(_, m) => { - ty::BorrowKind::from_mutbl((*m).into()) - } - adjustment::AutoBorrow::RawPtr(m) => ty::BorrowKind::from_mutbl(*m), + let bk = match mutbl { + ty::Mutability::Not => ty::BorrowKind::ImmBorrow, + ty::Mutability::Mut => ty::BorrowKind::MutBorrow, }; self.delegate.borrow_mut().borrow(&place_with_id, place_with_id.hir_id, bk); } @@ -1296,7 +1294,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) | adjustment::Adjust::Borrow(_) - | adjustment::Adjust::ReborrowPin(_) + | adjustment::Adjust::ReborrowPin(..) | adjustment::Adjust::DynStar => { // Result is an rvalue. Ok(self.cat_rvalue(expr.hir_id, target)) diff --git a/compiler/rustc_middle/src/ty/adjustment.rs b/compiler/rustc_middle/src/ty/adjustment.rs index 7f56e05901c5f..5a32078760e5b 100644 --- a/compiler/rustc_middle/src/ty/adjustment.rs +++ b/compiler/rustc_middle/src/ty/adjustment.rs @@ -105,9 +105,8 @@ pub enum Adjust<'tcx> { /// Cast into a dyn* object. DynStar, - /// Take a `Pin` and call either `as_mut()` or `as_ref()` to get a `Pin<&mut T>` or - /// `Pin<&T>`. - ReborrowPin(AutoBorrow<'tcx>), + /// Take a pinned reference and reborrow as a `Pin<&mut T>` or `Pin<&T>`. + ReborrowPin(ty::Region<'tcx>, hir::Mutability), } /// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)` diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 8e6e13f94e0e1..3bb00e0b028af 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -147,50 +147,64 @@ impl<'tcx> Cx<'tcx> { ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) } } Adjust::DynStar => ExprKind::Cast { source: self.thir.exprs.push(expr) }, - Adjust::ReborrowPin(AutoBorrow::Ref(region, m)) => { + Adjust::ReborrowPin(region, mutbl) => { debug!("apply ReborrowPin adjustment"); - match m { - AutoBorrowMutability::Mut { .. } => { - // Rewrite `$expr` as `Pin::as_mut(&mut $expr)` - let as_mut_method = - self.tcx().require_lang_item(rustc_hir::LangItem::PinAsMut, Some(span)); - let pin_ty_args = match expr.ty.kind() { - ty::Adt(_, args) => args, - _ => bug!("ReborrowPin with non-Pin type"), - }; - let as_mut_ty = - Ty::new_fn_def(self.tcx, as_mut_method, pin_ty_args.into_iter()); - - let ty = Ty::new_ref(self.tcx, region, expr.ty, ty::Mutability::Mut); - let arg = ExprKind::Borrow { - borrow_kind: BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, - arg: self.thir.exprs.push(expr), - }; - debug!(?arg, "borrow arg"); - let arg = self.thir.exprs.push(Expr { temp_lifetime, ty, span, kind: arg }); - - let kind = ExprKind::Call { - ty: as_mut_ty, - fun: self.thir.exprs.push(Expr { - temp_lifetime, - ty: as_mut_ty, - span, - kind: ExprKind::ZstLiteral { user_ty: None }, - }), - args: Box::new([arg]), - from_hir_call: true, - fn_span: span, - }; - debug!(?kind); - kind - } - AutoBorrowMutability::Not => { - // FIXME: We need to call Pin::as_ref on the expression - bug!("ReborrowPin with shared reference is not implemented yet") - } - } + // Rewrite `$expr` as `Pin { __pointer: &(mut)? *($expr).__pointer }` + + // We'll need these types later on + let pin_ty_args = match expr.ty.kind() { + ty::Adt(_, args) => args, + _ => bug!("ReborrowPin with non-Pin type"), + }; + let pin_ty = pin_ty_args.iter().next().unwrap().expect_ty(); + let ptr_target_ty = match pin_ty.kind() { + ty::Ref(_, ty, _) => *ty, + _ => bug!("ReborrowPin with non-Ref type"), + }; + + // pointer = ($expr).__pointer + let pointer_target = ExprKind::Field { + lhs: self.thir.exprs.push(expr), + variant_index: FIRST_VARIANT, + name: FieldIdx::from(0u32), + }; + let arg = Expr { temp_lifetime, ty: pin_ty, span, kind: pointer_target }; + let arg = self.thir.exprs.push(arg); + + // arg = *pointer + let expr = ExprKind::Deref { arg }; + let arg = self.thir.exprs.push(Expr { + temp_lifetime, + ty: ptr_target_ty, + span, + kind: expr, + }); + + // expr = &mut target + let expr = self.thir.exprs.push(Expr { + temp_lifetime, + ty: Ty::new_ref(self.tcx, region, ptr_target_ty, mutbl), + span, + kind: ExprKind::Borrow { + borrow_kind: BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, + arg, + }, + }); + + // kind = Pin { __pointer: pointer } + let pin_did = self.tcx.require_lang_item(rustc_hir::LangItem::Pin, Some(span)); + let kind = ExprKind::Adt(Box::new(AdtExpr { + adt_def: self.tcx.adt_def(pin_did), + variant_index: FIRST_VARIANT, + args: pin_ty_args, + fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]), + user_ty: None, + base: None, + })); + + debug!(?kind); + kind } - Adjust::ReborrowPin(AutoBorrow::RawPtr(_)) => bug!("ReborrowPin with raw pointer"), }; Expr { temp_lifetime, ty: adjustment.target, span, kind } @@ -1059,7 +1073,7 @@ impl<'tcx> Cx<'tcx> { // Reconstruct the output assuming it's a reference with the // same region and mutability as the receiver. This holds for - // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`. + // `Deref(Mut)::deref(_mut)` and `Index(Mut)::index(_mut)`. let ty::Ref(region, _, mutbl) = *self.thir[args[0]].ty.kind() else { span_bug!(span, "overloaded_place: receiver is not a reference"); }; diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 979294563d735..27ff9afa92c25 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1418,7 +1418,6 @@ symbols! { pic, pie, pin, - pin_as_mut, pin_ergonomics, platform_intrinsics, plugin, diff --git a/library/core/src/pin.rs b/library/core/src/pin.rs index bb1196e82de78..9c13662e08e8f 100644 --- a/library/core/src/pin.rs +++ b/library/core/src/pin.rs @@ -1408,7 +1408,6 @@ impl Pin { /// } /// } /// ``` - #[cfg_attr(not(bootstrap), lang = "pin_as_mut")] #[stable(feature = "pin", since = "1.33.0")] #[inline(always)] pub fn as_mut(&mut self) -> Pin<&mut Ptr::Target> { diff --git a/tests/ui/async-await/pin-reborrow-shorter.rs b/tests/ui/async-await/pin-reborrow-shorter.rs new file mode 100644 index 0000000000000..06c266e0035fb --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-shorter.rs @@ -0,0 +1,14 @@ +//@check-pass + +#![feature(pin_ergonomics)] +#![allow(dead_code, incomplete_features)] + +use std::pin::Pin; + +fn shorter<'b, T: 'b>(_: Pin<&'b mut T>) {} + +fn test<'a: 'b, 'b, T: 'a>(x: Pin<&'a mut T>) { + shorter::<'b>(x); +} + +fn main() {} From 92a5d21bc4a0b735750a4dff1eac204ce636b513 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 19 Sep 2024 15:29:01 -0700 Subject: [PATCH 4/5] Add a test case to make sure we don't reborrow twice --- tests/ui/async-await/pin-reborrow-arg.rs | 2 +- tests/ui/async-await/pin-reborrow-once.rs | 13 +++++++++++++ tests/ui/async-await/pin-reborrow-once.stderr | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/ui/async-await/pin-reborrow-once.rs create mode 100644 tests/ui/async-await/pin-reborrow-once.stderr diff --git a/tests/ui/async-await/pin-reborrow-arg.rs b/tests/ui/async-await/pin-reborrow-arg.rs index 34f23533b65b2..b3e8718d036b4 100644 --- a/tests/ui/async-await/pin-reborrow-arg.rs +++ b/tests/ui/async-await/pin-reborrow-arg.rs @@ -15,7 +15,7 @@ impl Foo { fn foo(_: Pin<&mut Foo>) { } -fn bar(mut x: Pin<&mut Foo>) { +fn bar(x: Pin<&mut Foo>) { foo(x); foo(x); // for this to work we need to automatically reborrow, // as if the user had written `foo(x.as_mut())`. diff --git a/tests/ui/async-await/pin-reborrow-once.rs b/tests/ui/async-await/pin-reborrow-once.rs new file mode 100644 index 0000000000000..241efadef7d57 --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-once.rs @@ -0,0 +1,13 @@ +#![feature(pin_ergonomics)] +#![allow(dead_code, incomplete_features)] + +// Make sure with pin reborrowing that we can only get one mutable reborrow of a pinned reference. + +use std::pin::{pin, Pin}; + +fn twice(_: Pin<&mut i32>, _: Pin<&mut i32>) {} + +fn main() { + let x = pin!(42); + twice(x, x); //~ ERROR cannot borrow +} diff --git a/tests/ui/async-await/pin-reborrow-once.stderr b/tests/ui/async-await/pin-reborrow-once.stderr new file mode 100644 index 0000000000000..b8fde8ffee898 --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-once.stderr @@ -0,0 +1,12 @@ +error[E0499]: cannot borrow `*x.__pointer` as mutable more than once at a time + --> $DIR/pin-reborrow-once.rs:12:14 + | +LL | twice(x, x); + | ----- - ^ second mutable borrow occurs here + | | | + | | first mutable borrow occurs here + | first borrow later used by call + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0499`. From a18800f8078f3fb7b986183b53c34a6a1b921d63 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 19 Sep 2024 16:27:09 -0700 Subject: [PATCH 5/5] pin_ergonomics: allow reborrowing as Pin<&T> --- compiler/rustc_hir_typeck/src/coercion.rs | 24 ++++++++++++------- compiler/rustc_mir_build/src/thir/cx/expr.rs | 15 +++++++----- tests/ui/async-await/pin-reborrow-arg.rs | 9 +++++++ .../async-await/pin-reborrow-const-as-mut.rs | 18 ++++++++++++++ .../pin-reborrow-const-as-mut.stderr | 19 +++++++++++++++ 5 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 tests/ui/async-await/pin-reborrow-const-as-mut.rs create mode 100644 tests/ui/async-await/pin-reborrow-const-as-mut.stderr diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index d45d7f54ed4a9..94b8541075e54 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -798,9 +798,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // Right now we can only reborrow if this is a `Pin<&mut T>`. let extract_pin_mut = |ty: Ty<'tcx>| { // Get the T out of Pin - let ty = match ty.kind() { + let (pin, ty) = match ty.kind() { ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => { - args[0].expect_ty() + (*pin, args[0].expect_ty()) } _ => { debug!("can't reborrow {:?} as pinned", ty); @@ -809,7 +809,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { }; // Make sure the T is something we understand (just `&mut U` for now) match ty.kind() { - ty::Ref(region, ty, ty::Mutability::Mut) => Ok((*region, *ty)), + ty::Ref(region, ty, mutbl) => Ok((pin, *region, *ty, *mutbl)), _ => { debug!("can't reborrow pin of inner type {:?}", ty); Err(TypeError::Mismatch) @@ -817,16 +817,22 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { } }; - let (_, _a_ty) = extract_pin_mut(a)?; - let (b_region, _b_ty) = extract_pin_mut(b)?; + let (pin, a_region, a_ty, mut_a) = extract_pin_mut(a)?; + let (_, b_region, _b_ty, mut_b) = extract_pin_mut(b)?; + + coerce_mutbls(mut_a, mut_b)?; + + // update a with b's mutability since we'll be coercing mutability + let a = Ty::new_adt( + self.tcx, + pin, + self.tcx.mk_args(&[Ty::new_ref(self.tcx, a_region, a_ty, mut_b).into()]), + ); // To complete the reborrow, we need to make sure we can unify the inner types, and if so we // add the adjustments. self.unify_and(a, b, |_inner_ty| { - vec![Adjustment { - kind: Adjust::ReborrowPin(b_region, hir::Mutability::Mut), - target: b, - }] + vec![Adjustment { kind: Adjust::ReborrowPin(b_region, mut_b), target: b }] }) } diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 3bb00e0b028af..3f730b5d18306 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -181,22 +181,25 @@ impl<'tcx> Cx<'tcx> { }); // expr = &mut target + let borrow_kind = match mutbl { + hir::Mutability::Mut => BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, + hir::Mutability::Not => BorrowKind::Shared, + }; + let new_pin_target = Ty::new_ref(self.tcx, region, ptr_target_ty, mutbl); let expr = self.thir.exprs.push(Expr { temp_lifetime, - ty: Ty::new_ref(self.tcx, region, ptr_target_ty, mutbl), + ty: new_pin_target, span, - kind: ExprKind::Borrow { - borrow_kind: BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, - arg, - }, + kind: ExprKind::Borrow { borrow_kind, arg }, }); // kind = Pin { __pointer: pointer } let pin_did = self.tcx.require_lang_item(rustc_hir::LangItem::Pin, Some(span)); + let args = self.tcx.mk_args(&[new_pin_target.into()]); let kind = ExprKind::Adt(Box::new(AdtExpr { adt_def: self.tcx.adt_def(pin_did), variant_index: FIRST_VARIANT, - args: pin_ty_args, + args, fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]), user_ty: None, base: None, diff --git a/tests/ui/async-await/pin-reborrow-arg.rs b/tests/ui/async-await/pin-reborrow-arg.rs index b3e8718d036b4..2008bd1f52dd9 100644 --- a/tests/ui/async-await/pin-reborrow-arg.rs +++ b/tests/ui/async-await/pin-reborrow-arg.rs @@ -15,6 +15,9 @@ impl Foo { fn foo(_: Pin<&mut Foo>) { } +fn foo_const(_: Pin<&Foo>) { +} + fn bar(x: Pin<&mut Foo>) { foo(x); foo(x); // for this to work we need to automatically reborrow, @@ -22,6 +25,12 @@ fn bar(x: Pin<&mut Foo>) { Foo::baz(x); Foo::baz(x); + + foo_const(x); // make sure we can reborrow &mut as &. + + let x: Pin<&Foo> = Pin::new(&Foo); + + foo_const(x); // make sure reborrowing from & to & works. } fn main() {} diff --git a/tests/ui/async-await/pin-reborrow-const-as-mut.rs b/tests/ui/async-await/pin-reborrow-const-as-mut.rs new file mode 100644 index 0000000000000..27c70a7b4dfae --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-const-as-mut.rs @@ -0,0 +1,18 @@ +#![feature(pin_ergonomics)] +#![allow(dead_code, incomplete_features)] + +// make sure we can't accidentally reborrow Pin<&T> as Pin<&mut T> + +use std::pin::Pin; + +struct Foo; + +fn foo(_: Pin<&mut Foo>) { +} + +fn bar(x: Pin<&Foo>) { + foo(x); //~ ERROR mismatched types + //| ERROR types differ in mutability +} + +fn main() {} diff --git a/tests/ui/async-await/pin-reborrow-const-as-mut.stderr b/tests/ui/async-await/pin-reborrow-const-as-mut.stderr new file mode 100644 index 0000000000000..2c2d9ec271742 --- /dev/null +++ b/tests/ui/async-await/pin-reborrow-const-as-mut.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/pin-reborrow-const-as-mut.rs:14:9 + | +LL | foo(x); + | --- ^ types differ in mutability + | | + | arguments to this function are incorrect + | + = note: expected struct `Pin<&mut Foo>` + found struct `Pin<&Foo>` +note: function defined here + --> $DIR/pin-reborrow-const-as-mut.rs:10:4 + | +LL | fn foo(_: Pin<&mut Foo>) { + | ^^^ ---------------- + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`.