Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum AutoderefKind {
Builtin,
/// A type which must dispatch to a `Deref` implementation.
Overloaded,
/// A pinned reference type, such as `Pin<&T>` and `Pin<&mut T>`.
Pin,
}
struct AutoderefSnapshot<'tcx> {
at_start: bool,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_hir_typeck/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use itertools::Itertools;
use rustc_hir_analysis::autoderef::{Autoderef, AutoderefKind};
use rustc_infer::infer::InferOk;
use rustc_infer::traits::PredicateObligations;
use rustc_middle::bug;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, DerefAdjustKind, OverloadedDeref};
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
Expand Down Expand Up @@ -62,6 +63,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
})
.unwrap_or(DerefAdjustKind::Builtin)
}
AutoderefKind::Pin => {
bug!("Pin autoderef kind should not be present in the steps")
}
AutoderefKind::Builtin => DerefAdjustKind::Builtin,
})
.zip_eq(targets)
Expand Down
73 changes: 72 additions & 1 deletion compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -844,7 +853,69 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {

// 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, [], Adjust::ReborrowPin(mut_b), ForceLeakCheck::No)
self.unify_and(
a,
b,
[Adjustment { kind: Adjust::Deref(DerefAdjustKind::Pin), target: a_ty }],
Adjust::Borrow(AutoBorrow::Pin(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 {
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);
}

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(
Expand Down
25 changes: 12 additions & 13 deletions compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
}

adjustment::Adjust::Deref(DerefAdjustKind::Builtin) => {}
adjustment::Adjust::Deref(DerefAdjustKind::Builtin | DerefAdjustKind::Pin) => {}

// Autoderefs for overloaded Deref calls in fact reference
// their receiver. That is, if we have `(*x)` where `x`
Expand All @@ -749,16 +749,6 @@ 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(mutbl) => {
// Reborrowing a Pin is like a combinations of a deref and a borrow, so we do
// both.
let bk = match mutbl {
ty::Mutability::Not => ty::BorrowKind::Immutable,
ty::Mutability::Mut => ty::BorrowKind::Mutable,
};
self.delegate.borrow_mut().borrow(&place_with_id, place_with_id.hir_id, bk);
}
}
place_with_id = self.cat_expr_adjusted(expr, place_with_id, adjustment)?;
}
Expand Down Expand Up @@ -798,6 +788,16 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
ty::BorrowKind::from_mutbl(m),
);
}

adjustment::AutoBorrow::Pin(m) => {
debug!("walk_autoref: expr.hir_id={} base_place={:?}", expr.hir_id, base_place);

self.delegate.borrow_mut().borrow(
base_place,
base_place.hir_id,
ty::BorrowKind::from_mutbl(m),
);
}
}
}

Expand Down Expand Up @@ -1291,8 +1291,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::Borrow(_) => {
// Result is an rvalue.
Ok(self.cat_rvalue(expr.hir_id, target))
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Adjust::Deref(DerefAdjustKind::Builtin) => {
// FIXME(const_trait_impl): We *could* enforce `&T: [const] Deref` here.
}
Adjust::Deref(DerefAdjustKind::Pin) => {
// FIXME(const_trait_impl): We *could* enforce `Pin<&T>: [const] Deref` here.
}
Adjust::Pointer(_pointer_coercion) => {
// FIXME(const_trait_impl): We should probably enforce these.
}
Adjust::ReborrowPin(_mutability) => {
// FIXME(const_trait_impl): We could enforce these; they correspond to
// `&mut T: DerefMut` tho, so it's kinda moot.
}
Adjust::Borrow(_) => {
// No effects to enforce here.
}
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use rustc_lint::builtin::{
};
use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::adjustment::{
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
PointerCoercion,
};
use rustc_middle::ty::{
self, AssocContainer, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt,
Expand Down Expand Up @@ -243,12 +244,16 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
ty::Ref(_, ty, _) => *ty,
_ => bug!("Expected a reference type for argument to Pin"),
};
adjustments.push(Adjustment {
kind: Adjust::Deref(DerefAdjustKind::Pin),
target: inner_ty,
});
Ty::new_pinned_ref(self.tcx, region, inner_ty, mutbl)
}
_ => bug!("Cannot adjust receiver type for reborrowing pin of {target:?}"),
};

