Skip to content

Commit 9b6f3cd

Browse files
committed
change type of async_drop_in_place to accept &mut _
1 parent 1e3dc21 commit 9b6f3cd

8 files changed

Lines changed: 37 additions & 55 deletions

File tree

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ where
354354
let ty::Adt(adt_def, adt_args) = pin_obj_ty.kind() else {
355355
bug!();
356356
};
357-
let obj_ptr_ty = Ty::new_mut_ptr(tcx, drop_ty);
358357
let unwrap_ty = adt_def.non_enum_variant().fields[FieldIdx::ZERO].ty(tcx, adt_args);
359358
let obj_ref_place = Place::from(self.new_temp(unwrap_ty));
360359
call_statements.push(self.assign(
@@ -366,11 +365,7 @@ where
366365
))),
367366
));
368367

369-
let obj_ptr_place = Place::from(self.new_temp(obj_ptr_ty));
370-
371-
let addr = Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_deref(obj_ref_place));
372-
call_statements.push(self.assign(obj_ptr_place, addr));
373-
obj_ptr_place
368+
obj_ref_place
374369
};
375370
call_statements
376371
.push(Statement::new(self.source_info, StatementKind::StorageLive(fut.local)));

compiler/rustc_mir_transform/src/shim/async_destructor_ctor.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(super) fn build_async_drop_shim<'tcx>(
5151
let typing_env = ty::TypingEnv::fully_monomorphized();
5252

5353
let drop_ty = parent_args.first().unwrap().expect_ty();
54-
let drop_ptr_ty = Ty::new_mut_ptr(tcx, drop_ty);
54+
let drop_ptr_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, drop_ty);
5555

5656
assert!(tcx.is_coroutine(def_id));
5757
let coroutine_kind = tcx.coroutine_kind(def_id).unwrap();
@@ -209,7 +209,7 @@ fn build_adrop_for_coroutine_shim<'tcx>(
209209
let source_info = SourceInfo::outermost(span);
210210
// converting `(_1: Pin<&mut CorLayout>, _2: &mut Context<'_>) -> Poll<()>`
211211
// into `(_1: Pin<&mut ProxyLayout>, _2: &mut Context<'_>) -> Poll<()>`
212-
// let mut _x: &mut CorLayout = &*_1.0.0;
212+
// let mut _x: &mut CorLayout = &mut *_1.0.0;
213213
// Replace old _1.0 accesses into _x accesses;
214214
let body = tcx.optimized_mir(*coroutine_def_id).future_drop_poll().unwrap();
215215
let mut body: Body<'tcx> = EarlyBinder::bind(body.clone()).instantiate(tcx, impl_args);
@@ -231,7 +231,7 @@ fn build_adrop_for_coroutine_shim<'tcx>(
231231

