Skip to content
Draft
8 changes: 2 additions & 6 deletions compiler/rustc_codegen_cranelift/example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,10 @@ unsafe extern "C" {
fn _Unwind_Resume(exc: *mut ()) -> !;
}

#[lang = "drop_in_place"]
#[allow(unconditional_recursion)]
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[lang = "drop_glue"]
pub unsafe fn drop_glue<T: ?Sized>(_to_drop: &mut T) {
// Code here does not matter - this is replaced by the
// real drop glue by the compiler.
unsafe {
drop_in_place(to_drop);
}
}

#[lang = "unpin"]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ pub(crate) fn codegen_drop<'tcx>(
unwind: UnwindAction,
) {
let ty = drop_place.layout().ty;
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty);
let drop_instance = Instance::resolve_drop_glue(fx.tcx, ty);
let ret_block = fx.get_block(target);

// AsyncDropGlueCtorShim can't be here
Expand Down Expand Up @@ -734,7 +734,7 @@ pub(crate) fn codegen_drop<'tcx>(
fx.bcx.switch_to_block(continued);

// FIXME(eddyb) perhaps move some of this logic into
// `Instance::resolve_drop_in_place`?
// `Instance::resolve_drop_glue`?
let virtual_drop = Instance {
def: ty::InstanceKind::Virtual(drop_instance.def_id(), 0),
args: drop_instance.args,
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_codegen_gcc/example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,10 @@ fn eh_personality() -> ! {
loop {}
}

#[lang = "drop_in_place"]
#[allow(unconditional_recursion)]
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[lang = "drop_glue"]
pub unsafe fn drop_glue<T: ?Sized>(_to_drop: &mut T) {
// Code here does not matter - this is replaced by the
// real drop glue by the compiler.
drop_in_place(to_drop);
}

#[lang = "unpin"]
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,18 @@ fn upstream_monomorphizations_provider(

let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();

let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
let drop_glue_fn_def_id = tcx.lang_items().drop_glue_fn();
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();

for &cnum in cnums.iter() {
for (exported_symbol, _) in tcx.exported_generic_symbols(cnum).iter() {
let (def_id, args) = match *exported_symbol {
ExportedSymbol::Generic(def_id, args) => (def_id, args),
ExportedSymbol::DropGlue(ty) => {
if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id {
if let Some(drop_in_place_fn_def_id) = drop_glue_fn_def_id {
(drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
} else {
// `drop_in_place` in place does not exist, don't try
// to use it.
// `drop_glue` does not exist, don't try to use it.
continue;
}
}
Expand Down Expand Up @@ -465,7 +464,7 @@ fn upstream_drop_glue_for_provider<'tcx>(
tcx: TyCtxt<'tcx>,
args: GenericArgsRef<'tcx>,
) -> Option<CrateNum> {
let def_id = tcx.lang_items().drop_in_place_fn()?;
let def_id = tcx.lang_items().drop_glue_fn()?;
tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned()
}

Expand Down Expand Up @@ -587,7 +586,7 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
}
ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::resolve_drop_in_place(tcx, ty),
Instance::resolve_drop_glue(tcx, ty),
instantiating_crate,
),
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
) -> MergingSucc {
let ty = location.ty(self.mir, bx.tcx()).ty;
let ty = self.monomorphize(ty);
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
let drop_fn = Instance::resolve_drop_glue(bx.tcx(), ty);

if let ty::InstanceKind::DropGlue(_, None) = drop_fn.def {
// we don't actually need to drop anything.
Expand All @@ -622,7 +622,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
};
let (maybe_null, drop_fn, fn_abi, drop_instance) = match ty.kind() {
// FIXME(eddyb) perhaps move some of this logic into
// `Instance::resolve_drop_in_place`?
// `Instance::resolve_drop_glue`?
ty::Dynamic(_, _) => {
// IN THIS ARM, WE HAVE:
// ty = *mut (dyn Trait)
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,19 +886,21 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
_ => {
debug_assert_eq!(
instance,
ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty)
ty::Instance::resolve_drop_glue(*self.tcx, place.layout.ty)
);
place
}
};

let instance = {
let _trace =
enter_trace_span!(M, resolve::resolve_drop_in_place, ty = ?place.layout.ty);
ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty)
let _trace = enter_trace_span!(M, resolve::resolve_drop_glue, ty = ?place.layout.ty);
ty::Instance::resolve_drop_glue(*self.tcx, place.layout.ty)
};
let fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, ty::List::empty())?;

let arg = self.mplace_to_imm_ptr(&place, None)?;
let ref_ty = Ty::new_mut_ref(self.tcx.tcx, self.tcx.lifetimes.re_erased, place.layout.ty);
let arg = self.mplace_to_imm_ptr(&place, Some(ref_ty))?;

let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?);

self.init_fn_call(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let place = self.eval_place(place)?;
let instance = {
let _trace =
enter_trace_span!(M, resolve::resolve_drop_in_place, ty = ?place.layout.ty);
Instance::resolve_drop_in_place(*self.tcx, place.layout.ty)
enter_trace_span!(M, resolve::resolve_drop_glue, ty = ?place.layout.ty);
Instance::resolve_drop_glue(*self.tcx, place.layout.ty)
};
if let ty::InstanceKind::DropGlue(_, None) = instance.def {
// This is the branch we enter if and only if the dropped type has no drop glue
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ language_item_table! {
FormatArgument, sym::format_argument, format_argument, Target::Struct, GenericRequirement::None;
FormatArguments, sym::format_arguments, format_arguments, Target::Struct, GenericRequirement::None;

DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
// Compiler-generated drop glue function, aka `core::ptr::drop_glue`
DropGlue, sym::drop_glue, drop_glue_fn, Target::Fn, GenericRequirement::Exact(1);
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;

/// For all binary crates without `#![no_main]`, Rust will generate a "main" function.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ impl<'tcx> TyCtxt<'tcx> {
Node::Expr(parent_expr) => {
match parent_expr.kind {
// Addr-of, field projections, and LHS of assignment don't constitute reads.
// Assignment does call `drop_in_place`, though, but its safety
// requirements are not the same.
// Assignment does call `drop_glue`, though, but its safety requirements are
// not the same.
ExprKind::AddrOf(..) | ExprKind::Field(..) => false,

// Place-preserving expressions only constitute reads if their
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/exported_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'tcx> ExportedSymbol<'tcx> {
tcx.symbol_name(ty::Instance::new_raw(def_id, args))
}
ExportedSymbol::DropGlue(ty) => {
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
tcx.symbol_name(ty::Instance::resolve_drop_glue(tcx, ty))
}
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ pub enum TerminatorKind<'tcx> {
///
/// After drop elaboration: `Drop` terminators are a complete nop for types that have no drop
/// glue. For other types, `Drop` terminators behave exactly like a call to
/// `core::mem::drop_in_place` with a pointer to the given place.
/// `core::mem::drop_glue` with a reference to the given place.
///
/// `Drop` before drop elaboration is a *conditional* execution of the drop glue. Specifically,
/// the `Drop` will be executed if...
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn opt_incr_drop_glue_mode<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Instantiati
let Some(dtor) = adt_def.destructor(tcx) else {
// We use LocalCopy for drops of enums only; this code is inherited from
// https://github.com/rust-lang/rust/pull/67332 and the theory is that we get to optimize
// out code like drop_in_place(Option::None) before crate-local ThinLTO, which improves
// out code like `drop_glue(&mut Option::None)` before crate-local ThinLTO, which improves
// compile time. At the time of writing, simply removing this entire check does seem to
// regress incr-opt compile times. But it sure seems like a more sophisticated check could
// do better here.
Expand All @@ -80,8 +80,8 @@ fn opt_incr_drop_glue_mode<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Instantiati
}
};

// We've gotten to a drop_in_place for a type that directly implements Drop.
// The drop glue is a wrapper for the Drop::drop impl, and we are an optimized build, so in an
// We've gotten to a `drop_glue` for a type that directly implements Drop.
// The drop glue is a wrapper for the `Drop::drop` impl, and we are an optimized build, so in an
// effort to coordinate with the mode that the actual impl will get, we make the glue also
// LocalCopy.
if tcx.cross_crate_inlinable(dtor.did) {
Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ pub enum InstanceKind<'tcx> {
/// Proxy shim for async drop of future (def_id, proxy_cor_ty, impl_cor_ty)
FutureDropPollShim(DefId, Ty<'tcx>, Ty<'tcx>),

/// `core::ptr::drop_in_place::<T>`.
/// `core::ptr::drop_glue::<T>`.
///
/// The `DefId` is for `core::ptr::drop_in_place`.
/// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
/// glue.
/// The `DefId` is for `core::ptr::drop_glue`.
/// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop glue.
DropGlue(DefId, Option<Ty<'tcx>>),

/// Compiler-generated `<T as Clone>::clone` implementation.
Expand Down Expand Up @@ -293,7 +292,7 @@ impl<'tcx> InstanceKind<'tcx> {
use rustc_hir::definitions::DefPathData;
let def_id = match *self {
ty::InstanceKind::Item(def) => def,
ty::InstanceKind::DropGlue(_, Some(_)) => return false,
ty::InstanceKind::DropGlue(_, Some(ty)) => return ty.is_array(),
ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => return ty.is_coroutine(),
ty::InstanceKind::FutureDropPollShim(_, _, _) => return false,
ty::InstanceKind::AsyncDropGlue(_, _) => return false,
Expand Down Expand Up @@ -717,8 +716,8 @@ impl<'tcx> Instance<'tcx> {
}
}

pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
let def_id = tcx.require_lang_item(LangItem::DropInPlace, DUMMY_SP);
pub fn resolve_drop_glue(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
let def_id = tcx.require_lang_item(LangItem::DropGlue, DUMMY_SP);
let args = tcx.mk_args(&[ty.into()]);
Instance::expect_resolve(
tcx,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,12 +1228,12 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: ExternAbi)
return false;
}

// With -Z panic-in-drop=abort, drop_in_place never unwinds.
// With -Z panic-in-drop=abort, `drop_glue` never unwinds.
//
// This is not part of `codegen_fn_attrs` as it can differ between crates
// and therefore cannot be computed in core.
if !tcx.sess.opts.unstable_opts.panic_in_drop.unwinds()
&& tcx.is_lang_item(did, LangItem::DropInPlace)
&& tcx.is_lang_item(did, LangItem::DropGlue)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
let scalar = match *entry {
VtblEntry::MetadataDropInPlace => {
if ty.needs_drop(tcx, ty::TypingEnv::fully_monomorphized()) {
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
let instance = ty::Instance::resolve_drop_glue(tcx, ty);
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance, CTFE_ALLOC_SALT);
let fn_ptr = Pointer::from(fn_alloc_id);
Scalar::from_pointer(fn_ptr, &tcx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::util;
/// they are dropped from an aligned address.
///
/// For example, if we have something like
///
/// ```ignore (illustrative)
/// #[repr(packed)]
/// struct Foo {
Expand All @@ -20,9 +21,8 @@ use crate::util;
/// let foo = ...;
/// ```
///
/// We want to call `drop_in_place::<Vec<u8>>` on `data` from an aligned
/// address. This means we can't simply drop `foo.data` directly, because
/// its address is not aligned.
/// We want to call `drop_glue::<Vec<u8>>` with a reference to `data`, which must be aligned.
/// This means we can't simply drop `foo.data` directly, because its address is not aligned.
///
/// Instead, we move `foo.data` to a local and drop that:
/// ```ignore (illustrative)
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_mir_transform/src/coroutine/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ pub(super) fn create_coroutine_drop_shim<'tcx>(
// not a coroutine body itself; it just has its drop built out of it.
let _ = body.coroutine.take();
// Make sure the resume argument is not included here, since we're
// building a body for `drop_in_place`.
// building a body for `drop_glue`.
body.arg_count = 1;

let source_info = SourceInfo::outermost(body.span);
Expand All @@ -596,18 +596,14 @@ pub(super) fn create_coroutine_drop_shim<'tcx>(

make_coroutine_state_argument_indirect(tcx, &mut body);

// Change the coroutine argument from &mut to *mut
body.local_decls[SELF_ARG] =
LocalDecl::with_source_info(Ty::new_mut_ptr(tcx, coroutine_ty), source_info);

// Make sure we remove dead blocks to remove
// unrelated code from the resume part of the function
simplify::remove_dead_blocks(&mut body);

// Update the body's def to become the drop glue.
let coroutine_instance = body.source.instance;
let drop_in_place = tcx.require_lang_item(LangItem::DropInPlace, body.span);
let drop_instance = InstanceKind::DropGlue(drop_in_place, Some(coroutine_ty));
let drop_glue = tcx.require_lang_item(LangItem::DropGlue, body.span);
let drop_instance = InstanceKind::DropGlue(drop_glue, Some(coroutine_ty));

// Temporary change MirSource to coroutine's instance so that dump_mir produces more sensible
// filename.
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_mir_transform/src/elaborate_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ where
let ty::Adt(adt_def, adt_args) = pin_obj_ty.kind() else {
bug!();
};
let obj_ptr_ty = Ty::new_mut_ptr(tcx, drop_ty);
let unwrap_ty = adt_def.non_enum_variant().fields[FieldIdx::ZERO].ty(tcx, adt_args);
let obj_ref_place = Place::from(self.new_temp(unwrap_ty));
call_statements.push(self.assign(
Expand All @@ -366,11 +365,7 @@ where
))),
));

let obj_ptr_place = Place::from(self.new_temp(obj_ptr_ty));

let addr = Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_deref(obj_ref_place));
call_statements.push(self.assign(obj_ptr_place, addr));
obj_ptr_place
obj_ref_place
};
call_statements
.push(Statement::new(self.source_info, StatementKind::StorageLive(fut.local)));
Expand Down
Loading
Loading