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

Conversation

ShoyuVanilla
Copy link
Member

@ShoyuVanilla ShoyuVanilla commented Aug 3, 2025

Fixes #144207

@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 3, 2025
@@ -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?

@compiler-errors
Copy link
Member

cc @rust-lang/project-const-traits this behavior definitely needs to be noted somewhere, and I'm not totally sure if it makes sense to imply by default, because it may have some unnecessary semver observable implications.

),
);

// `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 😅

@ShoyuVanilla
Copy link
Member Author

ShoyuVanilla commented Aug 4, 2025

it may have some unnecessary semver observable implications.

I've thought more about this, and I believe there might be semver-breaking implications. For example:

// crate A
pub struct Foo<T> {
    inner: Bar<T>,
    // ...
}

// crate B, depends on crate A
const fn foobar<T>(foo: Foo<T>, bar: Bar<T>)
where
    Foo<T>: [const] Destruct,
{
    // Some code relying on the implied bound: `Bar<T>: [const] Destruct`
}

If crate A removes the private field inner: Bar<T> from Foo<T>, then foobar in crate B might break due to the loss of the implicitly inferred bound.

And I think the inverse can also happen with the current nightly rustc. For instance:

If crate A deletes private field inner: Bar<T> from Foo<T>, const fn foobar in crate B might suffer from the removal of implicit bound.

And I think the opposite direction might be happen in current nightly rustc, like:

pub struct Foo {
    // All fields currently satisfy `const Destruct`
    // ...
}

If a private field that does not implement const Destruct is added to Foo, it may invalidate the previously inferred Foo: const Destruct bound.

This suggests we might need to restrict structural Destruct inference to public fields only, to avoid semver breakage from purely private structural changes. 🤔

@fee1-dead
Copy link
Member

fee1-dead commented Aug 4, 2025

I think we'd probably want to resolve this at the most restricted scope, i.e. when comparing impl'd method bounds with trait method bounds.

I'm imagining us doing some sort of special elaboration for T: ~const Destruct bounds to pierce destruct bounds (Option<T>: ~const Destruct) into public fields. Since Option has all public fields we'd replace it with T: ~const Destruct. If a struct has both public and private fields e.g. struct Foo<T1, T2> { pub f1: T1, f2: T2, } we elaborate it to both Foo<T1, T2>: ~const Destruct and T1: ~const Destruct

This elaboration should also be gated behind a separate feature as it will probably have to stabilize separately to const traits

@ShoyuVanilla
Copy link
Member Author

ShoyuVanilla commented Aug 4, 2025

I think we'd probably want to resolve this at the most restricted scope, i.e. when comparing impl'd method bounds with trait method bounds.

I'm imagining us doing some sort of special elaboration for T: ~const Destruct bounds to pierce destruct bounds (Option<T>: ~const Destruct) into public fields. Since Option has all public fields we'd replace it with T: ~const Destruct. If a struct has both public and private fields e.g. struct Foo<T1, T2> { pub f1: T1, f2: T2, } we elaborate it to both Foo<T1, T2>: ~const Destruct and T1: ~const Destruct

This elaboration should also be gated behind a separate feature as it will probably have to stabilize separately to const traits

That sounds like the right approach to address the semver issue. Thanks! I’ll give it a try.

By the way, could I also ask your thoughts on the opposite direction? Specifically: what happens if we add a new private field to an ADT that does not implement const Destruct? Would that retroactively break any inferred const Destruct bounds?

(Sorry in advance if this is already designed/answered somewhere or just a misguided question 😅 )
Edit) Oh, I guess you meant both directions with the phrase "pierce destruct bounds into public fields" 😅 Nevermind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infer the [const] Destruct bound on fields from [const] Destruct bound on struct
5 participants