Skip to content

Elaborate destruct host effect clauses with structurally implied clauses #144856

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
43 changes: 33 additions & 10 deletions compiler/rustc_type_ir/src/elaborate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,39 @@ impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
),
};
}
// `T: [const] Trait` implies `T: [const] Supertrait`.
ty::ClauseKind::HostEffect(data) => self.extend_deduped(
cx.explicit_implied_const_bounds(data.def_id()).iter_identity().map(|trait_ref| {
elaboratable.child(
trait_ref
.to_host_effect_clause(cx, data.constness)
.instantiate_supertrait(cx, bound_clause.rebind(data.trait_ref)),
)
}),
),
ty::ClauseKind::HostEffect(data) => {
// `T: [const] Trait` implies `T: [const] Supertrait`.
self.extend_deduped(
cx.explicit_implied_const_bounds(data.def_id()).iter_identity().map(
|trait_ref| {
elaboratable.child(
trait_ref
.to_host_effect_clause(cx, data.constness)
.instantiate_supertrait(
cx,
bound_clause.rebind(data.trait_ref),
),
)
},
),
);

// `Adt: [const] Trait` implies each field also implements `[const] Trait`
Copy link
Member

@compiler-errors compiler-errors Aug 3, 2025

Choose a reason for hiding this comment

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

If we did apply this, I'd probably want to abstract out the function that maps from a ty to its constituent types and share it between the Destruct trait logic and here. (T,): Destruct should probably imply T: Destruct, for example.

And [Ty; N]: Destruct should probably imply Ty: Destruct (perhaps except for 0, but that's another problem with the destruct trait...).

Logic would probably need to be pulled out of https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_next_trait_solver/solve/assembly/structural_traits.rs.html#743-820

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, absolutely I should do so and fill up missing cases for arrays and tuples. Thanks!

And it seems that I should also check for whether ManuallyDrop<T>: Destruct bound accidentally implies the bound T: Destruct as well 😅

let destruct_def_id = cx.require_lang_item(TraitSolverLangItem::Destruct);
if data.def_id() == destruct_def_id
&& let ty::Adt(adt_def, args) = data.self_ty().kind()
{
self.extend_deduped(adt_def.all_field_tys(cx).iter_instantiated(cx, args).map(
|ty| {
elaboratable.child(
bound_clause
.rebind(data.trait_ref.with_self_ty(cx, ty))
.to_host_effect_clause(cx, data.constness),
)
},
));
}
}
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
// We know that `T: 'a` for some type `T`. We can
// often elaborate this. For example, if we know that
Expand Down
9 changes: 5 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,9 +2147,7 @@ const fn expect_failed(msg: &str) -> ! {
#[rustc_const_unstable(feature = "const_try", issue = "74935")]
impl<T> const Clone for Option<T>
where
// FIXME(const_hack): the T: ~const Destruct should be inferred from the Self: ~const Destruct in clone_from.
// See https://github.com/rust-lang/rust/issues/144207
T: ~const Clone + ~const Destruct,
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused how a bound on clone_from can mean we remove a bound from clone. That seems backwards, since this function doesn't call clone_from?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess this might be okay since this bound was on impl, not fn clone?

T: ~const Clone,
{
#[inline]
fn clone(&self) -> Self {
Expand All @@ -2160,7 +2158,10 @@ where
}

#[inline]
fn clone_from(&mut self, source: &Self) {
fn clone_from(&mut self, source: &Self)
where
Self: ~const Destruct,
{
match (self, source) {
(Some(to), Some(from)) => to.clone_from(from),
(to, from) => *to = from.clone(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Demonstrates that `impl<T> const Clone for Option<T>` does not require const_hack bounds.
// See issue #144207.
//@ revisions: next old
//@ [next] compile-flags: -Znext-solver
//@ check-pass

#![feature(const_trait_impl, const_destruct)]

use std::marker::Destruct;

#[const_trait]
pub trait CloneLike: Sized {
fn clone(&self) -> Self;

fn clone_from(&mut self, source: &Self)
where
Self: [const] Destruct,
{
*self = source.clone()
}
}

enum OptionLike<T> {
None,
Some(T),
}

impl<T> const CloneLike for OptionLike<T>
where
T: [const] CloneLike,
{
fn clone(&self) -> Self {
match self {
Self::Some(x) => Self::Some(x.clone()),
Self::None => Self::None,
}
}

fn clone_from(&mut self, source: &Self)
where
Self: [const] Destruct,
{
match (self, source) {
(Self::Some(to), Self::Some(from)) => to.clone_from(from),
(to, from) => *to = from.clone(),
}
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ revisions: next old
//@ [next] compile-flags: -Znext-solver
//@ check-pass

#![feature(const_trait_impl, const_destruct)]

use std::marker::Destruct;

const fn ensure_const_destruct<T: [const] Destruct>(_t: T) {}

enum Either<T, U> {
Left(T),
Right(U),
}

struct Foo<T, U, V>(Either<T, U>, Option<V>);

struct Bar<T, U, V>(T, Result<U, V>);

const fn f<T>(x: T)
where
Option<T>: [const] Destruct,
{
ensure_const_destruct(x);
}

const fn g<T, U>(x: Result<T, U>)
where
Either<T, U>: [const] Destruct,
{
ensure_const_destruct(x);
}

const fn h<T, U, V>(x: Foo<T, U, V>)
where
Bar<T, U, V>: [const] Destruct,
{
ensure_const_destruct(x);
}

fn main() {}
Loading