Skip to content

Commit d3c53aa

Browse files
committed
Allow reborrowing pinned self methods
1 parent 220a6ac commit d3c53aa

File tree

6 files changed

+123
-10
lines changed

6 files changed

+123
-10
lines changed

compiler/rustc_hir_typeck/src/method/confirm.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,18 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
239239
Some(probe::AutorefOrPtrAdjustment::ReborrowPin(mutbl)) => {
240240
let region = self.next_region_var(infer::Autoref(self.span));
241241

242-
adjustments.push(Adjustment {
243-
kind: Adjust::ReborrowPin(region, mutbl),
244-
target,
245-
});
242+
target = match target.kind() {
243+
ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => {
244+
let inner_ty = match args[0].expect_ty().kind() {
245+
ty::Ref(_, ty, _) => *ty,
246+
_ => bug!("Expected a reference type for argument to Pin"),
247+
};
248+
Ty::new_pinned_ref(self.tcx, region, inner_ty, mutbl)
249+
}
250+
_ => bug!("Cannot adjust receiver type for reborrowing pin of {target:?}"),
251+
};
252+
253+
adjustments.push(Adjustment { kind: Adjust::ReborrowPin(region, mutbl), target });
246254
}
247255
None => {}
248256
}

compiler/rustc_hir_typeck/src/method/probe.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,13 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11021102
unstable_candidates.as_deref_mut(),
11031103
)
11041104
})
1105+
.or_else(|| {
1106+
self.pick_reborrow_pin_method(
1107+
step,
1108+
self_ty,
1109+
unstable_candidates.as_deref_mut(),
1110+
)
1111+
})
11051112
})
11061113
})
11071114
}
@@ -1136,7 +1143,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11361143
})
11371144
}
11381145

1139-
ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1146+
ty::Adt(def, args)
1147+
if self.tcx.features().pin_ergonomics
1148+
&& self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) =>
1149+
{
11401150
// make sure this is a pinned reference (and not a `Pin<Box>` or something)
11411151
if let ty::Ref(_, _, mutbl) = args[0].expect_ty().kind() {
11421152
pick.autoref_or_ptr_adjustment =
@@ -1175,6 +1185,43 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11751185
})
11761186
}
11771187

1188+
/// Looks for applicable methods if we reborrow a `Pin<&mut T>` as a `Pin<&T>`.
1189+
#[instrument(level = "debug", skip(self, step, unstable_candidates))]
1190+
fn pick_reborrow_pin_method(
1191+
&self,
1192+
step: &CandidateStep<'tcx>,
1193+
self_ty: Ty<'tcx>,
1194+
unstable_candidates: Option<&mut Vec<(Candidate<'tcx>, Symbol)>>,
1195+
) -> Option<PickResult<'tcx>> {
1196+
if !self.tcx.features().pin_ergonomics {
1197+
return None;
1198+
}
1199+
1200+
// make sure self is a Pin<&mut T>
1201+
let inner_ty = match self_ty.kind() {
1202+
ty::Adt(def, args) if self.tcx.is_lang_item(def.did(), hir::LangItem::Pin) => {
1203+
match args[0].expect_ty().kind() {
1204+
ty::Ref(_, ty, hir::Mutability::Mut) => *ty,
1205+
_ => {
1206+
return None;
1207+
}
1208+
}
1209+
}
1210+
_ => return None,
1211+
};
1212+
1213+
let region = self.tcx.lifetimes.re_erased;
1214+
let autopin_ty = Ty::new_pinned_ref(self.tcx, region, inner_ty, hir::Mutability::Not);
1215+
self.pick_method(autopin_ty, unstable_candidates).map(|r| {
1216+
r.map(|mut pick| {
1217+
pick.autoderefs = step.autoderefs;
1218+
pick.autoref_or_ptr_adjustment =
1219+
Some(AutorefOrPtrAdjustment::ReborrowPin(hir::Mutability::Not));
1220+
pick
1221+
})
1222+
})
1223+
}
1224+
11781225
/// If `self_ty` is `*mut T` then this picks `*const T` methods. The reason why we have a
11791226
/// special case for this is because going from `*mut T` to `*const T` with autoderefs and
11801227
/// autorefs would require dereferencing the pointer, which is not safe.

compiler/rustc_middle/src/ty/sty.rs

+10
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,16 @@ impl<'tcx> Ty<'tcx> {
584584
Ty::new_ref(tcx, r, ty, hir::Mutability::Not)
585585
}
586586

