Skip to content

Commit 281786e

Browse files
committed
change type argument of drop_in_place lang item to &mut _
1 parent 9695dac commit 281786e

13 files changed

Lines changed: 47 additions & 70 deletions

File tree

compiler/rustc_codegen_cranelift/example/mini_core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ unsafe extern "C" {
555555
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
556556
// Code here does not matter - this is replaced by the
557557
// real drop glue by the compiler.
558+
// FIXME
558559
unsafe {
559560
drop_in_place(to_drop);
560561
}

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
901901
};
902902
let fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, ty::List::empty())?;
903903

904-
let arg = self.mplace_to_ptr(&place)?;
904+
let arg = self.mplace_to_mut_ref(&place)?;
905+
905906
let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?);
906907

907908
self.init_fn_call(

compiler/rustc_mir_transform/src/coroutine/drop.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,6 @@ pub(super) fn create_coroutine_drop_shim<'tcx>(
596596

597597
make_coroutine_state_argument_indirect(tcx, &mut body);
598598

599-
// Change the coroutine argument from &mut to *mut
600-
body.local_decls[SELF_ARG] =
601-
LocalDecl::with_source_info(Ty::new_mut_ptr(tcx, coroutine_ty), source_info);
602-
603599
// Make sure we remove dead blocks to remove
604600
// unrelated code from the resume part of the function
605601
simplify::remove_dead_blocks(&mut body);

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,12 @@ fn fn_abi_of_instance_raw<'tcx>(
340340
}
341341

