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
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 @@ -575,14 +575,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 @@ -577,12 +577,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 @@ -603,7 +603,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 @@ -621,7 +621,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
34 changes: 9 additions & 25 deletions compiler/rustc_const_eval/src/interpret/call.rs
Comment thread
WaffleLapkin marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::borrow::Cow;
use either::{Left, Right};
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
use rustc_hir::def_id::DefId;
use rustc_hir::{LangItem, find_attr};
use rustc_hir::find_attr;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, AdtDef, Instance, Ty, Unnormalized, VariantDef};
use rustc_middle::{bug, mir, span_bug};
Expand Down Expand Up @@ -278,7 +278,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
callee_arg: &mir::Place<'tcx>,
callee_ty: Ty<'tcx>,
already_live: bool,
is_drop_in_place: bool,
) -> InterpResult<'tcx>
where
'tcx: 'x,
Expand Down Expand Up @@ -323,16 +322,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.storage_live_dyn(local, meta)?;
}
// Now we can finally actually evaluate the callee place.
let mut callee_arg = self.eval_place(*callee_arg)?;
// drop_in_place has a signature which says that the first argument is `*mut T`
// but really it's `&mut T`. This is where we handle that terrible hack in
// the MIR semantics.
// FIXME(#154274): remove this hack.
if is_drop_in_place && callee_arg_idx == 0 {
let pointee_ty = callee_arg.layout.ty.builtin_deref(true).unwrap();
let mutref_ty = Ty::new_mut_ref(*self.tcx, self.tcx.lifetimes.re_erased, pointee_ty);
callee_arg = callee_arg.transmute(self.layout_of(mutref_ty)?, self)?;
}
let callee_arg = self.eval_place(*callee_arg)?;
// We allow some transmutes here.
// FIXME: Depending on the PassMode, this should reset some padding to uninitialized. (This
// is true for all `copy_op`, but there are a lot of special cases for argument passing
Expand Down Expand Up @@ -464,12 +454,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
// Determine whether there is a special VaList argument. This is always the
// last argument, and since arguments start at index 1 that's `arg_count`.
let va_list_arg = callee_fn_abi.c_variadic.then(|| mir::Local::from_usize(body.arg_count));
// Part of the hack for #154274, see `pass_argument`.
let is_drop_in_place = {
let def_id = body.source.def_id();
self.tcx.is_lang_item(def_id, LangItem::DropInPlace)
|| self.tcx.is_lang_item(def_id, LangItem::AsyncDropInPlace)
};

// During argument passing, we want retagging with protectors.
M::with_retag_mode(self, RetagMode::FnEntry, |ecx| {
Expand Down Expand Up @@ -532,7 +516,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
&dest,
field_ty,
/* already_live */ true,
is_drop_in_place,
)?;
}
} else {
Expand All @@ -545,7 +528,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
&dest,
ty,
/* already_live */ false,
is_drop_in_place,
)?;
}
}
Expand Down Expand Up @@ -913,19 +895,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 @@ -593,8 +593,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 @@ -321,7 +321,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 @@ -300,8 +300,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 @@ -743,7 +743,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 @@ -70,7 +70,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 @@ -81,8 +81,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
11 changes: 5 additions & 6 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 @@ -716,8 +715,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 @@ -1221,12 +1221,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 @@ -365,11 +364,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
1 change: 0 additions & 1 deletion compiler/rustc_mir_transform/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
let mut body =
new_body(source, blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);

// The first argument (index 0), but local 1 (after the return place).
let dropee_ptr = Place::from(Local::arg(0));

if ty.is_some() {
Expand Down
Loading
Loading