587+
pub fn new_pinned_ref(
588+
tcx: TyCtxt<'tcx>,
589+
r: Region<'tcx>,
590+
ty: Ty<'tcx>,
591+
mutbl: ty::Mutability,
592+
) -> Ty<'tcx> {
593+
let pin = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, None));
594+
Ty::new_adt(tcx, pin, tcx.mk_args(&[Ty::new_ref(tcx, r, ty, mutbl).into()]))
595+
}
596+
587597
#[inline]
588598
pub fn new_ptr(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, mutbl: ty::Mutability) -> Ty<'tcx> {
589599
Ty::new(tcx, ty::RawPtr(ty, mutbl))

tests/ui/async-await/pin-reborrow-self.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ pub fn bar(x: Pin<&mut Foo>) {
2222

2323
Foo::baz(x);
2424

25-
// FIXME: We should allow downgrading a Pin<&mut T> to Pin<&T>
26-
// x.baz();
25+
x.baz();
2726
}
2827

2928
pub fn baaz(x: Pin<&Foo>) {

tests/ui/feature-gates/feature-gate-pin_ergonomics.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ use std::pin::Pin;
44

55
struct Foo;
66

7+
impl Foo {
8+
fn foo(self: Pin<&mut Self>) {
9+
}
10+
}
11+
712
fn foo(_: Pin<&mut Foo>) {
813
}
914

1015
fn bar(mut x: Pin<&mut Foo>) {
1116
foo(x);
1217
foo(x); //~ ERROR use of moved value: `x`
18+
19+
x.foo(); //~ ERROR use of moved value: `x`
20+
x.foo(); //~ ERROR use of moved value: `x`
1321
}
1422

1523
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: use of moved value: `x`
2-
--> $DIR/feature-gate-pin_ergonomics.rs:12:9
2+
--> $DIR/feature-gate-pin_ergonomics.rs:17:9
33
|
44
LL | fn bar(mut x: Pin<&mut Foo>) {
55
| ----- move occurs because `x` has type `Pin<&mut Foo>`, which does not implement the `Copy` trait
@@ -9,13 +9,54 @@ LL | foo(x);
99
| ^ value used here after move
1010
|
1111
note: consider changing this parameter type in function `foo` to borrow instead if owning the value isn't necessary
12-
--> $DIR/feature-gate-pin_ergonomics.rs:7:11
12+
--> $DIR/feature-gate-pin_ergonomics.rs:12:11
1313
|
1414
LL | fn foo(_: Pin<&mut Foo>) {
1515
| --- ^^^^^^^^^^^^^ this parameter takes ownership of the value
1616
| |
1717
| in this function
1818

19-
error: aborting due to 1 previous error
19+
error[E0382]: use of moved value: `x`
20+
--> $DIR/feature-gate-pin_ergonomics.rs:19:5
21+
|
22+
LL | fn bar(mut x: Pin<&mut Foo>) {
23+
| ----- move occurs because `x` has type `Pin<&mut Foo>`, which does not implement the `Copy` trait
24+
LL | foo(x);
25+
LL | foo(x);
26+
| - value moved here
27+
LL |
28+
LL | x.foo();
29+
| ^ value used here after move
30+
|
31+
note: consider changing this parameter type in function `foo` to borrow instead if owning the value isn't necessary
32+
--> $DIR/feature-gate-pin_ergonomics.rs:12:11
33+
|
34+
LL | fn foo(_: Pin<&mut Foo>) {
35+
| --- ^^^^^^^^^^^^^ this parameter takes ownership of the value
36+
| |
37+
| in this function
38+
39+
error[E0382]: use of moved value: `x`
40+
--> $DIR/feature-gate-pin_ergonomics.rs:20:5
41+
|
42+
LL | fn bar(mut x: Pin<&mut Foo>) {
43+
| ----- move occurs because `x` has type `Pin<&mut Foo>`, which does not implement the `Copy` trait
44+
...
45+
LL | x.foo();
46+
| ----- `x` moved due to this method call
47+
LL | x.foo();
48+
| ^ value used here after move
49+
|
50+
note: `Foo::foo` takes ownership of the receiver `self`, which moves `x`
51+
--> $DIR/feature-gate-pin_ergonomics.rs:8:12
52+
|
53+
LL | fn foo(self: Pin<&mut Self>) {
54+
| ^^^^
55+
help: consider reborrowing the `Pin` instead of moving it
56+
|
57+
LL | x.as_mut().foo();
58+
| +++++++++
59+
60+
error: aborting due to 3 previous errors
2061

2162
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)