232232
{
233233
let mut idx: usize = 0;
234-
// _proxy = _1.0 : Pin<&ProxyLayout> ==> &ProxyLayout
234+
// _proxy = _1.0 : Pin<&mut ProxyLayout> ==> &mut ProxyLayout
235235
let proxy_ref_place = Place::from(pin_proxy_layout_local)
236236
.project_deeper(&[PlaceElem::Field(FieldIdx::ZERO, proxy_ref)], tcx);
237237
body.basic_blocks_mut()[START_BLOCK].statements.insert(
@@ -245,22 +245,23 @@ fn build_adrop_for_coroutine_shim<'tcx>(
245245
),
246246
);
247247
idx += 1;
248-
let mut cor_ptr_local = proxy_ref_local;
248+
249+
// _cor_ref_tmp = (*(*_proxy).0).0...
250+
let mut cor_ref_tmp_local = proxy_ref_local;
249251
proxy_ty.find_async_drop_impl_coroutine(tcx, |ty| {
250252
if ty != proxy_ty {
251-
let ty_ptr = Ty::new_mut_ptr(tcx, ty);
252-
let impl_ptr_place = Place::from(cor_ptr_local).project_deeper(
253-
&[PlaceElem::Deref, PlaceElem::Field(FieldIdx::ZERO, ty_ptr)],
253+
let ty_ref = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
254+
let impl_ptr_place = Place::from(cor_ref_tmp_local).project_deeper(
255+
&[PlaceElem::Deref, PlaceElem::Field(FieldIdx::ZERO, ty_ref)],
254256
tcx,
255257
);
256-
cor_ptr_local = body.local_decls.push(LocalDecl::new(ty_ptr, span));
257-
// _cor_ptr = _proxy.0.0 (... .0)
258+
cor_ref_tmp_local = body.local_decls.push(LocalDecl::new(ty_ref, span));
258259
body.basic_blocks_mut()[START_BLOCK].statements.insert(
259260
idx,
260261
Statement::new(
261262
source_info,
262263
StatementKind::Assign(Box::new((
263-
Place::from(cor_ptr_local),
264+
Place::from(cor_ref_tmp_local),
264265
Rvalue::Use(Operand::Copy(impl_ptr_place)),
265266
))),
266267
),
@@ -269,17 +270,15 @@ fn build_adrop_for_coroutine_shim<'tcx>(
269270
}
270271
});
271272

272-
// _cor_ref = &*cor_ptr
273-
let reborrow = Rvalue::Ref(
274-
tcx.lifetimes.re_erased,
275-
BorrowKind::Mut { kind: MutBorrowKind::Default },
276-
tcx.mk_place_deref(Place::from(cor_ptr_local)),
277-
);
273+
// _cor_ref = cor_ref_tmp
278274
body.basic_blocks_mut()[START_BLOCK].statements.insert(
279275
idx,
280276
Statement::new(
281277
source_info,
282-
StatementKind::Assign(Box::new((Place::from(cor_ref_local), reborrow))),
278+
StatementKind::Assign(Box::new((
279+
Place::from(cor_ref_local),
280+
Rvalue::Use(Operand::Move(Place::from(cor_ref_tmp_local))),
281+
))),
283282
),
284283
);
285284
}
@@ -341,7 +340,7 @@ fn build_adrop_for_adrop_shim<'tcx>(
341340
let mut cor_ptr_local = proxy_ref_local;
342341
proxy_ty.find_async_drop_impl_coroutine(tcx, |ty| {
343342
if ty != proxy_ty {
344-
let ty_ptr = Ty::new_mut_ptr(tcx, ty);
343+
let ty_ptr = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, ty);
345344
let impl_ptr_place = Place::from(cor_ptr_local)
346345
.project_deeper(&[PlaceElem::Deref, PlaceElem::Field(FieldIdx::ZERO, ty_ptr)], tcx);
347346
cor_ptr_local = locals.push(LocalDecl::new(ty_ptr, span));

library/core/src/future/async_drop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ pub trait AsyncDrop {
5252
/// [ptr::drop_in_place]: crate::ptr::drop_in_place()
5353
#[unstable(feature = "async_drop", issue = "126482")]
5454
#[lang = "async_drop_in_place"]
55-
// FIXME: accept a reference here instead
56-
pub async unsafe fn async_drop_in_place<T: ?Sized>(_to_drop: *mut T) {
55+
pub async unsafe fn async_drop_in_place<T: ?Sized>(_to_drop: &mut T) {
5756
// Code here does not matter - this is replaced by the
5857
// real implementation by the compiler.
5958
}

src/tools/miri/tests/pass/async-drop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::task::{Context, Poll, Waker};
1818

1919
async fn test_async_drop<T>(x: T) {
2020
let mut x = mem::MaybeUninit::new(x);
21-
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
21+
let dtor = pin!(unsafe { async_drop_in_place(&mut *x.as_mut_ptr()) });
2222
test_idempotency(dtor).await;
2323
}
2424

@@ -69,7 +69,7 @@ fn main() {
6969
.await;
7070

7171
let mut ptr19 = mem::MaybeUninit::new(AsyncInt(19));
72-
let async_drop_fut = pin!(unsafe { async_drop_in_place(ptr19.as_mut_ptr()) });
72+
let async_drop_fut = pin!(unsafe { async_drop_in_place(&mut *ptr19.as_mut_ptr()) });
7373
test_idempotency(async_drop_fut).await;
7474

7575
let foo = AsyncInt(20);

tests/ui/async-await/async-drop/async-drop-initial.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
//@ edition: 2021
1212

1313
// FIXME(zetanumbers): consider AsyncDestruct::async_drop cleanup tests
14-
use core::future::{async_drop_in_place, AsyncDrop, Future};
14+
use core::future::{AsyncDrop, Future, async_drop_in_place};
1515
use core::hint::black_box;
1616
use core::mem::{self, ManuallyDrop};
17-
use core::pin::{pin, Pin};
17+
use core::pin::{Pin, pin};
1818
use core::task::{Context, Poll, Waker};
1919

2020
async fn test_async_drop<T>(x: T, _size: usize) {
2121
let mut x = mem::MaybeUninit::new(x);
22-
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
22+
let dtor = pin!(unsafe { async_drop_in_place(&mut *x.as_mut_ptr()) });
2323

2424
// FIXME(zetanumbers): This check fully depends on the layout of
2525
// the coroutine state, since async destructor combinators are just
@@ -60,10 +60,7 @@ fn main() {
6060
let j = 42;
6161
test_async_drop(&i, 16).await;
6262
test_async_drop(&j, 16).await;
63-
test_async_drop(
64-
AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 },
65-
136,
66-
).await;
63+
test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }, 136).await;
6764
test_async_drop(ManuallyDrop::new(AsyncInt(9)), 16).await;
6865

6966
let foo = AsyncInt(10);
@@ -92,7 +89,7 @@ fn main() {
9289
.await;
9390

9491
let mut ptr19 = mem::MaybeUninit::new(AsyncInt(19));
95-
let async_drop_fut = pin!(unsafe { async_drop_in_place(ptr19.as_mut_ptr()) });
92+
let async_drop_fut = pin!(unsafe { async_drop_in_place(&mut *ptr19.as_mut_ptr()) });
9693
test_idempotency(async_drop_fut).await;
9794

9895
let foo = AsyncInt(20);
@@ -228,19 +225,13 @@ union AsyncUnion {
228225

229226
impl Drop for AsyncUnion {
230227
fn drop(&mut self) {
231-
println!(
232-
"AsyncUnion::drop: {}, {}",
233-
unsafe { self.signed },
234-
unsafe { self.unsigned },
235-
);
228+
println!("AsyncUnion::drop: {}, {}", unsafe { self.signed }, unsafe { self.unsigned },);
236229
}
237230
}
238231
impl AsyncDrop for AsyncUnion {
239232
async fn drop(self: Pin<&mut Self>) {
240-
println!(
241-
"AsyncUnion::Dropper::poll: {}, {}",
242-
unsafe { self.signed },
243-
unsafe { self.unsigned },
244-
);
233+
println!("AsyncUnion::Dropper::poll: {}, {}", unsafe { self.signed }, unsafe {
234+
self.unsigned
235+
},);
245236
}
246237
}

tests/ui/async-await/async-drop/ex-ice-132103.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#![feature(async_drop)]
77
#![allow(incomplete_features)]
88

9-
use core::future::{async_drop_in_place, Future};
9+
use core::future::{Future, async_drop_in_place};
1010
use core::mem::{self};
1111
use core::pin::pin;
1212
use core::task::{Context, Waker};
1313

1414
async fn test_async_drop<T>(x: T) {
1515
let mut x = mem::MaybeUninit::new(x);
16-
pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
16+
pin!(unsafe { async_drop_in_place(&mut *x.as_mut_ptr()) });
1717
}
1818

1919
fn main() {

tests/ui/async-await/async-drop/open-drop-error2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
#![feature(async_drop)]
44
#![allow(incomplete_features)]
55

6-
use std::{
7-
future::{Future, async_drop_in_place},
8-
pin::pin,
9-
task::Context,
10-
};
6+
use std::future::{Future, async_drop_in_place};
7+
use std::pin::pin;
8+
use std::task::Context;
119

1210
fn wrong() -> impl Sized {
1311
//~^ ERROR: the size for values of type `str` cannot be known at compilation time
1412
*"abc" // Doesn't implement Sized
1513
}
1614
fn weird(context: &mut Context<'_>) {
1715
let mut e = wrong();
18-
let h = unsafe { async_drop_in_place(&raw mut e) };
16+
let h = unsafe { async_drop_in_place(&mut e) };
1917
let i = pin!(h);
2018
i.poll(context);
2119
}

tests/ui/async-await/async-drop/open-drop-error2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the size for values of type `str` cannot be known at compilation time
2-
--> $DIR/open-drop-error2.rs:12:15
2+
--> $DIR/open-drop-error2.rs:10:15
33
|
44
LL | fn wrong() -> impl Sized {
55
| ^^^^^^^^^^ doesn't have a size known at compile-time

0 commit comments

Comments
 (0)