342342
/// Returns argument attributes for a scalar argument.
343-
///
344-
/// `drop_target_pointee`, if set, causes pointer-typed scalars to be treated like mutable
345-
/// references to the given type. This is used to special-case the argument of `ptr::drop_in_place`,
346-
/// interpreting it as `&mut T` instead of `*mut T`, for the purposes of attributes (which is valid
347-
/// as per its safety contract). If `drop_target_pointee` is set, `offset` must be 0 and `layout.ty`
348-
/// must be a pointer to the given type. Note that for wide pointers this function is called twice
349-
/// -- once for the data pointer and once for the vtable pointer. `drop_target_pointee` must only
350-
/// be set for the data pointer.
351343
fn arg_attrs_for_rust_scalar<'tcx>(
352344
cx: LayoutCx<'tcx>,
353345
scalar: Scalar,
354346
layout: TyAndLayout<'tcx>,
355347
offset: Size,
356348
is_return: bool,
357-
drop_target_pointee: Option<Ty<'tcx>>,
358349
) -> ArgAttributes {
359350
let mut attrs = ArgAttributes::new();
360351

@@ -374,23 +365,13 @@ fn arg_attrs_for_rust_scalar<'tcx>(
374365

375366
// Set `nonnull` if the validity range excludes zero, or for the argument to `drop_in_place`,
376367
// which must be nonnull per its documented safety requirements.
377-
if !valid_range.contains(0) || drop_target_pointee.is_some() {
368+
if !valid_range.contains(0) {
378369
attrs.set(ArgAttribute::NonNull);
379370
}
380371

381372
let tcx = cx.tcx();
382373

383-
let drop_target_pointee_info = drop_target_pointee.and_then(|pointee| {
384-
assert_eq!(pointee, layout.ty.builtin_deref(true).unwrap());
385-
assert_eq!(offset, Size::ZERO);
386-
// The argument to `drop_in_place` is semantically equivalent to a mutable reference.
387-
let mutref = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, pointee);
388-
let layout = cx.layout_of(mutref).unwrap();
389-
layout.pointee_info_at(&cx, offset)
390-
});
391-
392-
if let Some(pointee) = drop_target_pointee_info.or_else(|| layout.pointee_info_at(&cx, offset))
393-
{
374+
if let Some(pointee) = layout.pointee_info_at(&cx, offset) {
394375
if pointee.align > Align::ONE {
395376
attrs.pointee_align =
396377
Some(pointee.align.min(cx.tcx().sess.target.max_reliable_alignment()));
@@ -589,20 +570,10 @@ fn fn_abi_new_uncached<'tcx>(
589570
extra_args
590571
};
591572

592-
let is_drop_in_place = determined_fn_def_id.is_some_and(|def_id| {
593-
tcx.is_lang_item(def_id, LangItem::DropInPlace)
594-
|| tcx.is_lang_item(def_id, LangItem::AsyncDropInPlace)
595-
});
596-
597573
let arg_of = |ty: Ty<'tcx>, arg_idx: Option<usize>| -> Result<_, &'tcx FnAbiError<'tcx>> {
598574
let span = tracing::debug_span!("arg_of");
599575
let _entered = span.enter();
600576
let is_return = arg_idx.is_none();
601-
let is_drop_target = is_drop_in_place && arg_idx == Some(0);
602-
let drop_target_pointee = is_drop_target.then(|| match ty.kind() {
603-
ty::RawPtr(ty, _) => *ty,
604-
_ => bug!("argument to drop_in_place is not a raw ptr: {:?}", ty),
605-
});
606577

607578
let layout = cx.layout_of(ty).map_err(|err| &*tcx.arena.alloc(FnAbiError::Layout(*err)))?;
608579
let layout = if is_virtual_call && arg_idx == Some(0) {
@@ -615,16 +586,7 @@ fn fn_abi_new_uncached<'tcx>(
615586
};
616587

617588
Ok(ArgAbi::new(cx, layout, |scalar, offset| {
618-
arg_attrs_for_rust_scalar(
619-
*cx,
620-
scalar,
621-
layout,
622-
offset,
623-
is_return,
624-
// Only set `drop_target_pointee` for the data part of a wide pointer.
625-
// See `arg_attrs_for_rust_scalar` docs for more information.
626-
drop_target_pointee.filter(|_| offset == Size::ZERO),
627-
)
589+
arg_attrs_for_rust_scalar(*cx, scalar, layout, offset, is_return)
628590
}))
629591
};
630592

library/core/src/future/async_drop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +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
5556
pub async unsafe fn async_drop_in_place<T: ?Sized>(_to_drop: *mut T) {
5657
// Code here does not matter - this is replaced by the
5758
// real implementation by the compiler.

library/core/src/ptr/mod.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -801,20 +801,35 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
801801
/// // Ensure that the last item was dropped.
802802
/// assert!(weak.upgrade().is_none());
803803
/// ```
804+
#[inline(always)]
804805
#[stable(feature = "drop_in_place", since = "1.8.0")]
805-
#[lang = "drop_in_place"]
806-
#[allow(unconditional_recursion)]
807806
#[rustc_diagnostic_item = "ptr_drop_in_place"]
808807
#[rustc_const_unstable(feature = "const_drop_in_place", issue = "109342")]
809808
pub const unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T)
810809
where
811810
T: [const] Destruct,
812811
{
813-
// Code here does not matter - this is replaced by the
814-
// real drop glue by the compiler.
812+
// Due to historic reasons, `drop_in_place` takes a pointer rather than a reference,
813+
// which results in worse codegen since we don't apply noalias/dereferenceable llvm
814+
// attributes to pointer arguments. To workaround this without breaking public
815+
// interface, `drop_in_place` calls the lang item, rather than being one directly.
816+
817+
#[lang = "drop_in_place"]
818+
const unsafe fn drop_in_place_glue<T: PointeeSized>(_: &mut T)
819+
where
820+
T: [const] Destruct,
821+
{
822+
// Code here does not matter - this is replaced by the
823+
// real drop glue by the compiler.
824+
825+
// // SAFETY: see comment above
826+
// unsafe { drop_in_place_glue(to_drop) }
827+
}
815828

816-
// SAFETY: see comment above
817-
unsafe { drop_in_place(to_drop) }
829+
// SAFETY:
830+
// - compiler glue has the same safety requirements as this function
831+
// - the pointer must be valid as per the safety requirement of this function
832+
unsafe { drop_in_place_glue(&mut *to_drop) }
818833
}
819834

820835
/// Creates a null raw pointer.

tests/codegen-llvm/inline-hint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn f() {
1616

1717
struct A(String, String);
1818

19-
// CHECK: ; core::ptr::drop_in_place::<inline_hint::A>
19+
// CHECK: ; core::ptr::drop_in_place::drop_in_place_glue::<inline_hint::A>
2020
// CHECK-NEXT: ; Function Attrs:
2121
// CHECK-NOT: inlinehint
2222
// CHECK-SAME: {{$}}

tests/codegen-llvm/sanitizer/cfi/emit-type-metadata-id-itanium-cxx-abi-drop-in-place.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#![crate_type = "lib"]
1111

12-
// CHECK-LABEL: define{{.*}}4core3ptr13drop_in_placeDNtNtB4_6marker4Send
12+
// CHECK-LABEL: define{{.*}}4core3ptr13drop_in_place18drop_in_place_glueDNtNtB6_6marker4Send
1313
// CHECK-SAME: {{.*}}!type ![[TYPE1:[0-9]+]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
14-
// CHECK: call i1 @llvm.type.test(ptr {{%.+}}, metadata !"_ZTSFvPu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEE")
14+
// CHECK: call i1 @llvm.type.test(ptr {{%.+}}, metadata !"_ZTSFvU3mutu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEEE")
1515

1616
struct EmptyDrop;
1717
// CHECK-NOT: define{{.*}}4core3ptr{{[0-9]+}}drop_in_place$LT${{.*}}EmptyDrop$GT${{.*}}!type ![[TYPE1]] !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
@@ -28,4 +28,4 @@ pub fn foo() {
2828
let _ = Box::new(PresentDrop) as Box<dyn Send>;
2929
}
3030

31-
// CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvPu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEE"}
31+
// CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvU3mutu3refIu3dynIu{{[0-9]+}}NtNtNtC{{[[:print:]]+}}_4core3ops4drop4Dropu6regionEEE"}

tests/ui/consts/const-eval/c-variadic-fail.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ LL | drop(ap);
240240
| ^^^^^^^^
241241
note: inside `std::mem::drop::<VaList<'_>>`
242242
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
243-
note: inside `drop_in_place::<VaList<'_>> - shim(Some(VaList<'_>))`
243+
note: inside `drop_in_place::drop_in_place_glue::<VaList<'_>> - shim(Some(VaList<'_>))`
244244
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
245245
note: inside `<VaList<'_> as Drop>::drop`
246246
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL
@@ -272,7 +272,7 @@ LL | drop(ap);
272272
| ^^^^^^^^
273273
note: inside `std::mem::drop::<VaList<'_>>`
274274
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
275-
note: inside `drop_in_place::<VaList<'_>> - shim(Some(VaList<'_>))`
275+
note: inside `drop_in_place::drop_in_place_glue::<VaList<'_>> - shim(Some(VaList<'_>))`
276276
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
277277
note: inside `<VaList<'_> as Drop>::drop`
278278
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL
@@ -325,7 +325,7 @@ error[E0080]: pointer not dereferenceable: pointer must point to some allocation
325325
LL | }
326326
| ^ evaluation of `drop_of_invalid::{constant#0}` failed inside this call
327327
|
328-
note: inside `drop_in_place::<VaList<'_>> - shim(Some(VaList<'_>))`
328+
note: inside `drop_in_place::drop_in_place_glue::<VaList<'_>> - shim(Some(VaList<'_>))`
329329
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
330330
note: inside `<VaList<'_> as Drop>::drop`
331331
--> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL

tests/ui/consts/miri_unleashed/assoc_const.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ error[E0080]: calling non-const function `<Vec<u32> as Drop>::drop`
44
LL | const F: u32 = (U::X, 42).1;
55
| ^ evaluation of `<std::string::String as Bar<std::vec::Vec<u32>, std::string::String>>::F` failed inside this call
66
|
7-
note: inside `drop_in_place::<(Vec<u32>, u32)> - shim(Some((Vec<u32>, u32)))`
7+
note: inside `drop_in_place::drop_in_place_glue::<(Vec<u32>, u32)> - shim(Some((Vec<u32>, u32)))`
88
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
9-
note: inside `drop_in_place::<Vec<u32>> - shim(Some(Vec<u32>))`
9+
note: inside `drop_in_place::drop_in_place_glue::<Vec<u32>> - shim(Some(Vec<u32>))`
1010
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
1111

1212
note: erroneous constant encountered

0 commit comments

Comments
 (0)