adjustments.push(Adjustment { kind: Adjust::ReborrowPin(mutbl), target });
adjustments
.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Pin(mutbl)), target });
}
None => {}
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_lint/src/autorefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<(Muta
&Adjust::Borrow(AutoBorrow::Ref(mutbl)) => Some((mutbl.into(), false)),
Adjust::NeverToAny
| Adjust::Pointer(..)
| Adjust::ReborrowPin(..)
| Adjust::Deref(DerefAdjustKind::Builtin)
| Adjust::Borrow(AutoBorrow::RawPtr(..)) => None,
| Adjust::Deref(DerefAdjustKind::Builtin | DerefAdjustKind::Pin)
| Adjust::Borrow(AutoBorrow::RawPtr(..) | AutoBorrow::Pin(..)) => None,
}
}
7 changes: 4 additions & 3 deletions compiler/rustc_middle/src/ty/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ pub enum Adjust {
Borrow(AutoBorrow),

Pointer(PointerCoercion),

/// Take a pinned reference and reborrow as a `Pin<&mut T>` or `Pin<&T>`.
ReborrowPin(hir::Mutability),
}

#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub enum DerefAdjustKind {
Builtin,
Overloaded(OverloadedDeref),
Pin,
}

/// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`
Expand Down Expand Up @@ -196,6 +194,9 @@ pub enum AutoBorrow {

/// Converts from T to *T.
RawPtr(hir::Mutability),

/// Converts from T to Pin<&T>.
Pin(hir::Mutability),
}

/// Information for `CoerceUnsized` impls, storing information we
Expand Down
54 changes: 18 additions & 36 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ impl<'tcx> ThirBuildCx<'tcx> {
adjust_span(&mut expr);
ExprKind::Deref { arg: self.thir.exprs.push(expr) }
}
Adjust::Deref(DerefAdjustKind::Pin) => {
adjust_span(&mut expr);
// pointer = ($expr).pointer
let pin_ty = expr.ty.pinned_ty().expect("Deref(Pin) with non-Pin type");
let pointer_target = ExprKind::Field {
lhs: self.thir.exprs.push(expr),
variant_index: FIRST_VARIANT,
name: FieldIdx::ZERO,
};
let expr = Expr { temp_scope_id, ty: pin_ty, span, kind: pointer_target };
// expr = *pointer
ExprKind::Deref { arg: self.thir.exprs.push(expr) }
}
Adjust::Deref(DerefAdjustKind::Overloaded(deref)) => {
// We don't need to do call adjust_span here since
// deref coercions always start with a built-in deref.
Expand Down Expand Up @@ -178,54 +191,23 @@ impl<'tcx> ThirBuildCx<'tcx> {
Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) }
}
Adjust::ReborrowPin(mutbl) => {
debug!("apply ReborrowPin adjustment");
// 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::ZERO,
};
let arg = Expr { temp_scope_id, 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_scope_id,
ty: ptr_target_ty,
span,
kind: expr,
});

// expr = &mut target
Adjust::Borrow(AutoBorrow::Pin(mutbl)) => {
// expr = &pin (mut|const|) arget
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, self.tcx.lifetimes.re_erased, ptr_target_ty, mutbl);
Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, expr.ty, mutbl);
let arg = self.thir.exprs.push(expr);
let expr = self.thir.exprs.push(Expr {
temp_scope_id,
ty: new_pin_target,
span,
kind: ExprKind::Borrow { borrow_kind, arg },
});

// kind = Pin { __pointer: pointer }
// kind = Pin { pointer }
let pin_did = self.tcx.require_lang_item(rustc_hir::LangItem::Pin, span);
let args = self.tcx.mk_args(&[new_pin_target.into()]);
let kind = ExprKind::Adt(Box::new(AdtExpr {
Expand Down
Loading
Loading