-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Implement coercions between &pin (mut|const) T and &(mut) T when T: Unpin
#149130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,12 +269,21 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { | |
| return self.coerce_to_raw_ptr(a, b, b_mutbl); | ||
| } | ||
| ty::Ref(r_b, _, mutbl_b) => { | ||
| if self.tcx.features().pin_ergonomics() | ||
| && a.pinned_ty().is_some_and(|ty| ty.is_ref()) | ||
| && let Ok(coerce) = self.commit_if_ok(|_| self.coerce_maybe_pinned_ref(a, b)) | ||
| { | ||
| return Ok(coerce); | ||
| } | ||
| return self.coerce_to_ref(a, b, r_b, mutbl_b); | ||
| } | ||
| ty::Adt(pin, _) | ||
| if self.tcx.features().pin_ergonomics() | ||
| && self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => | ||
| { | ||
| if a.is_ref() && b.pinned_ty().is_some_and(|ty| ty.is_ref()) { | ||
| return self.coerce_maybe_pinned_ref(a, b); | ||
| } | ||
| let pin_coerce = self.commit_if_ok(|_| self.coerce_to_pin_ref(a, b)); | ||
| if pin_coerce.is_ok() { | ||
| return pin_coerce; | ||
|
|
@@ -847,6 +856,62 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { | |
| self.unify_and(a, b, [], Adjust::ReborrowPin(mut_b), ForceLeakCheck::No) | ||
| } | ||
|
|
||
| /// Coerce pinned reference to regular reference or vice versa | ||
| /// | ||
| /// - `Pin<&mut T>` <-> `&mut T` when `T: Unpin` | ||
| /// - `Pin<&T>` <-> `&T` when `T: Unpin` | ||
| /// - `Pin<&mut T>` <-> `Pin<&T>` when `T: Unpin` | ||
| #[instrument(skip(self), level = "trace")] | ||
| fn coerce_maybe_pinned_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> { | ||
| let span = self.cause.span; | ||
| let Some((a_ty, a_pinnedness, a_mutbl, a_region)) = a.maybe_pinned_ref() else { | ||
frank-king marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| span_bug!(span, "expect pinned reference or reference, found {:?}", a); | ||
| }; | ||
| let Some((_b_ty, b_pinnedness, b_mutbl, _b_region)) = b.maybe_pinned_ref() else { | ||
| span_bug!(span, "expect pinned reference or reference, found {:?}", b); | ||
| }; | ||
|
||
| use ty::Pinnedness::*; | ||
| if a_pinnedness == b_pinnedness { | ||
| span_bug!(span, "expect different pinnedness, found {:?} and {:?}", a, b); | ||
| } | ||
frank-king marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| coerce_mutbls(a_mutbl, b_mutbl)?; | ||
|
|
||
| let (deref, borrow) = match (a_pinnedness, b_pinnedness) { | ||
| (Not, Not) | (Pinned, Pinned) => { | ||
| span_bug!(span, "expect different pinnedness, found {:?} and {:?}", a, b) | ||
| } | ||
| (Pinned, Not) => { | ||
| let mutbl = AutoBorrowMutability::new(b_mutbl, AllowTwoPhase::Yes); | ||
| (DerefAdjustKind::Pin, AutoBorrow::Ref(mutbl)) | ||
| } | ||
| (Not, Pinned) => (DerefAdjustKind::Builtin, AutoBorrow::Pin(b_mutbl)), | ||
| }; | ||
| let mut coerce = self.unify_and( | ||
| // update a with b's pinnedness and mutability since we'll be coercing pinnedness and mutability | ||
| match b_pinnedness { | ||
| Pinned => Ty::new_pinned_ref(self.tcx, a_region, a_ty, b_mutbl), | ||
| Not => Ty::new_ref(self.tcx, a_region, a_ty, b_mutbl), | ||
| }, | ||
| b, | ||
| [Adjustment { kind: Adjust::Deref(deref), target: a_ty }], | ||
| Adjust::Borrow(borrow), | ||
| ForceLeakCheck::No, | ||
| )?; | ||
|
|
||
| // Create an obligation for `a_ty: Unpin`. | ||
| let cause = | ||
| self.cause(self.cause.span, ObligationCauseCode::Coercion { source: a, target: b }); | ||
| let pred = ty::TraitRef::new( | ||
| self.tcx, | ||
| self.tcx.require_lang_item(hir::LangItem::Unpin, self.cause.span), | ||
| [a_ty], | ||
| ); | ||
| let obligation = Obligation::new(self.tcx, cause, self.fcx.param_env, pred); | ||
| coerce.obligations.push(obligation); | ||
| Ok(coerce) | ||
| } | ||
|
|
||
| fn coerce_from_fn_pointer( | ||
| &self, | ||
| a: Ty<'tcx>, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sort of thinking that this code (and the lines added under the
ty::Adtarm, should go incoerce_to_refandcoerce_to_pin_refrespectively - keeping this function relatively clean.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also would help to eliminate some
span_bugs fromcoerce_maybe_pinned_ref