From 3b3b6869cc9d0d404c35e18c54a60348a571de02 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Sun, 12 Apr 2026 17:09:52 +0530 Subject: [PATCH 01/11] feat: create an MVP for using `Destruct` for custom dtors --- compiler/rustc_attr_ir/src/lang_items.rs | 1 + .../src/back/symbol_export.rs | 4 +- compiler/rustc_middle/src/ty/instance.rs | 16 +++- compiler/rustc_monomorphize/src/collector.rs | 3 + .../src/solve/assembly/mod.rs | 70 +++++++++------- compiler/rustc_span/src/symbol.rs | 1 + .../src/traits/select/candidate_assembly.rs | 8 +- compiler/rustc_ty_utils/src/instance.rs | 83 ++++++++++++------- library/core/src/marker.rs | 11 ++- tests/ui/drop/custom_dtor_with_destruct.rs | 19 +++++ .../drop/custom_dtor_with_destruct.run.stdout | 1 + 11 files changed, 150 insertions(+), 67 deletions(-) create mode 100644 tests/ui/drop/custom_dtor_with_destruct.rs create mode 100644 tests/ui/drop/custom_dtor_with_destruct.run.stdout diff --git a/compiler/rustc_attr_ir/src/lang_items.rs b/compiler/rustc_attr_ir/src/lang_items.rs index f2ad7abba755d..5f6519a77e0f9 100644 --- a/compiler/rustc_attr_ir/src/lang_items.rs +++ b/compiler/rustc_attr_ir/src/lang_items.rs @@ -192,6 +192,7 @@ language_item_table! { Drop, sym::drop, drop_trait, Target::Trait, GenericRequirement::None; Destruct, sym::destruct, destruct_trait, Target::Trait, GenericRequirement::None; + DestructDropInPlace, sym::destruct_drop_in_place, destruct_drop_in_place, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None; AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1); diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index f30ba43b0b0ba..2270835816a94 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -503,7 +503,7 @@ fn upstream_monomorphizations_provider( let mut instances: DefIdMap> = Default::default(); - let drop_glue_fn_def_id = tcx.lang_items().drop_glue_fn(); + let drop_in_place_fn_def_id = tcx.lang_items().destruct_drop_in_place(); let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn(); for &cnum in cnums.iter() { @@ -572,7 +572,7 @@ fn upstream_drop_glue_for_provider<'tcx>( tcx: TyCtxt<'tcx>, args: GenericArgsRef<'tcx>, ) -> Option { - let def_id = tcx.lang_items().drop_glue_fn()?; + let def_id = tcx.lang_items().destruct_drop_in_place()?; tcx.upstream_monomorphizations_for(def_id)?.get(&args).cloned() } diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 1863986682ec8..7742c080cced4 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -160,7 +160,7 @@ pub enum ShimKind<'tcx> { /// Proxy shim for async drop of future (def_id, proxy_cor_ty, impl_cor_ty) FutureDropPoll(DefId, Ty<'tcx>, Ty<'tcx>), - /// `core::ptr::drop_glue::`. + /// `Destruct::drop_in_place()` /// /// The `DefId` is for `core::ptr::drop_glue`. /// The `Option>` is either `Some(T)`, or `None` for empty drop glue. @@ -796,8 +796,8 @@ impl<'tcx> Instance<'tcx> { } } - 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); + pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { + let def_id = tcx.require_lang_item(LangItem::DestructDropInPlace, DUMMY_SP); let args = tcx.mk_args(&[ty.into()]); Instance::expect_resolve( tcx, @@ -808,6 +808,16 @@ impl<'tcx> Instance<'tcx> { ) } + pub fn try_resolve_drop_in_place( + tcx: TyCtxt<'tcx>, + typing_env: ty::TypingEnv<'tcx>, + ty: Ty<'tcx>, + ) -> Result>, ErrorGuaranteed> { + let def_id = tcx.require_lang_item(LangItem::DestructDropInPlace, DUMMY_SP); + let args = tcx.mk_args(&[ty.into()]); + Instance::try_resolve(tcx, typing_env, def_id, args) + } + pub fn resolve_async_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { let def_id = tcx.require_lang_item(LangItem::AsyncDropInPlace, DUMMY_SP); let args = tcx.mk_args(&[ty.into()]); diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index b7813992db5bf..9288804dbae25 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1074,6 +1074,9 @@ fn visit_instance_use<'tcx>( /// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we /// can just link to the upstream crate and therefore don't need a mono item. fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool { + if let ty::InstanceKind::DropGlue(_, Some(_)) = instance.def { + return instance.upstream_monomorphization(tcx).is_none(); + } let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else { return true; }; diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index 51e1aea3850e4..b42963b2b2682 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -478,37 +478,47 @@ where match assemble_from { AssembleCandidatesFrom::All => { - self.assemble_builtin_impl_candidates(goal, &mut candidates)?; - // For performance we only assemble impls if there are no candidates - // which would shadow them. This is necessary to avoid hangs in rayon, - // see trait-system-refactor-initiative#109 for more details. - // - // We always assemble builtin impls as trivial builtin impls have a higher - // priority than where-clauses. - // - // We only do this if any such candidate applies without any constraints - // as we may want to weaken inference guidance in the future and don't want - // to worry about causing major performance regressions when doing so. - // See trait-system-refactor-initiative#226 for some ideas here. - let assemble_impls = match self.typing_mode() { - TypingMode::Coherence => true, - TypingMode::Typeck { .. } - | TypingMode::PostTypeckUntilBorrowck { .. } - | TypingMode::Reflection - | TypingMode::PostBorrowck { .. } - | TypingMode::PostAnalysis - | TypingMode::Codegen - | TypingMode::ErasedNotCoherence(MayBeErased) => !candidates.iter().any(|c| { - matches!( - c.source, - CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) - | CandidateSource::AliasBound(_) - ) && has_no_inference_or_external_constraints(c.result) - }), - }; - if assemble_impls { - self.assemble_impl_candidates(goal, &mut candidates)?; + let trait_def_id = goal.predicate.trait_def_id(self.cx()); + // Check if there are any user defined impls for Destruct. If there are, + // use those, and fallback to builtin drop glue impl if none are present + if self.cx().is_trait_lang_item(trait_def_id, SolverTraitLangItem::Destruct) { + self.assemble_impl_candidates(goal, &mut candidates); + let has_impl_candidate = + candidates.iter().any(|c| matches!(c.source, CandidateSource::Impl(_))); + if !has_impl_candidate { + self.assemble_builtin_impl_candidates(goal, &mut candidates); + } self.assemble_object_bound_candidates(goal, &mut candidates); + } else { + self.assemble_builtin_impl_candidates(goal, &mut candidates); + // For performance we only assemble impls if there are no candidates + // which would shadow them. This is necessary to avoid hangs in rayon, + // see trait-system-refactor-initiative#109 for more details. + // + // We always assemble builtin impls as trivial builtin impls have a higher + // priority than where-clauses. + // + // We only do this if any such candidate applies without any constraints + // as we may want to weaken inference guidance in the future and don't want + // to worry about causing major performance regressions when doing so. + // See trait-system-refactor-initiative#226 for some ideas here. + let assemble_impls = match self.typing_mode() { + TypingMode::Coherence => true, + TypingMode::Analysis { .. } + | TypingMode::Borrowck { .. } + | TypingMode::PostBorrowckAnalysis { .. } + | TypingMode::PostAnalysis => !candidates.iter().any(|c| { + matches!( + c.source, + CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) + | CandidateSource::AliasBound(_) + ) && has_no_inference_or_external_constraints(c.result) + }), + }; + if assemble_impls { + self.assemble_impl_candidates(goal, &mut candidates); + self.assemble_object_bound_candidates(goal, &mut candidates); + } } } AssembleCandidatesFrom::EnvAndBounds => { diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 768f1be1cd48d..4fbc8620858d0 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -814,6 +814,7 @@ symbols! { derive_from, derive_smart_pointer, destruct, + destruct_drop_in_place, destructuring_assignment, diagnostic, diagnostic_namespace, diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 30700689a8ff0..8fa1cad27065a 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -114,7 +114,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.assemble_candidates_for_unsizing(obligation, &mut candidates); } Some(LangItem::Destruct) => { - self.assemble_const_destruct_candidates(obligation, &mut candidates); + let before = candidates.vec.len(); + self.assemble_candidates_from_impls(obligation, &mut candidates); + let added_impl = + candidates.vec[before..].iter().any(|c| matches!(c, ImplCandidate(_))); + if !added_impl { + self.assemble_const_destruct_candidates(obligation, &mut candidates); + } } Some(LangItem::TransmuteTrait) => { // User-defined transmutability impls are permitted. diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 94ce0c2a533d2..ecf1b6169e87b 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -37,34 +37,7 @@ fn resolve_instance_raw<'tcx>( } else if tcx.is_lang_item(def_id, LangItem::DropGlue) { let ty = args.type_at(0); - let shim = if ty.needs_drop(tcx, typing_env) { - debug!(" => nontrivial drop glue"); - match *ty.kind() { - ty::Coroutine(coroutine_def_id, ..) => { - // FIXME: sync drop of coroutine with async drop (generate both versions?) - // Currently just ignored - if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() { - ty::ShimKind::DropGlue(def_id, None) - } else { - ty::ShimKind::DropGlue(def_id, Some(ty)) - } - } - ty::Closure(..) - | ty::CoroutineClosure(..) - | ty::Tuple(..) - | ty::Adt(..) - | ty::Dynamic(..) - | ty::Array(..) - | ty::Slice(..) - | ty::UnsafeBinder(..) => ty::ShimKind::DropGlue(def_id, Some(ty)), - // Drop shims can only be built from ADTs. - _ => return Ok(None), - } - } else { - debug!(" => trivial drop glue"); - ty::ShimKind::DropGlue(def_id, None) - }; - ty::InstanceKind::Shim(shim) + return ty::Instance::try_resolve_drop_in_place(tcx, typing_env, ty); } else if tcx.is_lang_item(def_id, LangItem::AsyncDropInPlace) { let ty = args.type_at(0); @@ -175,6 +148,20 @@ fn resolve_associated_item<'tcx>( if !eligible { return Ok(None); } + if tcx.is_lang_item(trait_ref.def_id, LangItem::Destruct) { + if !tcx.is_lang_item(trait_item_id, LangItem::DestructDropInPlace) { + bug!( + "unexpected associated item for built-in `{trait_ref}`: {}", + tcx.item_name(trait_item_id) + ); + } + + debug!("Got user Destruct impl"); + return Ok(Some(Instance { + def: ty::InstanceKind::Item(leaf_def.item.def_id), + args: rcvr_args, + })); + } let typing_env = typing_env.with_post_analysis_normalized(tcx); let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env); @@ -429,6 +416,46 @@ fn resolve_associated_item<'tcx>( } else { bug!("unexpected associated associated item") } + } else if tcx.is_lang_item(trait_ref.def_id, LangItem::Destruct) { + debug!( + "resolving Destruct for ImplSource::Builtin: {:?}, {:?}, {:?}", + typing_env, trait_item_id, rcvr_args + ); + if !tcx.is_lang_item(trait_item_id, LangItem::DestructDropInPlace) { + bug!( + "unexpected associated item for built-in `{trait_ref}`: {}", + tcx.item_name(trait_item_id) + ); + } + + let self_ty = trait_ref.self_ty(); + + let def = if self_ty.needs_drop(tcx, typing_env) { + match *self_ty.kind() { + ty::Coroutine(coroutine_def_id, ..) => { + if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() + { + ty::InstanceKind::DropGlue(trait_item_id, None) + } else { + ty::InstanceKind::DropGlue(trait_item_id, Some(self_ty)) + } + } + ty::Closure(..) + | ty::CoroutineClosure(..) + | ty::Tuple(..) + | ty::Adt(..) + | ty::Dynamic(..) + | ty::Array(..) + | ty::Slice(..) + | ty::UnsafeBinder(..) => { + ty::InstanceKind::DropGlue(trait_item_id, Some(self_ty)) + } + _ => return Ok(None), + } + } else { + ty::InstanceKind::DropGlue(trait_item_id, None) + }; + Some(ty::Instance { def, args: rcvr_args }) } else { Instance::try_resolve_item_for_coroutine(tcx, trait_item_id, trait_id, rcvr_args) } diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index e499955df9479..a67d20402afad 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1057,10 +1057,15 @@ marker_impls! { #[unstable(feature = "const_destruct", issue = "133214")] #[rustc_const_unstable(feature = "const_destruct", issue = "133214")] #[lang = "destruct"] -#[diagnostic::on_unimplemented(message = "can't drop `{Self}`")] -#[rustc_deny_explicit_impl] +#[rustc_on_unimplemented(message = "can't drop `{Self}`")] #[rustc_dyn_incompatible_trait] -pub const trait Destruct: PointeeSized {} +pub const trait Destruct: PointeeSized { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} /// A marker for tuple types. /// diff --git a/tests/ui/drop/custom_dtor_with_destruct.rs b/tests/ui/drop/custom_dtor_with_destruct.rs new file mode 100644 index 0000000000000..54ca01ca24fa1 --- /dev/null +++ b/tests/ui/drop/custom_dtor_with_destruct.rs @@ -0,0 +1,19 @@ +//@ run-pass +//@ check-stdout +//@ check-run-results + +#![feature(const_destruct)] +use std::marker::Destruct; +struct A { + _a: String, +} + +impl Destruct for A { + unsafe fn drop_in_place(_to_drop: *mut Self) { + println!("Hey i was dropped"); + } +} + +fn main() { + let _a = A { _a: String::new() }; +} diff --git a/tests/ui/drop/custom_dtor_with_destruct.run.stdout b/tests/ui/drop/custom_dtor_with_destruct.run.stdout new file mode 100644 index 0000000000000..e864173b60164 --- /dev/null +++ b/tests/ui/drop/custom_dtor_with_destruct.run.stdout @@ -0,0 +1 @@ +Hey i was dropped From 910b7b20a494ed1b5945bb627f9517e2eafece11 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Sun, 3 May 2026 13:37:34 +0530 Subject: [PATCH 02/11] fix: update minicores --- compiler/rustc_codegen_gcc/example/mini_core.rs | 8 +++++++- .../rust-analyzer/crates/hir-ty/src/tests/regression.rs | 8 +++++++- tests/auxiliary/minicore.rs | 8 +++++++- tests/ui/traits/const-traits/auxiliary/minicore.rs | 8 +++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_gcc/example/mini_core.rs b/compiler/rustc_codegen_gcc/example/mini_core.rs index 2d5a29ceb8191..fbd4df8a45ed3 100644 --- a/compiler/rustc_codegen_gcc/example/mini_core.rs +++ b/compiler/rustc_codegen_gcc/example/mini_core.rs @@ -54,7 +54,13 @@ pub trait MetaSized: PointeeSized {} pub trait Sized: MetaSized {} #[lang = "destruct"] -pub trait Destruct {} +pub trait Destruct { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} #[lang = "tuple_trait"] pub trait Tuple {} diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs index ff0e075ff6d69..f24336e360266 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs @@ -2438,7 +2438,13 @@ impl const MyClone for i32 { } } #[lang = "destruct"] -pub trait Destruct {} +pub trait Destruct { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} "#, ); } diff --git a/tests/auxiliary/minicore.rs b/tests/auxiliary/minicore.rs index 04564049dbed2..0078e674ad6bb 100644 --- a/tests/auxiliary/minicore.rs +++ b/tests/auxiliary/minicore.rs @@ -70,7 +70,13 @@ pub trait Sized: MetaSized {} #[lang = "destruct"] #[diagnostic::on_unimplemented(message = "can't drop `{Self}`")] -pub trait Destruct: PointeeSized {} +pub trait Destruct: PointeeSized { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} #[lang = "legacy_receiver"] pub trait LegacyReceiver {} diff --git a/tests/ui/traits/const-traits/auxiliary/minicore.rs b/tests/ui/traits/const-traits/auxiliary/minicore.rs index e1d1135e6d4ec..d5d6cfc2cb342 100644 --- a/tests/ui/traits/const-traits/auxiliary/minicore.rs +++ b/tests/ui/traits/const-traits/auxiliary/minicore.rs @@ -122,7 +122,13 @@ impl Receiver for T { } #[lang = "destruct"] -pub const trait Destruct {} +pub const trait Destruct { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} #[lang = "freeze"] pub unsafe auto trait Freeze {} From 6e0d6b2984927dc8f191da715284ccb0c5f8f7a6 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Wed, 6 May 2026 19:19:17 +0530 Subject: [PATCH 03/11] fix: add destruct_drop_in_place to another minicore --- compiler/rustc_codegen_cranelift/example/mini_core.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_cranelift/example/mini_core.rs b/compiler/rustc_codegen_cranelift/example/mini_core.rs index 08adec96a079b..8d580305baed1 100644 --- a/compiler/rustc_codegen_cranelift/example/mini_core.rs +++ b/compiler/rustc_codegen_cranelift/example/mini_core.rs @@ -50,7 +50,13 @@ pub trait MetaSized: PointeeSized {} pub trait Sized: MetaSized {} #[lang = "destruct"] -pub trait Destruct {} +pub trait Destruct { + /// Entrypoint for drop + /// + /// Generated by default if not implemented manually. + #[lang = "destruct_drop_in_place"] + unsafe fn drop_in_place(_to_drop: *mut Self); +} #[lang = "tuple_trait"] pub trait Tuple {} From ee2d97a794abc2351a853a252520d20b2e3c9a80 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Wed, 6 May 2026 20:42:55 +0530 Subject: [PATCH 04/11] fix: bless tests fix: bless tests --- tests/ui/consts/const-eval/c-variadic-fail.stderr | 12 ++++++------ .../collect-in-dead-drop.noopt.stderr | 2 +- .../required-consts/collect-in-dead-drop.opt.stderr | 2 +- .../collect-in-dead-move.noopt.stderr | 2 +- .../required-consts/collect-in-dead-move.opt.stderr | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/ui/consts/const-eval/c-variadic-fail.stderr b/tests/ui/consts/const-eval/c-variadic-fail.stderr index ccf2936324a9c..f4a02006129b5 100644 --- a/tests/ui/consts/const-eval/c-variadic-fail.stderr +++ b/tests/ui/consts/const-eval/c-variadic-fail.stderr @@ -464,8 +464,8 @@ LL | drop(ap); | ^^^^^^^^ note: inside `std::mem::drop::>` --> $SRC_DIR/core/src/mem/mod.rs:LL:COL -note: inside `std::ptr::drop_glue::> - shim(Some(VaList<'_>))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside ` as Destruct>::drop_in_place - shim(Some(VaList<'_>))` + --> $SRC_DIR/core/src/marker.rs:LL:COL note: inside ` as Drop>::drop` --> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL @@ -496,8 +496,8 @@ LL | drop(ap); | ^^^^^^^^ note: inside `std::mem::drop::>` --> $SRC_DIR/core/src/mem/mod.rs:LL:COL -note: inside `std::ptr::drop_glue::> - shim(Some(VaList<'_>))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside ` as Destruct>::drop_in_place - shim(Some(VaList<'_>))` + --> $SRC_DIR/core/src/marker.rs:LL:COL note: inside ` as Drop>::drop` --> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL @@ -549,8 +549,8 @@ error[E0080]: pointer not dereferenceable: pointer must point to some allocation LL | } | ^ evaluation of `drop_of_invalid::{constant#0}` failed inside this call | -note: inside `std::ptr::drop_glue::> - shim(Some(VaList<'_>))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside ` as Destruct>::drop_in_place - shim(Some(VaList<'_>))` + --> $SRC_DIR/core/src/marker.rs:LL:COL note: inside ` as Drop>::drop` --> $SRC_DIR/core/src/ffi/va_list.rs:LL:COL diff --git a/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr index 38e169c97016d..2727672dd4d40 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-drop.noopt.stderr @@ -11,7 +11,7 @@ LL | let _ = Fail::::C; | ^^^^^^^^^^^^ note: the above error was encountered while instantiating `fn as Drop>::drop` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + --> $SRC_DIR/core/src/marker.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/consts/required-consts/collect-in-dead-drop.opt.stderr b/tests/ui/consts/required-consts/collect-in-dead-drop.opt.stderr index 38e169c97016d..2727672dd4d40 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-drop.opt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-drop.opt.stderr @@ -11,7 +11,7 @@ LL | let _ = Fail::::C; | ^^^^^^^^^^^^ note: the above error was encountered while instantiating `fn as Drop>::drop` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + --> $SRC_DIR/core/src/marker.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr b/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr index 9f652e26f242f..19b41fb3e3ae6 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-move.noopt.stderr @@ -11,7 +11,7 @@ LL | let _ = Fail::::C; | ^^^^^^^^^^^^ note: the above error was encountered while instantiating `fn as Drop>::drop` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + --> $SRC_DIR/core/src/marker.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/consts/required-consts/collect-in-dead-move.opt.stderr b/tests/ui/consts/required-consts/collect-in-dead-move.opt.stderr index 9f652e26f242f..19b41fb3e3ae6 100644 --- a/tests/ui/consts/required-consts/collect-in-dead-move.opt.stderr +++ b/tests/ui/consts/required-consts/collect-in-dead-move.opt.stderr @@ -11,7 +11,7 @@ LL | let _ = Fail::::C; | ^^^^^^^^^^^^ note: the above error was encountered while instantiating `fn as Drop>::drop` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL + --> $SRC_DIR/core/src/marker.rs:LL:COL error: aborting due to 1 previous error From 3cbb172945446a382207e004bbc2a0a9154504ea Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 19:38:08 +0530 Subject: [PATCH 05/11] refactor: change drop_in_place to take &mut Self --- compiler/rustc_codegen_cranelift/example/mini_core.rs | 2 +- compiler/rustc_codegen_gcc/example/mini_core.rs | 2 +- library/core/src/marker.rs | 4 ++-- src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs | 2 +- tests/auxiliary/minicore.rs | 2 +- tests/ui/drop/custom_dtor_with_destruct.rs | 2 +- tests/ui/traits/const-traits/auxiliary/minicore.rs | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/example/mini_core.rs b/compiler/rustc_codegen_cranelift/example/mini_core.rs index 8d580305baed1..dac03ab7894bf 100644 --- a/compiler/rustc_codegen_cranelift/example/mini_core.rs +++ b/compiler/rustc_codegen_cranelift/example/mini_core.rs @@ -55,7 +55,7 @@ pub trait Destruct { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } #[lang = "tuple_trait"] diff --git a/compiler/rustc_codegen_gcc/example/mini_core.rs b/compiler/rustc_codegen_gcc/example/mini_core.rs index fbd4df8a45ed3..c3a64c326be55 100644 --- a/compiler/rustc_codegen_gcc/example/mini_core.rs +++ b/compiler/rustc_codegen_gcc/example/mini_core.rs @@ -59,7 +59,7 @@ pub trait Destruct { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } #[lang = "tuple_trait"] diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index a67d20402afad..7dd248fc52e44 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -856,7 +856,7 @@ unsafe impl TrivialClone for PhantomData {} #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_default", issue = "143894")] -const impl Default for PhantomData { +impl const Default for PhantomData { fn default() -> Self { Self } @@ -1064,7 +1064,7 @@ pub const trait Destruct: PointeeSized { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } /// A marker for tuple types. diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs index f24336e360266..82e4874cc1de1 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression.rs @@ -2443,7 +2443,7 @@ pub trait Destruct { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } "#, ); diff --git a/tests/auxiliary/minicore.rs b/tests/auxiliary/minicore.rs index 0078e674ad6bb..f01d650bf2fcf 100644 --- a/tests/auxiliary/minicore.rs +++ b/tests/auxiliary/minicore.rs @@ -75,7 +75,7 @@ pub trait Destruct: PointeeSized { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } #[lang = "legacy_receiver"] diff --git a/tests/ui/drop/custom_dtor_with_destruct.rs b/tests/ui/drop/custom_dtor_with_destruct.rs index 54ca01ca24fa1..3c2793c204ade 100644 --- a/tests/ui/drop/custom_dtor_with_destruct.rs +++ b/tests/ui/drop/custom_dtor_with_destruct.rs @@ -9,7 +9,7 @@ struct A { } impl Destruct for A { - unsafe fn drop_in_place(_to_drop: *mut Self) { + unsafe fn drop_in_place(_to_drop: &mut Self) { println!("Hey i was dropped"); } } diff --git a/tests/ui/traits/const-traits/auxiliary/minicore.rs b/tests/ui/traits/const-traits/auxiliary/minicore.rs index d5d6cfc2cb342..0f425e6fc5ec6 100644 --- a/tests/ui/traits/const-traits/auxiliary/minicore.rs +++ b/tests/ui/traits/const-traits/auxiliary/minicore.rs @@ -127,7 +127,7 @@ pub const trait Destruct { /// /// Generated by default if not implemented manually. #[lang = "destruct_drop_in_place"] - unsafe fn drop_in_place(_to_drop: *mut Self); + unsafe fn drop_in_place(_to_drop: &mut Self); } #[lang = "freeze"] From 29aff4f0ef5adb93e62a3e5c2bc4dcf9d997b785 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 20:06:07 +0530 Subject: [PATCH 06/11] fix: fix rebase errors --- .../rustc_codegen_cranelift/src/abi/mod.rs | 2 +- .../src/back/symbol_export.rs | 2 +- compiler/rustc_codegen_ssa/src/mir/block.rs | 2 +- .../rustc_const_eval/src/interpret/call.rs | 4 ++-- .../rustc_const_eval/src/interpret/step.rs | 2 +- .../src/middle/exported_symbols.rs | 2 +- compiler/rustc_middle/src/ty/vtable.rs | 2 +- .../src/remove_noop_landing_pads.rs | 2 +- compiler/rustc_monomorphize/src/collector.rs | 4 ++-- .../src/solve/assembly/mod.rs | 19 +++++++++++-------- .../rustc_public_bridge/src/context/impls.rs | 2 +- compiler/rustc_ty_utils/src/instance.rs | 8 ++++---- 12 files changed, 27 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/abi/mod.rs b/compiler/rustc_codegen_cranelift/src/abi/mod.rs index 7f46b19f7568f..f215998fb1bf6 100644 --- a/compiler/rustc_codegen_cranelift/src/abi/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/abi/mod.rs @@ -743,7 +743,7 @@ pub(crate) fn codegen_drop<'tcx>( unwind: UnwindAction, ) { let ty = drop_place.layout().ty; - let drop_instance = Instance::resolve_drop_glue(fx.tcx, ty); + let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty); let ret_block = fx.get_block(target); // AsyncDropGlueCtorShim can't be here diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 2270835816a94..bde43dc0bab7f 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -694,7 +694,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_glue(tcx, ty), + Instance::resolve_drop_in_place(tcx, ty), instantiating_crate, ), ExportedSymbol::AsyncDropGlueCtorShim(ty) => { diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index afd9a88784c2f..af67d48512b18 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -641,7 +641,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_glue(bx.tcx(), ty); + let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty); if let ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_, None)) = drop_fn.def { // we don't actually need to drop anything. diff --git a/compiler/rustc_const_eval/src/interpret/call.rs b/compiler/rustc_const_eval/src/interpret/call.rs index 805c20755d9f7..0db6ca776e945 100644 --- a/compiler/rustc_const_eval/src/interpret/call.rs +++ b/compiler/rustc_const_eval/src/interpret/call.rs @@ -1015,7 +1015,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { _ => { debug_assert_eq!( instance, - ty::Instance::resolve_drop_glue(*self.tcx, place.layout.ty) + ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty) ); place } @@ -1023,7 +1023,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { let instance = { let _trace = enter_trace_span!(M, resolve::resolve_drop_glue, ty = ?place.layout.ty); - ty::Instance::resolve_drop_glue(*self.tcx, place.layout.ty) + ty::Instance::resolve_drop_in_place(*self.tcx, place.layout.ty) }; let fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, ty::List::empty())?; diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 6dd1ed598e3aa..992c3b8cdc192 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -631,7 +631,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { let instance = { let _trace = enter_trace_span!(M, resolve::resolve_drop_glue, ty = ?place.layout.ty); - Instance::resolve_drop_glue(*self.tcx, place.layout.ty) + Instance::resolve_drop_in_place(*self.tcx, place.layout.ty) }; if let ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_, None)) = instance.def { // This is the branch we enter if and only if the dropped type has no drop glue diff --git a/compiler/rustc_middle/src/middle/exported_symbols.rs b/compiler/rustc_middle/src/middle/exported_symbols.rs index e23ad3c832ef7..39de26b66a1be 100644 --- a/compiler/rustc_middle/src/middle/exported_symbols.rs +++ b/compiler/rustc_middle/src/middle/exported_symbols.rs @@ -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_glue(tcx, ty)) + tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty)) } ExportedSymbol::AsyncDropGlueCtorShim(ty) => { tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty)) diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index fb56bda7d4562..26d950b26986f 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -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_glue(tcx, ty); + let instance = ty::Instance::resolve_drop_in_place(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) diff --git a/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs b/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs index 7d55756a9a694..a0453190325bc 100644 --- a/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs +++ b/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs @@ -128,7 +128,7 @@ impl RemoveNoopLandingPads { extra.typing_env, ty::EarlyBinder::bind(extra.tcx, ty), ); - let drop_fn = Instance::resolve_drop_glue(extra.tcx, ty); + let drop_fn = Instance::resolve_drop_in_place(extra.tcx, ty); if let ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_, None)) = drop_fn.def { // no need to drop anything, if all of our successors are also no-op then we // can be skipped. diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 9288804dbae25..e8c052eea0a23 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -953,7 +953,7 @@ fn visit_drop_use<'tcx>( source: Span, output: &mut MonoItems<'tcx>, ) { - let instance = Instance::resolve_drop_glue(tcx, ty); + let instance = Instance::resolve_drop_in_place(tcx, ty); visit_instance_use(tcx, instance, is_direct_call, source, output); } @@ -1074,7 +1074,7 @@ fn visit_instance_use<'tcx>( /// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we /// can just link to the upstream crate and therefore don't need a mono item. fn should_codegen_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool { - if let ty::InstanceKind::DropGlue(_, Some(_)) = instance.def { + if let ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_, Some(_))) = instance.def { return instance.upstream_monomorphization(tcx).is_none(); } let Some(def_id) = instance.def.def_id_if_not_guaranteed_local_codegen() else { diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index b42963b2b2682..75e453c185379 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -482,15 +482,15 @@ where // Check if there are any user defined impls for Destruct. If there are, // use those, and fallback to builtin drop glue impl if none are present if self.cx().is_trait_lang_item(trait_def_id, SolverTraitLangItem::Destruct) { - self.assemble_impl_candidates(goal, &mut candidates); + self.assemble_impl_candidates(goal, &mut candidates)?; let has_impl_candidate = candidates.iter().any(|c| matches!(c.source, CandidateSource::Impl(_))); if !has_impl_candidate { - self.assemble_builtin_impl_candidates(goal, &mut candidates); + self.assemble_builtin_impl_candidates(goal, &mut candidates)?; } self.assemble_object_bound_candidates(goal, &mut candidates); } else { - self.assemble_builtin_impl_candidates(goal, &mut candidates); + self.assemble_builtin_impl_candidates(goal, &mut candidates)?; // For performance we only assemble impls if there are no candidates // which would shadow them. This is necessary to avoid hangs in rayon, // see trait-system-refactor-initiative#109 for more details. @@ -504,10 +504,13 @@ where // See trait-system-refactor-initiative#226 for some ideas here. let assemble_impls = match self.typing_mode() { TypingMode::Coherence => true, - TypingMode::Analysis { .. } - | TypingMode::Borrowck { .. } - | TypingMode::PostBorrowckAnalysis { .. } - | TypingMode::PostAnalysis => !candidates.iter().any(|c| { + TypingMode::Typeck { .. } + | TypingMode::PostTypeckUntilBorrowck { .. } + | TypingMode::PostBorrowck { .. } + | TypingMode::Reflection + | TypingMode::PostAnalysis + | TypingMode::Codegen + | TypingMode::ErasedNotCoherence(_) => !candidates.iter().any(|c| { matches!( c.source, CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) @@ -516,7 +519,7 @@ where }), }; if assemble_impls { - self.assemble_impl_candidates(goal, &mut candidates); + self.assemble_impl_candidates(goal, &mut candidates)?; self.assemble_object_bound_candidates(goal, &mut candidates); } } diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index 70e2498ac350b..1e5bfe4ce2e01 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -701,7 +701,7 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { /// Resolve an instance for drop_in_place for the given type. pub fn resolve_drop_in_place(&self, internal_ty: Ty<'tcx>) -> Instance<'tcx> { - let instance = Instance::resolve_drop_glue(self.tcx, internal_ty); + let instance = Instance::resolve_drop_in_place(self.tcx, internal_ty); instance } diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index ecf1b6169e87b..87dc8a52937b3 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -435,9 +435,9 @@ fn resolve_associated_item<'tcx>( ty::Coroutine(coroutine_def_id, ..) => { if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() { - ty::InstanceKind::DropGlue(trait_item_id, None) + ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, None)) } else { - ty::InstanceKind::DropGlue(trait_item_id, Some(self_ty)) + ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, Some(self_ty))) } } ty::Closure(..) @@ -448,12 +448,12 @@ fn resolve_associated_item<'tcx>( | ty::Array(..) | ty::Slice(..) | ty::UnsafeBinder(..) => { - ty::InstanceKind::DropGlue(trait_item_id, Some(self_ty)) + ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, Some(self_ty))) } _ => return Ok(None), } } else { - ty::InstanceKind::DropGlue(trait_item_id, None) + ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, None)) }; Some(ty::Instance { def, args: rcvr_args }) } else { From b1bd0b33b99b8ff0185541c56fb60aa196a0818e Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 20:19:31 +0530 Subject: [PATCH 07/11] fix: fix some syntax bugs --- compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 2 +- compiler/rustc_ty_utils/src/instance.rs | 12 ++++++++---- library/core/src/marker.rs | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index bde43dc0bab7f..164960c861fb9 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -519,7 +519,7 @@ fn upstream_monomorphizations_provider( 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_glue_fn_def_id { + if let Some(drop_in_place_fn_def_id) = drop_in_place_fn_def_id { (drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()])) } else { // `drop_glue` does not exist, don't try to use it. diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 87dc8a52937b3..435bd86613553 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -437,7 +437,10 @@ fn resolve_associated_item<'tcx>( { ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, None)) } else { - ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, Some(self_ty))) + ty::InstanceKind::Shim(ty::ShimKind::DropGlue( + trait_item_id, + Some(self_ty), + )) } } ty::Closure(..) @@ -447,9 +450,10 @@ fn resolve_associated_item<'tcx>( | ty::Dynamic(..) | ty::Array(..) | ty::Slice(..) - | ty::UnsafeBinder(..) => { - ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, Some(self_ty))) - } + | ty::UnsafeBinder(..) => ty::InstanceKind::Shim(ty::ShimKind::DropGlue( + trait_item_id, + Some(self_ty), + )), _ => return Ok(None), } } else { diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 7dd248fc52e44..d2e0ff1eb5118 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -856,7 +856,7 @@ unsafe impl TrivialClone for PhantomData {} #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_default", issue = "143894")] -impl const Default for PhantomData { +const impl Default for PhantomData { fn default() -> Self { Self } From 43f9ed48d76a66e69b5d294128f052e129c84d23 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 20:41:38 +0530 Subject: [PATCH 08/11] fix: fix early return that did not rebase args --- compiler/rustc_ty_utils/src/instance.rs | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 435bd86613553..59559ca1a2f42 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -148,6 +148,19 @@ fn resolve_associated_item<'tcx>( if !eligible { return Ok(None); } + + let typing_env = typing_env.with_post_analysis_normalized(tcx); + let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env); + let args = rcvr_args.rebase_onto(tcx, trait_def_id, impl_data.args); + let args = translate_args( + &infcx, + param_env, + impl_data.impl_def_id, + args, + leaf_def.defining_node, + ); + let args = infcx.tcx.erase_and_anonymize_regions(args); + if tcx.is_lang_item(trait_ref.def_id, LangItem::Destruct) { if !tcx.is_lang_item(trait_item_id, LangItem::DestructDropInPlace) { bug!( @@ -159,22 +172,10 @@ fn resolve_associated_item<'tcx>( debug!("Got user Destruct impl"); return Ok(Some(Instance { def: ty::InstanceKind::Item(leaf_def.item.def_id), - args: rcvr_args, + args, })); } - let typing_env = typing_env.with_post_analysis_normalized(tcx); - let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env); - let args = rcvr_args.rebase_onto(tcx, trait_def_id, impl_data.args); - let args = translate_args( - &infcx, - param_env, - impl_data.impl_def_id, - args, - leaf_def.defining_node, - ); - let args = infcx.tcx.erase_and_anonymize_regions(args); - // HACK: We may have overlapping `dyn Trait` built-in impls and // user-provided blanket impls. Detect that case here, and return // ambiguity. From 05eb666a28d8e9511cb7b0ad1c960a3f00067aba Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 20:44:51 +0530 Subject: [PATCH 09/11] fix: readd dropped comment --- compiler/rustc_ty_utils/src/instance.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 59559ca1a2f42..3a9e0b932f7fa 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -434,6 +434,8 @@ fn resolve_associated_item<'tcx>( let def = if self_ty.needs_drop(tcx, typing_env) { match *self_ty.kind() { ty::Coroutine(coroutine_def_id, ..) => { + // FIXME: sync drop of coroutine with async drop (generate both versions?) + // Currently just ignored if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() { ty::InstanceKind::Shim(ty::ShimKind::DropGlue(trait_item_id, None)) From d5f676f315747a297fce4b6eee50d85aaf350490 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Tue, 4 Aug 2026 20:47:40 +0530 Subject: [PATCH 10/11] fix: add comment abt early return --- compiler/rustc_ty_utils/src/instance.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index 3a9e0b932f7fa..23755e5f8da3b 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -169,6 +169,9 @@ fn resolve_associated_item<'tcx>( ); } + // `Destruct` isn't object-safe and has no specialization concerns here, + // so the checks below (dyn-Trait overlap, defaultness, args-compatibility, + // compare_impl_item) don't apply so we build the Instance directly. debug!("Got user Destruct impl"); return Ok(Some(Instance { def: ty::InstanceKind::Item(leaf_def.item.def_id), From 150dba15c04040d7b606e93501b7e95e76487e27 Mon Sep 17 00:00:00 2001 From: JayanAXHF Date: Sat, 5 Sep 2026 15:18:46 +0530 Subject: [PATCH 11/11] fix: bless tests --- .../ui/consts/miri_unleashed/assoc_const.stderr | 8 ++++---- tests/ui/consts/miri_unleashed/drop.stderr | 4 ++-- .../consts/qualif-indirect-mutation-fail.stderr | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/ui/consts/miri_unleashed/assoc_const.stderr b/tests/ui/consts/miri_unleashed/assoc_const.stderr index 29f371373aef9..878f1b6b27f34 100644 --- a/tests/ui/consts/miri_unleashed/assoc_const.stderr +++ b/tests/ui/consts/miri_unleashed/assoc_const.stderr @@ -4,10 +4,10 @@ error[E0080]: calling non-const function `::drop` LL | const F: u32 = (U::X, 42).1; | ^ evaluation of `>::F` failed inside this call | -note: inside `std::ptr::drop_glue::<(NotConstDestruct, u32)> - shim(Some((NotConstDestruct, u32)))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL -note: inside `std::ptr::drop_glue:: - shim(Some(NotConstDestruct))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside `<(NotConstDestruct, u32) as Destruct>::drop_in_place - shim(Some((NotConstDestruct, u32)))` + --> $SRC_DIR/core/src/marker.rs:LL:COL +note: inside `::drop_in_place - shim(Some(NotConstDestruct))` + --> $SRC_DIR/core/src/marker.rs:LL:COL note: erroneous constant encountered --> $DIR/assoc_const.rs:36:13 diff --git a/tests/ui/consts/miri_unleashed/drop.stderr b/tests/ui/consts/miri_unleashed/drop.stderr index 6e2afda763171..47c6306c3137e 100644 --- a/tests/ui/consts/miri_unleashed/drop.stderr +++ b/tests/ui/consts/miri_unleashed/drop.stderr @@ -4,8 +4,8 @@ error[E0080]: calling non-const function `::drop` LL | }; | ^ evaluation of `TEST_BAD` failed inside this call | -note: inside `std::ptr::drop_glue:: - shim(Some(NotConstDestruct))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside `::drop_in_place - shim(Some(NotConstDestruct))` + --> $SRC_DIR/core/src/marker.rs:LL:COL warning: skipping const checks | diff --git a/tests/ui/consts/qualif-indirect-mutation-fail.stderr b/tests/ui/consts/qualif-indirect-mutation-fail.stderr index 9bf679f8a5348..7aec6b10d90c8 100644 --- a/tests/ui/consts/qualif-indirect-mutation-fail.stderr +++ b/tests/ui/consts/qualif-indirect-mutation-fail.stderr @@ -13,10 +13,10 @@ error[E0080]: calling non-const function `::drop` LL | }; | ^ evaluation of `A1` failed inside this call | -note: inside `std::ptr::drop_glue::> - shim(Some(Option))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL -note: inside `std::ptr::drop_glue:: - shim(Some(NotConstDestruct))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside ` as Destruct>::drop_in_place - shim(Some(Option))` + --> $SRC_DIR/core/src/marker.rs:LL:COL +note: inside `::drop_in_place - shim(Some(NotConstDestruct))` + --> $SRC_DIR/core/src/marker.rs:LL:COL error[E0493]: destructor of `Option` cannot be evaluated at compile-time --> $DIR/qualif-indirect-mutation-fail.rs:34:9 @@ -32,10 +32,10 @@ error[E0080]: calling non-const function `::drop` LL | }; | ^ evaluation of `A2` failed inside this call | -note: inside `std::ptr::drop_glue::> - shim(Some(Option))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL -note: inside `std::ptr::drop_glue:: - shim(Some(NotConstDestruct))` - --> $SRC_DIR/core/src/ptr/mod.rs:LL:COL +note: inside ` as Destruct>::drop_in_place - shim(Some(Option))` + --> $SRC_DIR/core/src/marker.rs:LL:COL +note: inside `::drop_in_place - shim(Some(NotConstDestruct))` + --> $SRC_DIR/core/src/marker.rs:LL:COL error[E0493]: destructor of `(u32, Option)` cannot be evaluated at compile-time --> $DIR/qualif-indirect-mutation-fail.rs:12:9