Skip to content

Treat ManuallyDrop as ~const Destruct #140445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,9 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
let destruct_def_id = cx.require_lang_item(TraitSolverLangItem::Destruct);

match self_ty.kind() {
// `ManuallyDrop` is trivially `~const Destruct` as we do not run any drop glue on it.
ty::Adt(adt_def, _) if adt_def.is_manually_drop() => Ok(vec![]),

// An ADT is `~const Destruct` only if all of the fields are,
// *and* if there is a `Drop` impl, that `Drop` impl is also `~const`.
ty::Adt(adt_def, args) => {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_trait_selection/src/traits/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ fn evaluate_host_effect_for_destruct_goal<'tcx>(
let self_ty = obligation.predicate.self_ty();

let const_conditions = match *self_ty.kind() {
// `ManuallyDrop` is trivially `~const Destruct` as we do not run any drop glue on it.
ty::Adt(adt_def, _) if adt_def.is_manually_drop() => thin_vec![],

// An ADT is `~const Destruct` only if all of the fields are,
// *and* if there is a `Drop` impl, that `Drop` impl is also `~const`.
ty::Adt(adt_def, args) => {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/traits/const-traits/drop-manually-drop-no-drop-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@[new] compile-flags: -Znext-solver
//@ revisions: old new
//@ check-pass

use std::mem::ManuallyDrop;

struct Moose;

impl Drop for Moose {
fn drop(&mut self) {}
}

struct ConstDropper<T>(ManuallyDrop<T>);

const fn foo(_var: ConstDropper<Moose>) {}

fn main() {}
24 changes: 24 additions & 0 deletions tests/ui/traits/const-traits/drop-manually-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@[new] compile-flags: -Znext-solver
//@ revisions: old new
//@ check-pass

#![feature(const_destruct)]
#![feature(const_trait_impl)]

use std::mem::ManuallyDrop;

struct Moose;

impl Drop for Moose {
fn drop(&mut self) {}
}

struct ConstDropper<T>(ManuallyDrop<T>);

impl<T> const Drop for ConstDropper<T> {
fn drop(&mut self) {}
}

const fn foo(_var: ConstDropper<Moose>) {}

fn main() {}
Loading