Files
carbon-lang/core/prelude/destroy.carbon
T
Jon Ross-Perkins 4a6376cf59 Rename/restructure Destroy logic to better reflect #6124 (#6144)
This also does a little restructuring in the same direction, following
#6124.

Leads want `Destroy` to work similarly now for all types. As a
consequence, there doesn't seem to be as much benefit to splitting off
aggregate destruction. In this PR, the `type.destroy` function can now
be expected to destroy anything that's destructible; that means it'll be
usable for the `final fn` once that support is available.

Similarly, this gets rid of the impls other than the single blanket
impl, now using `type.can_destroy`. Since they all need to use the same
function, there's no benefit to splitting approaches. Also, now it can
just be a `final impl` since there should be no need for people to
create specializations -- if this blanket impl applies, it means the
`final fn` is the same.

This also slips in `partial` support since there's no reason to have it
diverge anymore. Also `abstract`, which I'm not sure is broadly testable
since most cases it'd come up, the `abstract` keyword is explicitly
detected/rejected.

Note though that this doesn't make any really big changes. It's just
realigning on the leads decision. I'm going this way to try to reduce
name-related churn for other changes.
2025-09-30 20:43:36 +00:00

26 lines
908 B
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
package Core library "prelude/destroy";
// TODO: Add `Destructor`, as in:
// interface Destructor {
// private fn Op[ref self: Self]();
// }
// Destroys objects. This will invoke `Destructor` impls recursively on members;
// it does not deallocate memory.
interface Destroy {
// TODO: This should be `final fn Op[ref self: Self]() = "type.destroy"`.
fn Op[addr self: Self*]();
}
// Returns a constraint that matches all types that should have a `Destroy` impl.
private fn CanDestroy() -> type = "type.can_destroy";
// Destroys an instance of `DestroyT`. This is also used for trivial types.
final impl forall [DestroyT:! CanDestroy()] DestroyT as Destroy {
fn Op[addr self: Self*]() = "type.destroy";
}