Skip to content

Implement ~const Destruct effect goal in the new solver#132329

Merged
bors merged 5 commits into
rust-lang:masterfrom
compiler-errors:fn-and-destruct
Nov 23, 2024
Merged

Implement ~const Destruct effect goal in the new solver#132329
bors merged 5 commits into
rust-lang:masterfrom
compiler-errors:fn-and-destruct

Conversation

@compiler-errors
Copy link
Copy Markdown
Contributor

@compiler-errors compiler-errors commented Oct 29, 2024

This also fixed a subtle bug/limitation of the NeedsConstDrop check. Specifically, the "Qualif" API basically treats const drops as totally structural, even though dropping something that has an explicit Drop implementation cannot be structurally decomposed. For example:

#![feature(const_trait_impl)]

#[const_trait] trait Foo {
    fn foo();
}

struct Conditional<T: Foo>(T);

impl Foo for () {
    fn foo() {
        println!("uh oh");
    }
}

impl<T> const Drop for Conditional<T> where T: ~const Foo {
    fn drop(&mut self) {
        T::foo();
    }
}

const FOO: () = {
    let _ = Conditional(());
    //~^ This should error.
};

fn main() {}

In this example, when checking if the Conditional(()) rvalue is const-drop, since Conditional has a const destructor, we would previously recurse into the () value and determine it has nothing to drop, which means that it is considered to not need a const drop -- even though dropping Conditional(()) would mean evaluating the destructor which relies on that T: const Foo bound to hold!

This could be fixed alternatively by banning any const conditions on const Drop impls, but that really sucks -- that means that basically no interesting const drop impls could be written. We have the capability to totally and intuitively support the right behavior, which I've implemented here.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. PG-const-traits Project group: Const traits S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants