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
25 changes: 20 additions & 5 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,7 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
ty: Ty<'db>,
locals: &Locals<'a>,
addr: Address,
_metadata: &[u8],
metadata: &[u8],
span: MirSpan,
) -> Result<'db, ()> {
let Some(drop_fn) = self.lang_items().Drop_drop else {
Expand All @@ -3046,13 +3046,23 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
};

let generic_args = GenericArgs::new_from_slice(&[ty.into()]);
if let Ok(MirOrDynIndex::Mir(body)) =
self.get_mir_or_dyn_index(drop_fn, generic_args, locals, span)
let (drop_impl, drop_args) = self.db.lookup_impl_method(
ParamEnvAndCrate { param_env: self.param_env.param_env, krate: self.crate_id },
drop_fn,
generic_args,
);
if let Either::Left(drop_impl) = drop_impl
&& matches!(drop_impl.lookup(self.db).container, ItemContainerId::ImplId(_))
&& let Ok(body) = self.db.monomorphized_mir_body(
drop_impl.into(),
drop_args.store(),
self.param_env.store(),
)
Comment on lines +3049 to +3060

@Veykril Veykril Jun 13, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically here we now only run drop when its a concrete impl, not sure if we have a better more concise way of checking this tbh

View changes since the review

{
self.exec_looked_up_function(
body,
locals,
drop_fn,
drop_impl,
iter::once(IntervalOrOwned::Owned(addr.to_bytes().to_vec())),
span,
Interval { addr: Address::Invalid(0), size: 0 },
Expand Down Expand Up @@ -3093,6 +3103,12 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
AdtId::EnumId(_) => (),
}
}
TyKind::Dynamic(..) => {
if !metadata.is_empty() {
let concrete_ty = self.vtable_map.ty_of_bytes(metadata)?;
self.run_drop_glue_deep(concrete_ty, locals, addr, &[], span)?;
}
}
TyKind::Bool
| TyKind::Char
| TyKind::Int(_)
Expand All @@ -3115,7 +3131,6 @@ impl<'a, 'db: 'a> Evaluator<'a, 'db> {
| TyKind::Error(_)
| TyKind::Param(_)
| TyKind::Placeholder(_)
| TyKind::Dynamic(..)
| TyKind::FnPtr(..)
| TyKind::Bound(..)
| TyKind::Infer(..)
Expand Down
51 changes: 51 additions & 0 deletions crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ fn main() {
);
}

#[test]
fn drop_glue_for_type_without_drop_impl_does_not_recurse() {
check_pass(
r#"
//- minicore: drop, pin

struct HasDrop;
impl Drop for HasDrop {
fn drop(&mut self) {}
}

struct WithDropGlue {
field: HasDrop,
}

fn main() {
let _c = WithDropGlue { field: HasDrop };
}
"#,
);
}

#[test]
fn drop_if_let() {
check_pass(
Expand Down Expand Up @@ -358,6 +380,35 @@ fn main() {
);
}

#[test]
fn drop_glue_for_trait_object() {
check_panic(
r#"
//- minicore: manually_drop, coerce_unsized, fmt, panic, drop, pin
use core::mem::ManuallyDrop;
use core::ptr::drop_in_place;

struct X;
impl Drop for X {
fn drop(&mut self) {
panic!("concrete drop ran");
}
}

trait T {}
impl T for X {}

fn main() {
let mut x = ManuallyDrop::new(X);
let p = &mut x as *mut ManuallyDrop<X> as *mut X;
let obj: &mut dyn T = unsafe { &mut *p };
drop_in_place(obj);
}
"#,
"concrete drop ran",
);
}

#[test]
fn manually_drop() {
check_pass(
Expand Down
Loading